import { client } from '@/ui/api/client' import { ShareSecuritySettings } from '@/ui/components/share-security-settings' import { Button } from '@/ui/components/ui/button' import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@/ui/components/ui/dialog' import { Input } from '@/ui/components/ui/input' import { copyToClipboard } from '@/ui/lib/clipboard' import { m } from '@/ui/paraglide/messages.js' import type { ShareLinkInfo } from '@shumai/dtos' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' import { ArrowUpRight, Check, Copy, Pencil } from 'lucide-react' import React, { useEffect, useRef, useState } from 'react' import { toast } from 'sonner' interface ShareLinkCreatedDialogProps { open: boolean onOpenChange: (open: boolean) => void shareLink: ShareLinkInfo | null projectId: string } export function ShareLinkCreatedDialog({ open, onOpenChange, shareLink, projectId, }: ShareLinkCreatedDialogProps) { const navigate = useNavigate() const queryClient = useQueryClient() const [isEditingName, setIsEditingName] = useState(false) const [nameInput, setNameInput] = useState('') const [copied, setCopied] = useState(false) const nameInputRef = useRef(null) const cancelEditRef = useRef(false) const copiedTimeoutRef = useRef | null>(null) const shareId = shareLink?.id const { data: liveShareLink } = useQuery({ queryKey: ['share', shareId], queryFn: async () => { if (!shareId) return null const res = await client.api.shares[':shareId'].$get({ param: { shareId }, }) if (!res.ok) throw new Error(m.failed_fetch_share_link()) return (await res.json()) as unknown as ShareLinkInfo }, initialData: shareLink ?? undefined, enabled: open && !!shareId, }) const activeShare = liveShareLink ?? shareLink useEffect(() => { if (!open) { setIsEditingName(false) setCopied(false) if (copiedTimeoutRef.current) { clearTimeout(copiedTimeoutRef.current) copiedTimeoutRef.current = null } } }, [open]) useEffect(() => { return () => { if (copiedTimeoutRef.current) { clearTimeout(copiedTimeoutRef.current) } } }, []) useEffect(() => { if (isEditingName) { nameInputRef.current?.focus() nameInputRef.current?.select() } }, [isEditingName]) const { mutate: renameShare, isPending: isRenaming } = useMutation({ mutationFn: async (name: string) => { if (!activeShare) throw new Error(m.failed_rename_share_link()) const res = await client.api.shares[':shareId'].$put({ param: { shareId: activeShare.id }, json: { name }, }) if (!res.ok) throw new Error(m.failed_rename_share_link()) return (await res.json()) as unknown as ShareLinkInfo }, onSuccess: (updated) => { queryClient.setQueryData(['share', updated.id], updated) queryClient.invalidateQueries({ queryKey: ['shares', projectId] }) queryClient.invalidateQueries({ queryKey: ['share', updated.id] }) toast.success(m.share_link_renamed()) }, onError: () => { toast.error(m.failed_rename_share_link()) }, }) if (!activeShare) { return null } const shareUrl = `${window.location.origin}/share/${activeShare.id}` const startEditingName = () => { cancelEditRef.current = false setNameInput(activeShare.name) setIsEditingName(true) } const commitRename = () => { if (cancelEditRef.current) { cancelEditRef.current = false return } const trimmed = nameInput.trim() setIsEditingName(false) if (!trimmed || trimmed === activeShare.name) { return } renameShare(trimmed) } const handleCopyLink = async () => { const ok = await copyToClipboard(shareUrl) if (ok) { toast.success(m.copied_to_clipboard()) setCopied(true) if (copiedTimeoutRef.current) { clearTimeout(copiedTimeoutRef.current) } copiedTimeoutRef.current = setTimeout(() => { setCopied(false) copiedTimeoutRef.current = null }, 2000) } } const handleAdvancedSettings = () => { onOpenChange(false) navigate({ to: '/projects/$projectId/shares/$shareId', params: { projectId, shareId: activeShare.id }, }) } return ( {isEditingName ? ( <> {m.rename_share_link()}
setNameInput(e.target.value)} onBlur={commitRename} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault() commitRename() } else if (e.key === 'Escape') { e.preventDefault() e.stopPropagation() cancelEditRef.current = true setIsEditingName(false) } }} aria-label={m.rename_share_link()} className="h-8 min-w-0 flex-1 text-sm font-semibold" />
) : ( {activeShare.name} )}
) }