"use client"; import { useEffect, useRef, useState } from "react"; import { Alert, Button, Group, Paper, Stack, Text, TextInput, ThemeIcon, Title } from "@mantine/core"; import { IconArrowRight, IconShieldCheck, IconUserPlus } from "@tabler/icons-react"; import type { z } from "zod/v4"; import { clientApi } from "@homarr/api/client"; import { signIn } from "@homarr/auth/client"; import { revalidatePathActionAsync } from "@homarr/common/client"; import { useZodForm } from "@homarr/form"; import { UserCreatePasswordFields } from "@homarr/forms-collection"; import { useScopedI18n } from "@homarr/translation/client"; import { Link } from "@homarr/ui"; import { groupCreateSchema } from "@homarr/validation/group"; import { userInitSchema } from "@homarr/validation/user"; import { didCredentialsSignInFail } from "./account-recovery"; import type { OnboardingStudioProps } from "./types"; import classes from "./onboarding-studio.module.css"; const AccountShell = ({ title, description, children, }: { title: string; description: string; children: React.ReactNode; }) => (
{title} {description} {children}
); export const AccountSetup = ({ environment }: OnboardingStudioProps) => { if (environment.currentStep === "user") return ; return ; }; const CredentialsSetup = () => { const t = useScopedI18n("init.studio.account"); const [requiresSignIn, setRequiresSignIn] = useState(false); const recoveryActionRef = useRef(null); const mutation = clientApi.user.initUser.useMutation(); const form = useZodForm(userInitSchema, { initialValues: { username: "", password: "", confirmPassword: "" }, }); const submitAsync = async (values: z.infer) => { try { await mutation.mutateAsync(values); } catch (error) { form.setErrors({ username: error instanceof Error ? error.message : t("unknownError") }); return; } try { const result = await signIn("credentials", { name: values.username, password: values.password, redirect: false, }); if (didCredentialsSignInFail(result)) throw new Error(); } catch { setRequiresSignIn(true); await revalidatePathActionAsync("/init").catch(() => undefined); return; } await revalidatePathActionAsync("/init"); }; useEffect(() => { if (requiresSignIn) recoveryActionRef.current?.focus(); }, [requiresSignIn]); if (requiresSignIn) { return ( ); } return (
void submitAsync(values))}> {mutation.error ? ( {mutation.error.message} ) : null}
); }; const ExternalGroupSetup = () => { const t = useScopedI18n("init.studio.externalGroup"); const mutation = clientApi.group.createInitialExternalGroup.useMutation({ async onSuccess() { await revalidatePathActionAsync("/init"); }, }); const form = useZodForm(groupCreateSchema, { initialValues: { name: "" } }); return (
mutation.mutate(values))}> } withAsterisk {...form.getInputProps("name")} /> {mutation.error ? ( {mutation.error.message} ) : null}
); };