import { Button, Column, Form, FormField, FormSubmitButton, Icon, Label, Loading, Row, TextField, } from '@umami/react-zen'; import { useState } from 'react'; import { useConfig, useLinkQuery, useMessages } from '@/components/hooks'; import { useUpdateQuery } from '@/components/hooks/queries/useUpdateQuery'; import { ChevronDown, ChevronRight, RefreshCw } from '@/components/icons'; import { LINKS_URL } from '@/lib/constants'; import { getRandomChars } from '@/lib/generate'; import { isValidUrl } from '@/lib/url'; const generateId = () => getRandomChars(9); export function LinkEditForm({ linkId, teamId, onSave, onClose, }: { linkId?: string; teamId?: string; onSave?: () => void; onClose?: () => void; }) { const { formatMessage, labels, messages, getErrorMessage } = useMessages(); const { mutateAsync, error, isPending, touch, toast } = useUpdateQuery( linkId ? `/links/${linkId}` : '/links', { id: linkId, teamId, }, ); const { linksUrl } = useConfig(); const hostUrl = linksUrl || LINKS_URL; const { data, isLoading } = useLinkQuery(linkId); const [initialSlug] = useState(() => generateId()); const [showAdvanced, setShowAdvanced] = useState(false); const handleSubmit = async (formData: any) => { const { slug: formSlug, ...rest } = formData; // Only include slug if creating new link or if it was modified const payload = !linkId || formSlug !== data?.slug ? formData : rest; await mutateAsync(payload, { onSuccess: async () => { toast(formatMessage(messages.saved)); touch('links'); onSave?.(); onClose?.(); }, }); }; const handleSlug = () => generateId(); const checkUrl = (url: string) => { if (!isValidUrl(url)) { return formatMessage(labels.invalidUrl); } return true; }; if (linkId && isLoading) { return ; } return (
{({ setValue, watch }) => { const currentSlug = watch('slug') ?? initialSlug; return ( <> setShowAdvanced(!showAdvanced)} > {showAdvanced ? : } {showAdvanced && ( )} {onClose && ( )} {formatMessage(labels.save)} ); }}
); }