Copied.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use client';
  2. import './style.scss';
  3. import { useEffect, useState } from 'react';
  4. import { Copy } from 'lucide-react';
  5. import { Button } from '@/components/ui/button';
  6. import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  7. import { Input } from '@/components/ui/input';
  8. import { Label } from '@/components/ui/label';
  9. type Props = {
  10. isEnable: boolean;
  11. open: boolean;
  12. onChange: (open: boolean) => void;
  13. }
  14. export default function Copied({ isEnable, open, onChange }: Props) {
  15. const [url, setUrl] = useState<string>('');
  16. useEffect(() => {
  17. if (typeof window !== 'undefined') {
  18. setUrl(window.location.href);
  19. }
  20. }, []);
  21. if (!isEnable) {
  22. return;
  23. }
  24. const handleCopy = async () => {
  25. try {
  26. await navigator.clipboard.writeText(url);
  27. alert("주소가 복사되었습니다.");
  28. onChange(false);
  29. } catch (err) {
  30. console.error("복사 실패:", err);
  31. }
  32. };
  33. return (
  34. <Dialog open={open} onOpenChange={onChange}>
  35. <DialogContent className='sm:max-w-md'>
  36. <DialogHeader>
  37. <DialogTitle>주소 복사</DialogTitle>
  38. <DialogDescription>
  39. 이 주소를 복사하여 다른 곳에 붙여넣기 하세요.
  40. </DialogDescription>
  41. </DialogHeader>
  42. <div className='flex items-center space-x-2'>
  43. <div className='grid flex-1 gap-2'>
  44. <Label htmlFor='link' className='sr-only'>
  45. Link
  46. </Label>
  47. <Input id='link' defaultValue={url} readOnly />
  48. </div>
  49. <Button type='submit' size='sm' className='px-3' onClick={handleCopy}>
  50. <span className='sr-only'>Copy</span>
  51. <Copy />
  52. </Button>
  53. </div>
  54. </DialogContent>
  55. </Dialog>
  56. );
  57. }