()
| 18 | import { Button } from './ui/button' |
| 19 | |
| 20 | export default function ShareDialog() { |
| 21 | const params = useParams() |
| 22 | const id = params.id ?? 'new' |
| 23 | const [rawItem, setRawItem] = useAtom(historyAtomFamily({ id })) |
| 24 | const item = useMemo( |
| 25 | () => new ItemWrapper(rawItem, setRawItem), |
| 26 | [rawItem, setRawItem] |
| 27 | ) |
| 28 | const [open, setOpen] = useState<boolean>(false) |
| 29 | const [error, setError] = useState<string | undefined>() |
| 30 | const [versionIdx] = useVersion(item) |
| 31 | |
| 32 | useEffect(() => { |
| 33 | if (open) { |
| 34 | // TODO: support the other frameworks, maybe versions? |
| 35 | share(id, item, versionIdx) |
| 36 | .then(() => { |
| 37 | copyTextToClipboard( |
| 38 | document.location.href.replace('/ai', '/ai/shared') |
| 39 | ) |
| 40 | }) |
| 41 | .catch((error_: unknown) => { |
| 42 | console.error('Share error', error_) |
| 43 | setError((error_ as Error).toString()) |
| 44 | }) |
| 45 | } |
| 46 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 47 | }, [id, open]) |
| 48 | |
| 49 | return ( |
| 50 | <Dialog onOpenChange={open_ => setOpen(open_)}> |
| 51 | <Tooltip> |
| 52 | <TooltipTrigger asChild> |
| 53 | <DialogTrigger asChild> |
| 54 | <Button variant='ghost' className='-mr-4 hover:bg-transparent'> |
| 55 | <Share2Icon /> |
| 56 | </Button> |
| 57 | </DialogTrigger> |
| 58 | </TooltipTrigger> |
| 59 | <TooltipContent side='bottom'>Share this version</TooltipContent> |
| 60 | </Tooltip> |
| 61 | <DialogContent className='sm:max-w-[425px]'> |
| 62 | <DialogHeader> |
| 63 | <DialogTitle>Share</DialogTitle> |
| 64 | {error ? ( |
| 65 | <DialogDescription className='mb-2 text-red-500 dark:text-red-400'> |
| 66 | {error} |
| 67 | </DialogDescription> |
| 68 | ) : ( |
| 69 | <DialogDescription> |
| 70 | Copy the link below to share your creation |
| 71 | </DialogDescription> |
| 72 | )} |
| 73 | </DialogHeader> |
| 74 | <div className='items-center'> |
| 75 | <input |
| 76 | type='text' |
| 77 | value={document.location.href.replace('/ai', '/ai/shared')} |
nothing calls this directly
no test coverage detected