import { ShareLinkInfo, UpdateShareLinkRequest } from '@shumai/dtos' import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos' import { client } from '@/ui/api/client' import { Button } from '@/ui/components/ui/button' import { Input } from '@/ui/components/ui/input' import { Label } from '@/ui/components/ui/label' import { Switch } from '@/ui/components/ui/switch' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { ChevronRight, Copy, ExternalLink, Shield, Settings2, Palette, ListFilter, SortAsc, } from 'lucide-react' import React, { useState } from 'react' import { toast } from 'sonner' import { DateTimePicker } from '@/ui/components/datetime-picker' interface ShareSettingsSidebarProps { shareLink: ShareLinkInfo } export function ShareSettingsSidebar({ shareLink }: ShareSettingsSidebarProps) { const queryClient = useQueryClient() const [password, setPassword] = useState('') const [expireAt, setExpireAt] = useState( shareLink.expireAt ? new Date(shareLink.expireAt) : undefined, ) const [isPasswordEnabled, setIsPasswordEnabled] = useState(shareLink.hasPassword) const [isExpireEnabled, setIsExpireEnabled] = useState(!!shareLink.expireAt) const { data: fields } = useQuery({ queryKey: ['fields', shareLink.projectId], queryFn: async () => { const res = await client.api.projects[':projectId'].fields.$get({ param: { projectId: shareLink.projectId }, }) if (!res.ok) throw new Error('Failed to fetch fields') return await res.json() }, enabled: !!shareLink.projectId, }) const $updateShare = client.api.shares[':shareId'].$put const { mutate: updateShare } = useMutation({ mutationFn: async (json: UpdateShareLinkRequest) => { const res = await $updateShare({ param: { shareId: shareLink.id }, json, }) if (!res.ok) throw new Error('Failed to update') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['shares', shareLink.projectId] }) queryClient.invalidateQueries({ queryKey: ['share', shareLink.id] }) toast.success('Settings updated') }, }) const shareUrl = `${window.location.origin}/share/${shareLink.id}` const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text) toast.success('Copied to clipboard') } const toggleFieldVisibility = (fieldId: string) => { const current = shareLink.fieldVisibility || {} const next = { ...current, [fieldId]: !current[fieldId] } updateShare({ fieldVisibility: next }) } return (

Link Visibility

updateShare({ isDisabled: !checked })} />
} /> }>
{ setIsPasswordEnabled(checked) if (!checked) updateShare({ password: null }) }} />
{isPasswordEnabled && (
setPassword(e.target.value)} placeholder={shareLink.hasPassword ? '••••••••' : 'Set password'} className="text-xs" />
)}
{ setIsExpireEnabled(checked) if (!checked) { setExpireAt(undefined) updateShare({ expireAt: null }) } }} />
{isExpireEnabled && (
{ setExpireAt(date) updateShare({ expireAt: date ? date.toISOString() : null }) }} className="text-xs" />
)}
} /> }>
{(fields as MetadataFieldInfo[])?.map((field) => (
toggleFieldVisibility(field.id)} />
))}
} />
) } function SidebarAccordionItem({ title, icon, children, }: { title: string icon: React.ReactNode children?: React.ReactNode }) { const [isOpen, setIsOpen] = useState(false) return (
{isOpen && children && (
{children}
)}
) } function cn(...classes: (string | boolean | undefined | null)[]) { return classes.filter(Boolean).join(' ') }