"use client"; import { useState } from "react"; import Link from "next/link"; import { IconCheck, IconCopy, IconDice5, IconUserPlus } from "@tabler/icons-react"; import { useT } from "@/i18n/client"; import { loadPools } from "@/lib/name-client"; import { BUILT_IN_POOLS, generateNames, type NameOptions, type NamePools } from "@/lib/names"; const THIS_YEAR = new Date().getFullYear(); export function NameGenerator({ bookId }: { bookId: string }) { const t = useT(); const [options, setOptions] = useState({ gender: "any", middleName: false, surname: "all" }); const [count, setCount] = useState(10); const [useStats, setUseStats] = useState(true); const [year, setYear] = useState(1990); const [surnameLimit, setSurnameLimit] = useState(1000); const [names, setNames] = useState([]); const [copied, setCopied] = useState(null); const [loading, setLoading] = useState(false); const [info, setInfo] = useState<{ sources: string[]; fellBack: boolean; adjustedYear?: number } | null>(null); async function roll() { setLoading(true); let pools: NamePools = BUILT_IN_POOLS; let next: typeof info = null; if (useStats) { const genders = options.gender === "any" ? (["female", "male"] as const) : ([options.gender] as const); const loaded = await loadPools({ year, surnameLimit, genders: [...genders] }); pools = loaded.pools; next = { sources: loaded.sources, fellBack: loaded.fellBack, adjustedYear: loaded.adjustedYear }; } setInfo(next); setNames(generateNames(options, count, pools)); setLoading(false); } async function copy(name: string) { try { await navigator.clipboard.writeText(name); setCopied(name); setTimeout(() => setCopied(null), 1500); } catch { // Urklipp kan vara blockerat, namnet går fortfarande att markera för hand } } return (
{(["female", "male", "any"] as const).map((g) => ( ))}
{useStats ? ( <>
setYear(Number(e.target.value) || 1990)} />
{t("names.ageApprox", { age: Math.max(0, THIS_YEAR - year) })}
) : null}
setCount(Math.min(30, Math.max(1, Number(e.target.value) || 1)))} />
{info?.fellBack ?
{t("names.fallback")}
: null} {info?.adjustedYear ?
{t("names.yearAdjusted", { year: info.adjustedYear })}
: null}
{names.length === 0 ? (
{t("names.empty")}
) : (
{names.map((name) => (
{name} {t("names.createCharacter")}
))}
)}
{info && info.sources.length > 0 ? (
{t("names.source", { sources: info.sources.join(" · ") })}
) : null}
); }