import { useState, useEffect } from 'react'; import { Form, FormField, FormSubmitButton, Row, TextField, Button, Label, Column, Icon, Loading, } from '@umami/react-zen'; import { useConfig, useLinkQuery } from '@/components/hooks'; import { useMessages } from '@/components/hooks'; import { Refresh } from '@/components/icons'; import { getRandomChars } from '@/lib/crypto'; import { useUpdateQuery } from '@/components/hooks/queries/useUpdateQuery'; import { LINKS_URL } from '@/lib/constants'; 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 } = useMessages(); const { mutate, error, isPending, touch } = useUpdateQuery( linkId ? `/links/${linkId}` : '/links', { id: linkId, teamId, }, ); const { linksUrl } = useConfig(); const hostUrl = linksUrl || LINKS_URL; const { data, isLoading } = useLinkQuery(linkId); const [slug, setSlug] = useState(generateId()); const handleSubmit = async (data: any) => { mutate(data, { onSuccess: async () => { touch('links'); onSave?.(); onClose?.(); }, }); }; const handleSlug = () => { const slug = generateId(); setSlug(slug); return slug; }; const checkUrl = (url: string) => { if (!isValidUrl(url)) { return formatMessage(labels.invalidUrl); } return true; }; useEffect(() => { if (data) { setSlug(data.slug); } }, [data]); if (linkId && isLoading) { return ; } return (
{({ setValue }) => { return ( <> {onClose && ( )} {formatMessage(labels.save)} ); }}
); }