Skip to documentation
Design system / Sources & standards
Current repository source
reviewer-category-assigner.tsx
src/app/(tenants)/console/[tenantCode]/admin/programs/[programCode]/editions/[editionCode]/review-rounds/components/reviewer-category-assigner.tsx
This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.
"use client";
import { useId, useRef, useLayoutEffect } from "react";
import { Plus, Minus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Text } from "@/components/ui/text";
import type { EditionCategoryOption } from "../reviewRoundsApi";
/** Route-owned controlled molecule. The behavior hook persists one replacement per intent. */
export function ReviewerCategoryAssigner({ categories, assigned, canEdit, disabled = false, onChange }: {
categories: EditionCategoryOption[]; assigned: string[]; canEdit: boolean; disabled?: boolean;
onChange: (keys: string[]) => void | Promise<void>;
}) {
const id = useId();
const root = useRef<HTMLDivElement>(null);
const heading = useRef<HTMLHeadingElement>(null);
const focusedKey = useRef<string | null>(null);
const choices = [...categories, ...assigned.filter((key) => !categories.some((category) => category.categoryKey === key))
.map((key) => ({ id: key, categoryKey: key, name: key }))];
const selected = new Set(assigned);
const available = choices.filter((category) => !selected.has(category.categoryKey));
const allocated = choices.filter((category) => selected.has(category.categoryKey));
const sections = [{ label: "Assigned categories", entries: allocated, active: true }, { label: "Available categories", entries: available, active: false }];
// Moving a focused button between columns must not lose keyboard position.
useLayoutEffect(() => {
if (!focusedKey.current || !root.current) return;
if (document.activeElement === document.body || root.current.contains(document.activeElement)) {
const target = [...root.current.querySelectorAll<HTMLButtonElement>("[data-category-key]")]
.find((button) => button.dataset.categoryKey === focusedKey.current);
if (target && !disabled) target.focus({ preventScroll: true });
else heading.current?.focus({ preventScroll: true });
}
if (!disabled) focusedKey.current = null;
}, [assigned, disabled]);
return <div ref={root} className="space-y-3" aria-labelledby={`${id}-heading`} aria-busy={disabled}>
<h3 ref={heading} tabIndex={-1} id={`${id}-heading`} className="text-supporting font-semibold">Assigned categories</h3>
<Text variant="inherit" className="text-xs text-text-muted">No explicit assignments means this reviewer can review all categories.</Text>
<div className="grid min-w-0 gap-3 @min-[28rem]:grid-cols-2">
{sections.map(({ label, entries, active }) =>
<section key={label} aria-label={label} className="min-w-0 space-y-2 rounded-md border p-3">
<Text variant="inherit" className="text-xs font-medium">{label}</Text>
<div className="flex flex-wrap gap-1.5">
{entries.map((category) => canEdit ? <Button key={category.categoryKey} type="button" size="xs" variant="outline"
className="h-auto min-h-6 max-w-full justify-start whitespace-normal py-1 text-left" disabled={disabled}
data-category-key={category.categoryKey} aria-label={`${active ? "Unassign" : "Assign"} ${category.name}`}
onClick={() => { focusedKey.current = category.categoryKey; void onChange(active ? assigned.filter((key) => key !== category.categoryKey) : [...assigned, category.categoryKey]); }}>
{active ? <Minus className="size-3 shrink-0 text-destructive" aria-hidden="true" /> : <Plus className="size-3 shrink-0 text-success" aria-hidden="true" />}
<span className="min-w-0 break-words [overflow-wrap:anywhere]">{category.name}</span>
</Button> : <Text key={category.categoryKey} variant="inherit" className="text-xs">{category.name}</Text>)}
{!entries.length && <Text variant="inherit" className="text-xs text-text-muted">{active ? "None assigned." : "None available."}</Text>}
</div>
{canEdit && <Button type="button" size="xs" variant="ghost" disabled={disabled || !entries.length}
onClick={() => { focusedKey.current = active ? "__unassign_all__" : "__assign_all__"; void onChange(active ? [] : [...new Set([...assigned, ...categories.map((category) => category.categoryKey)])]); }}>
{active ? "Unassign all" : "Assign all"}
</Button>}
</section>)}
</div>
</div>;
}
All sources and standards