SnsShare.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. let url = encodeURIComponent(window.location.href);
  19. switch (type) {
  20. case 'facebook':
  21. url = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
  22. break;
  23. case 'twitter':
  24. url = `https://twitter.com/intent/tweet?url=${url}`;
  25. break;
  26. case 'reddit':
  27. url = `https://www.reddit.com/submit?url=${url}`;
  28. break;
  29. case 'band':
  30. url = `https://band.us/plugin/share?body=${url}`;
  31. break;
  32. default:
  33. return;
  34. }
  35. const width = 600;
  36. const height = 400;
  37. const left = (window.screenX + (window.outerWidth - width) / 2);
  38. const top = (window.screenY + (window.outerHeight - height) / 2);
  39. window.open(url, '_blank', `width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`);
  40. }
  41. onChange(false);
  42. }, []);
  43. if (!isEnable) {
  44. return;
  45. }
  46. return (
  47. <Dialog open={open} onOpenChange={onChange}>
  48. <DialogContent className='sm:max-w-md'>
  49. <DialogHeader>
  50. <DialogTitle>SNS 공유</DialogTitle>
  51. </DialogHeader>
  52. <div className='flex flex-col gap-2'>
  53. <Button variant='outline' onClick={() => handleSnsShare("facebook")}><FontAwesomeIcon icon={faFacebook} /> Facebook</Button>
  54. <Button variant='outline' onClick={() => handleSnsShare("twitter")}><FontAwesomeIcon icon={faTwitter} /> Twitter</Button>
  55. <Button variant='outline' onClick={() => handleSnsShare("reddit")}><FontAwesomeIcon icon={faReddit} /> Reddit</Button>
  56. <Button variant='outline' onClick={() => handleSnsShare("band")}><FontAwesomeIcon icon={faNairaSign} /> Band</Button>
  57. </div>
  58. </DialogContent>
  59. </Dialog>
  60. );
  61. }