import { Form, FormRow, FormButtons, Flexbox, TextField, Button, Toggle, LoadingButton, } from 'react-basics'; import { useContext, useState } from 'react'; import { getRandomChars } from 'next-basics'; import { useApi, useMessages, useModified } from 'components/hooks'; import { WebsiteContext } from 'app/(main)/websites/[websiteId]/WebsiteProvider'; const generateId = () => getRandomChars(16); export function ShareUrl({ hostUrl, onSave, }: { websiteId: string; hostUrl?: string; onSave?: () => void; }) { const website = useContext(WebsiteContext); const { domain, shareId } = website; const { formatMessage, labels, messages } = useMessages(); const [id, setId] = useState(shareId); const { post, useMutation } = useApi(); const { mutate, error, isPending } = useMutation({ mutationFn: (data: any) => post(`/websites/${website.id}`, data), }); const { touch } = useModified(); const url = `${hostUrl || process.env.hostUrl || window?.location.origin}${ process.env.basePath }/share/${id}/${domain}`; const handleGenerate = () => { setId(generateId()); }; const handleCheck = (checked: boolean) => { const data = { shareId: checked ? generateId() : null }; mutate(data, { onSuccess: async () => { touch(`website:${website.id}`); onSave?.(); }, }); setId(data.shareId); }; const handleSave = () => { mutate( { shareId: id }, { onSuccess: async () => { touch(`website:${website.id}`); onSave?.(); }, }, ); }; return ( <> {formatMessage(labels.enableShareUrl)} {id && (

{formatMessage(messages.shareUrl)}

{formatMessage(labels.save)}
)} ); } export default ShareUrl;