SnsShare.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use client';
  2. import './style.scss';
  3. import { useCallback } from 'react';
  4. import { Button } from '@/components/ui/button';
  5. import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  6. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
  7. import { faNairaSign } from '@fortawesome/free-solid-svg-icons';
  8. import { faFacebook, faTwitter, faReddit } from '@fortawesome/free-brands-svg-icons';
  9. type Props = {
  10. isEnable: boolean;
  11. open: boolean;
  12. onChange: (open: boolean) => void;
  13. }
  14. export default function SnsShare({ isEnable, open, onChange }: Props)
  15. {
  16. const handleSnsShare = useCallback((type: string) => {
  17. if (!type) {
  18. return;
  19. }
  20. let url = encodeURIComponent(window.location.href);
  21. switch (type) {
  22. case 'facebook':
  23. url = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
  24. break;
  25. case 'twitter':
  26. url = `https://twitter.com/intent/tweet?url=${url}`;
  27. break;
  28. case 'reddit':
  29. url = `https://www.reddit.com/submit?url=${url}`;
  30. break;
  31. case 'band':
  32. url = `https://band.us/plugin/share?body=${url}`;
  33. break;
  34. default:
  35. return;
  36. }
  37. const width = 600;
  38. const height = 400;
  39. const left = (window.screenX + (window.outerWidth - width) / 2);
  40. const top = (window.screenY + (window.outerHeight - height) / 2);
  41. window.open(url, '_blank', `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`);
  42. onChange(false);
  43. }, [onChange]);
  44. if (!isEnable) {
  45. return null;
  46. }
  47. return (
  48. <Dialog open={open} onOpenChange={onChange}>
  49. <DialogContent className='sm:max-w-md'>
  50. <DialogHeader>
  51. <DialogTitle>SNS 공유</DialogTitle>
  52. </DialogHeader>
  53. <div className='flex flex-col gap-2'>
  54. <Button variant='outline' onClick={() => handleSnsShare("facebook")}><FontAwesomeIcon icon={faFacebook} /> Facebook</Button>
  55. <Button variant='outline' onClick={() => handleSnsShare("twitter")}><FontAwesomeIcon icon={faTwitter} /> Twitter</Button>
  56. <Button variant='outline' onClick={() => handleSnsShare("reddit")}><FontAwesomeIcon icon={faReddit} /> Reddit</Button>
  57. <Button variant='outline' onClick={() => handleSnsShare("band")}><FontAwesomeIcon icon={faNairaSign} /> Band</Button>
  58. </div>
  59. </DialogContent>
  60. </Dialog>
  61. );
  62. }