import { useState, type ReactNode } from 'react'; import { LOCALE_LABELS, SUPPORTED_LOCALES, isLocale, resolveLocale, slugifyWorkspaceName, } from '@wellformed/shared'; import { api } from '../api/client'; import { useI18n } from '../i18n'; import { useSession } from '../state/session'; import { useDismissSsr } from '../state/ssr'; import { useToasts } from '../state/toasts'; import { goTo, workspacePath } from '../state/route'; /** * Where a signed-in account lands when the URL names no workspace, or names one * it cannot open. Both cases are the same question — which of these do you * want? — so they get the same screen, with a line of explanation when the URL * was the reason. */ export function WorkspacePicker({ unknownSlug, startCreating = false, }: { unknownSlug?: string; startCreating?: boolean; }): ReactNode { const session = useSession(); const toasts = useToasts(); const { t } = useI18n(); useDismissSsr(); const [creating, setCreating] = useState(startCreating || session.workspaces.length === 0); const [name, setName] = useState(''); // Off by default: most people arrive with the spreadsheet they want to // replace, and a demo on top of real intent is furniture in the way. const [demoStructure, setDemoStructure] = useState(false); const [demoData, setDemoData] = useState(false); // Asked here, not guessed from a header: the language decides how every // number and date in the workspace is written, and someone who imports // "12-3-2021" should never first learn the answer from "3/12/2021". const [locale, setLocale] = useState(() => resolveLocale(navigator.languages ?? [navigator.language]), ); const [busy, setBusy] = useState(false); async function create() { setBusy(true); try { const { workspace } = await api.createWorkspace({ name: name.trim(), demo: demoStructure ? (demoData ? 'full' : 'structure') : 'none', locale, }); goTo(workspacePath(workspace.slug)); } catch (error) { toasts.fail(error); setBusy(false); } } return (
{t('account.signedInAs')} {session.account?.email}.{' '}