|
|
@@ -0,0 +1,220 @@
|
|
|
+// 开公司向导:第一次用办公室时在这里开一间公司——公司名、你的称呼、几位同事。
|
|
|
+// 规格:docs/specs/company.md §4.2、§9。提交后引擎写下公司定义、给每位同事建档案,办公室随即出现。
|
|
|
+import { useState } from "react";
|
|
|
+import { Button } from "@/components/ui/button.js";
|
|
|
+import { useZCodeIntl } from "@/i18n/IntlProvider.js";
|
|
|
+import type { CompanyFoundingInput } from "./useCompany.js";
|
|
|
+
|
|
|
+interface CompanyFoundingProps {
|
|
|
+ /** 最近一次操作失败的原因;引擎的校验(称呼重复、角色设定太短)也走这里。 */
|
|
|
+ error: string | null;
|
|
|
+ onFound(input: CompanyFoundingInput): Promise<boolean>;
|
|
|
+}
|
|
|
+
|
|
|
+interface DraftEmployee {
|
|
|
+ name: string;
|
|
|
+ title: string;
|
|
|
+ rolePrompt: string;
|
|
|
+}
|
|
|
+
|
|
|
+const EMPTY_EMPLOYEE: DraftEmployee = { name: "", title: "", rolePrompt: "" };
|
|
|
+/** 一位同事的称呼最多这么长:引擎也是这么限的,写不下就不让提交。 */
|
|
|
+const NAME_CHARS = 12;
|
|
|
+const INPUT_CLASS =
|
|
|
+ "w-full border-2 border-border bg-background px-2 py-1 text-ui-sm text-foreground placeholder:text-foreground-subtle focus:outline-none";
|
|
|
+
|
|
|
+export function CompanyFounding({ error, onFound }: CompanyFoundingProps) {
|
|
|
+ const { intl } = useZCodeIntl();
|
|
|
+ const [name, setName] = useState("");
|
|
|
+ const [me, setMe] = useState("");
|
|
|
+ // 人事负责人的称呼;空串表示暂不指定。
|
|
|
+ const [hr, setHr] = useState("");
|
|
|
+ const [employees, setEmployees] = useState<DraftEmployee[]>([{ ...EMPTY_EMPLOYEE }]);
|
|
|
+ const [submitting, setSubmitting] = useState(false);
|
|
|
+
|
|
|
+ const ready =
|
|
|
+ Boolean(name.trim() && me.trim()) &&
|
|
|
+ employees.every((item) => item.name.trim() && item.title.trim() && item.rolePrompt.trim());
|
|
|
+ const named = employees.filter((item) => item.name.trim());
|
|
|
+
|
|
|
+ const update = (index: number, patch: Partial<DraftEmployee>): void => {
|
|
|
+ setEmployees((list) => list.map((item, at) => (at === index ? { ...item, ...patch } : item)));
|
|
|
+ };
|
|
|
+ const remove = (index: number): void => {
|
|
|
+ setEmployees((list) => (list.length > 1 ? list.filter((_item, at) => at !== index) : list));
|
|
|
+ setHr((current) => {
|
|
|
+ const removed = employees[index]?.name.trim();
|
|
|
+ return removed && current === removed ? "" : current;
|
|
|
+ });
|
|
|
+ };
|
|
|
+ const submit = async (): Promise<void> => {
|
|
|
+ if (!ready) return;
|
|
|
+ setSubmitting(true);
|
|
|
+ try {
|
|
|
+ await onFound({
|
|
|
+ name: name.trim(),
|
|
|
+ me: me.trim(),
|
|
|
+ employees: employees.map((item) => ({
|
|
|
+ name: item.name.trim(),
|
|
|
+ title: item.title.trim(),
|
|
|
+ rolePrompt: item.rolePrompt.trim(),
|
|
|
+ })),
|
|
|
+ ...(hr ? { hr } : {}),
|
|
|
+ });
|
|
|
+ } finally {
|
|
|
+ setSubmitting(false);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div
|
|
|
+ className="flex w-full flex-col gap-4 border-2 border-border bg-card p-5"
|
|
|
+ data-testid="company-found"
|
|
|
+ >
|
|
|
+ <div>
|
|
|
+ <h2 className="text-ui-lg font-semibold text-foreground">
|
|
|
+ {intl.formatMessage({ id: "company.found.title" })}
|
|
|
+ </h2>
|
|
|
+ <p className="mt-1 text-ui-sm text-foreground-subtle">
|
|
|
+ {intl.formatMessage({ id: "company.found.hint" })}
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
|
|
|
+ <label className="flex flex-col gap-1 text-ui-xs text-foreground">
|
|
|
+ {intl.formatMessage({ id: "company.found.name" })}
|
|
|
+ <input
|
|
|
+ value={name}
|
|
|
+ data-testid="company-found-name"
|
|
|
+ className={INPUT_CLASS}
|
|
|
+ placeholder={intl.formatMessage({ id: "company.found.name.placeholder" })}
|
|
|
+ onChange={(event) => setName(event.target.value)}
|
|
|
+ />
|
|
|
+ </label>
|
|
|
+ <label className="flex flex-col gap-1 text-ui-xs text-foreground">
|
|
|
+ {intl.formatMessage({ id: "company.found.me" })}
|
|
|
+ <input
|
|
|
+ value={me}
|
|
|
+ data-testid="company-found-me"
|
|
|
+ className={INPUT_CLASS}
|
|
|
+ placeholder={intl.formatMessage({ id: "company.found.me.placeholder" })}
|
|
|
+ onChange={(event) => setMe(event.target.value)}
|
|
|
+ />
|
|
|
+ </label>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <fieldset className="flex flex-col gap-2">
|
|
|
+ <legend className="text-ui-sm font-semibold text-foreground">
|
|
|
+ {intl.formatMessage({ id: "company.found.employees" })}
|
|
|
+ </legend>
|
|
|
+ <ul className="flex flex-col gap-2">
|
|
|
+ {employees.map((employee, index) => (
|
|
|
+ <li
|
|
|
+ key={index}
|
|
|
+ className="flex flex-col gap-1.5 border-2 border-border bg-background p-2"
|
|
|
+ data-testid={`company-found-employee-${String(index)}`}
|
|
|
+ >
|
|
|
+ <div className="grid grid-cols-1 gap-1.5 sm:grid-cols-2">
|
|
|
+ <input
|
|
|
+ value={employee.name}
|
|
|
+ maxLength={NAME_CHARS}
|
|
|
+ data-testid={`company-found-employee-name-${String(index)}`}
|
|
|
+ aria-label={intl.formatMessage({ id: "company.found.employee.name" })}
|
|
|
+ className={INPUT_CLASS}
|
|
|
+ placeholder={intl.formatMessage({
|
|
|
+ id: "company.found.employee.name.placeholder",
|
|
|
+ })}
|
|
|
+ onChange={(event) => update(index, { name: event.target.value })}
|
|
|
+ />
|
|
|
+ <input
|
|
|
+ value={employee.title}
|
|
|
+ data-testid={`company-found-employee-title-${String(index)}`}
|
|
|
+ aria-label={intl.formatMessage({ id: "company.found.employee.title" })}
|
|
|
+ className={INPUT_CLASS}
|
|
|
+ placeholder={intl.formatMessage({
|
|
|
+ id: "company.found.employee.title.placeholder",
|
|
|
+ })}
|
|
|
+ onChange={(event) => update(index, { title: event.target.value })}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <textarea
|
|
|
+ value={employee.rolePrompt}
|
|
|
+ rows={2}
|
|
|
+ data-testid={`company-found-employee-role-${String(index)}`}
|
|
|
+ aria-label={intl.formatMessage({ id: "company.found.employee.role" })}
|
|
|
+ className={`${INPUT_CLASS} resize-none`}
|
|
|
+ placeholder={intl.formatMessage({ id: "company.found.employee.role.placeholder" })}
|
|
|
+ onChange={(event) => update(index, { rolePrompt: event.target.value })}
|
|
|
+ />
|
|
|
+ {employees.length > 1 ? (
|
|
|
+ <div>
|
|
|
+ <Button
|
|
|
+ type="button"
|
|
|
+ size="sm"
|
|
|
+ variant="outline"
|
|
|
+ className="self-start"
|
|
|
+ data-testid={`company-found-employee-remove-${String(index)}`}
|
|
|
+ onClick={() => remove(index)}
|
|
|
+ >
|
|
|
+ {intl.formatMessage({ id: "company.found.employee.remove" })}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ ) : null}
|
|
|
+ </li>
|
|
|
+ ))}
|
|
|
+ </ul>
|
|
|
+ <div>
|
|
|
+ <Button
|
|
|
+ type="button"
|
|
|
+ size="sm"
|
|
|
+ variant="outline"
|
|
|
+ data-testid="company-found-add"
|
|
|
+ onClick={() => setEmployees((list) => [...list, { ...EMPTY_EMPLOYEE }])}
|
|
|
+ >
|
|
|
+ {intl.formatMessage({ id: "company.found.add" })}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ </fieldset>
|
|
|
+
|
|
|
+ <div className="flex flex-col gap-1">
|
|
|
+ <label className="flex items-center gap-2 text-ui-xs text-foreground">
|
|
|
+ {intl.formatMessage({ id: "company.found.hr" })}
|
|
|
+ <select
|
|
|
+ value={hr}
|
|
|
+ data-testid="company-found-hr"
|
|
|
+ className="min-w-0 border-2 border-border bg-background px-1 py-0.5 text-ui-sm"
|
|
|
+ onChange={(event) => setHr(event.target.value)}
|
|
|
+ >
|
|
|
+ <option value="">{intl.formatMessage({ id: "company.found.hr.none" })}</option>
|
|
|
+ {named.map((item) => (
|
|
|
+ <option key={item.name.trim()} value={item.name.trim()}>
|
|
|
+ {item.name.trim()}
|
|
|
+ </option>
|
|
|
+ ))}
|
|
|
+ </select>
|
|
|
+ </label>
|
|
|
+ <span className="text-ui-xs text-foreground-subtle">
|
|
|
+ {intl.formatMessage({ id: "company.found.hr.hint" })}
|
|
|
+ </span>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {error ? (
|
|
|
+ <p role="alert" data-testid="company-found-error" className="text-ui-sm text-destructive">
|
|
|
+ {error}
|
|
|
+ </p>
|
|
|
+ ) : null}
|
|
|
+ <div>
|
|
|
+ <Button
|
|
|
+ type="button"
|
|
|
+ data-testid="company-found-submit"
|
|
|
+ disabled={!ready || submitting}
|
|
|
+ onClick={() => void submit()}
|
|
|
+ >
|
|
|
+ {intl.formatMessage({
|
|
|
+ id: submitting ? "company.found.working" : "company.found.submit",
|
|
|
+ })}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|