import { Trans, useLingui } from "@lingui/react/macro"; import type { UserFact, UserFactCategory } from "@rakazo/contracts"; import { USER_FACT_CATEGORIES, USER_FACT_VALUE_MAX_LENGTH } from "@rakazo/contracts"; import { Button, DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuSeparator, DropdownMenuTrigger, Input, NativeSelect, NativeSelectOption, } from "@rakazo/ui-web"; import { ChevronDownIcon } from "lucide-react"; import { useState } from "react"; import type { UserFactDraft } from "../lib/user-profile-view"; import { factSpaceNames, toggleFactSpace, userFactDraft } from "../lib/user-profile-view"; /** Edits one fact in place: its wording, category and, with more than one space, where it shows. */ export function UserFactEditor({ fact, spaces, categoryLabels, busy, onSave, onCancel, }: { fact: UserFact; /** The user's spaces; the visibility control appears with more than one. */ spaces: ReadonlyArray<{ id: string; name: string }>; categoryLabels: Record; busy: boolean; onSave: (draft: UserFactDraft) => void; onCancel: () => void; }) { const { t } = useLingui(); const [draft, setDraft] = useState(() => userFactDraft(fact)); const allSpaces = t`All spaces`; const showSpaces = spaces.length > 1 || draft.spaceIds !== null; return (
  • { event.preventDefault(); if (draft.value.trim()) onSave(draft); }} onKeyDown={(event) => { // The spaces menu renders in a portal; its Escape only closes the menu. if (event.key === "Escape" && event.currentTarget.contains(event.target as Node)) { event.stopPropagation(); onCancel(); } }} > setDraft({ ...draft, value: event.target.value })} />
    setDraft({ ...draft, category: event.target.value as UserFactCategory }) } > {USER_FACT_CATEGORIES.map((category) => ( {categoryLabels[category]} ))} {showSpaces ? ( } > {factSpaceNames(draft.spaceIds, spaces).join(", ") || allSpaces} setDraft({ ...draft, spaceIds: null })} > {allSpaces} {spaces.map((space) => ( setDraft({ ...draft, spaceIds: toggleFactSpace(draft.spaceIds, space.id, checked), }) } > {space.name} ))} ) : null}
  • ); }