File size: 3,385 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {
   HStack,
   IconButton,
   Menu,
   MenuButton,
   MenuList,
   Text,
   useClipboard,
   useColorModeValue,
   useToast,
} from '@chakra-ui/react';
import React from 'react';
import { RiFileCopy2Fill } from 'react-icons/ri';
import { Option } from '../../assets/icons';
import { isTouchDevice } from '../../helper/isTouchDevice';
import CustomMenuItem from '../../utils/CustomMenuItem';

const MoreOptionMenu = ({ iconStyles, postTitle, reactionIconColor }) => {
   const toast = useToast();
   const postURL = window.location.href;

   const { onCopy } = useClipboard(postURL);

   const handleCopyLink = () => {
      onCopy(); // copy to clipboard

      const id = 'toast';

      if (!toast.isActive(id)) {
         toast({
            title: 'Copied to Clipboard',
            position: 'top-right',
            duration: 1000,
            status: 'success',
            isClosable: true,
            id,
         });
      }
   };

   const handleShareVia = (via) => {
      let url = '';
      switch (via) {
         case 'twitter':
            url = `https://twitter.com/share?url=${postURL}&text=${postTitle}`;
            break;
         case 'linkedin':
            url = `https://www.linkedin.com/shareArticle?url=${postURL}&title=${postTitle}`;
            break;
         case 'facebook':
            url = `https://www.facebook.com/sharer.php?u=${postURL}`;
            break;
         default:
            return;
      }

      window.open(url, '_blank'); // open link in new tab
   };

   const nativeShareVia = async () => {
      try {
         await navigator.share({ title: postTitle, url: postURL });
         // console.log('successfully share url');
      } catch (err) {
         console.log(err);
      }
   };

   return (
      <Menu autoSelect={false} isLazy>
         <MenuButton
            _hover={{
               '.moreBtn': { bg: useColorModeValue('gray.100', 'dark.cardBg') },
            }}
         >
            <IconButton
               as='div'
               icon={<Option fill={reactionIconColor} />}
               {...iconStyles}
               className='moreBtn'
            />
         </MenuButton>

         <MenuList
            p='.5rem'
            minW={{ base: '0 !important' }}
            w='250px'
            bg={useColorModeValue('light.cardBg', 'dark.cardBg')}
         >
            <CustomMenuItem onClick={handleCopyLink}>
               <HStack justify='space-between' w='100%'>
                  <Text>Copy Link</Text> <RiFileCopy2Fill size={20} />
               </HStack>
            </CustomMenuItem>

            {!isTouchDevice() && (
               <>
                  <CustomMenuItem onClick={() => handleShareVia('twitter')}>
                     Share to Twitter
                  </CustomMenuItem>

                  <CustomMenuItem onClick={() => handleShareVia('linkedin')}>
                     Share to Linkedin
                  </CustomMenuItem>

                  <CustomMenuItem onClick={() => handleShareVia('facebook')}>
                     Share to Facebook
                  </CustomMenuItem>
               </>
            )}

            {isTouchDevice() && (
               <CustomMenuItem onClick={nativeShareVia}>
                  Share post via...
               </CustomMenuItem>
            )}
         </MenuList>
      </Menu>
   );
};

export default MoreOptionMenu;