ActionButtonMenu.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { FC } from 'react';
  2. import { Button, Dropdown, Menu } from 'antd';
  3. import classNames from 'classnames';
  4. import dynamic from 'next/dynamic';
  5. import styles from './ActionButtonMenu.module.scss';
  6. import { ExternalAction } from '../../../interfaces/external-action';
  7. // Lazy loaded components
  8. const EllipsisOutlined = dynamic(() => import('@ant-design/icons/EllipsisOutlined'), {
  9. ssr: false,
  10. });
  11. const HeartOutlined = dynamic(() => import('@ant-design/icons/HeartOutlined'), {
  12. ssr: false,
  13. });
  14. const BellOutlined = dynamic(() => import('@ant-design/icons/BellOutlined'), {
  15. ssr: false,
  16. });
  17. const NOTIFY_KEY = 'notify';
  18. const FOLLOW_KEY = 'follow';
  19. export type ActionButtonMenuProps = {
  20. actions: ExternalAction[];
  21. showFollowItem?: boolean;
  22. showNotifyItem?: boolean;
  23. externalActionSelected: (action: ExternalAction) => void;
  24. notifyItemSelected: () => void;
  25. followItemSelected: () => void;
  26. className?: string;
  27. };
  28. export const ActionButtonMenu: FC<ActionButtonMenuProps> = ({
  29. actions,
  30. externalActionSelected,
  31. notifyItemSelected,
  32. followItemSelected,
  33. showFollowItem,
  34. showNotifyItem,
  35. className,
  36. }) => {
  37. const onMenuClick = a => {
  38. if (a.key === NOTIFY_KEY) {
  39. notifyItemSelected();
  40. return;
  41. }
  42. if (a.key === FOLLOW_KEY) {
  43. followItemSelected();
  44. return;
  45. }
  46. const action = actions.find(x => x.url === a.key);
  47. externalActionSelected(action);
  48. };
  49. const items = actions.map(action => ({
  50. key: action.url,
  51. label: (
  52. <span className={styles.item}>
  53. {action.icon && <img className={styles.icon} src={action.icon} alt={action.title} />}{' '}
  54. {action.title}
  55. </span>
  56. ),
  57. }));
  58. if (showFollowItem) {
  59. items.unshift({
  60. key: FOLLOW_KEY,
  61. label: (
  62. <span className={styles.item}>
  63. <HeartOutlined className={styles.icon} /> Follow this stream
  64. </span>
  65. ),
  66. });
  67. }
  68. if (showNotifyItem) {
  69. items.unshift({
  70. key: NOTIFY_KEY,
  71. label: (
  72. <span className={styles.item}>
  73. <BellOutlined className={styles.icon} />
  74. Notify when live
  75. </span>
  76. ),
  77. });
  78. }
  79. const menu = <Menu items={items} onClick={onMenuClick} />;
  80. const dropdownClasses = classNames([styles.menu, className]);
  81. return (
  82. <Dropdown
  83. overlay={menu}
  84. placement="bottomRight"
  85. trigger={['click']}
  86. className={dropdownClasses}
  87. >
  88. <div className={styles.buttonWrap}>
  89. <Button
  90. type="default"
  91. onClick={e => e.preventDefault()}
  92. size="large"
  93. icon={<EllipsisOutlined size={6} style={{ rotate: '90deg' }} />}
  94. />
  95. </div>
  96. </Dropdown>
  97. );
  98. };