Files

327 lines
15 KiB
TypeScript

// AK-P4b Interim-UI (AD-28) — Task detail preview screen composed from ~/ui.
// Presentational: renders the unwrapped TaskDetailData; mutations stay server-driven
// (<form method="post"> + intent, exactly like the baseline route action). The delete
// confirmation is a real ~/ui Dialog (David standard), not a Card fake. No
// useLoaderData / server imports. Final Subframe optics land as a design-nachzug.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherArchive, FeatherCheck, FeatherClipboard, FeatherSave, FeatherTrash2 } from "@subframe/core";
import type { TaskDetailData, TaskPriority, TaskStatus, TaskWatcherView } from "@hivegrid/view-models";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { Button } from "~/ui/components/Button";
import { Card } from "~/ui/components/Card";
import { Dialog } from "~/ui/components/Dialog";
import { IconButton } from "~/ui/components/IconButton";
import { Select } from "~/ui/components/Select";
import { TextArea } from "~/ui/components/TextArea";
import { TextInput } from "~/ui/components/TextInput";
import { DateField } from "./DateField";
import { APP_TIME_ZONE } from "~/locale/format";
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
const STATUSES: readonly TaskStatus[] = ["open", "in_progress", "blocked", "done", "cancelled"];
const PRIORITIES: readonly TaskPriority[] = ["low", "normal", "high", "urgent"];
/** The one edit form; the header's save button targets it via `form=`. */
const TASK_EDIT_FORM_ID = "task-edit-form";
/** Sentinel for "do not add a watcher" — an empty Select value is not allowed. */
export const NO_WATCHER = "__none__";
function watcherRemoveFormId(userId: string): string {
return `task-watcher-remove-${userId}`;
}
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
export type TaskDetailPageProps = {
data: TaskDetailData;
};
type BadgeFarbe = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-neutral" | "subtle-information";
function priorityFarbe(priority: TaskPriority): BadgeFarbe {
switch (priority) {
case "urgent":
return "subtle-negative";
case "high":
return "subtle-warning";
case "normal":
return "subtle-information";
case "low":
default:
return "subtle-neutral";
}
}
function statusFarbe(status: TaskStatus): BadgeFarbe {
switch (status) {
case "done":
return "subtle-positive";
case "blocked":
return "subtle-negative";
case "in_progress":
return "subtle-warning";
case "cancelled":
case "open":
default:
return "subtle-neutral";
}
}
function MetaRow({ label, value }: { label: string; value: string }) {
return (
<div className="flex flex-col gap-1">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{label}</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{value}</span>
</div>
);
}
function WatcherList({
watchers,
canWrite,
removeLabel,
emptyLabel,
}: {
watchers: readonly TaskWatcherView[];
canWrite: boolean;
removeLabel: string;
emptyLabel: string;
}) {
if (watchers.length === 0) {
return <span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{emptyLabel}</span>;
}
return (
<ul className="flex w-full flex-col gap-2">
{watchers.map((watcher) => (
<li
key={watcher.userId}
className="flex items-center justify-between gap-3 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-3 py-2"
data-watcher-id={watcher.userId}
>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{watcher.label}</span>
{canWrite ? (
<IconButton
type="submit"
form={watcherRemoveFormId(watcher.userId)}
variant="ghost"
size="small"
icon={<FeatherTrash2 />}
aria-label={removeLabel}
/>
) : null}
</li>
))}
</ul>
);
}
export function TaskDetailPage({ data }: TaskDetailPageProps) {
const { t, i18n } = useTranslation();
const [deleteOpen, setDeleteOpen] = useState(false);
const dateFormatter = new Intl.DateTimeFormat(i18n.language, { timeZone: APP_TIME_ZONE, dateStyle: "medium" });
const deadlineLabel = data.deadlineIso ? dateFormatter.format(new Date(data.deadlineIso)) : "—";
return (
// Same page chrome as the lead detail: full-bleed header bar carrying icon,
// title, status/priority badges and the action buttons, content below in
// PageBody. Before this the screen started with a bare heading in a padded
// div, which made it the only detail view without the app's header bar.
<PageShell uiId="ui.tasks.detail" className="bg-surface-neutral-primary">
<PageHeader
icon={<FeatherClipboard className={PAGE_HEADER_ICON_CLASS} />}
title={
<span className="inline-flex flex-wrap items-center gap-2">
{data.title}
<BadgeFeedback farbe={statusFarbe(data.status)} size="small">
{t(data.statusKey)}
</BadgeFeedback>
<BadgeFeedback farbe={priorityFarbe(data.priority)} size="small">
{t(data.priorityKey)}
</BadgeFeedback>
</span>
}
subtitle={t(data.sourceKey)}
actions={
// Order: complete, archive, delete, save. "Erledigen" is the positive
// outcome and gets the green variant; archive and save are ordinary
// primary actions; delete keeps the destructive red. Save lives here
// rather than at the bottom of the form: it is the single write for
// every field below, reached via the `form` attribute.
<span className="flex flex-wrap items-center gap-2" data-ui-id="ui.tasks.detail.actions">
{data.canWrite && data.status !== "done" ? (
<form method="post">
<Button type="submit" name="intent" value="complete" variant="success" icon={<FeatherCheck />} data-ui-id="ui.tasks.detail.complete">
{t("tasks.detail.actions.complete")}
</Button>
</form>
) : null}
{data.canWrite ? (
<form method="post">
<Button type="submit" name="intent" value="archive" icon={<FeatherArchive />} data-ui-id="ui.tasks.detail.archive">
{t("tasks.detail.actions.archive")}
</Button>
</form>
) : null}
{data.canDelete ? (
<Button type="button" variant="destructive" icon={<FeatherTrash2 />} onClick={() => setDeleteOpen(true)} data-ui-id="ui.tasks.detail.delete">
{t("tasks.detail.actions.delete")}
</Button>
) : null}
{data.canWrite ? (
<Button type="submit" form={TASK_EDIT_FORM_ID} icon={<FeatherSave />} data-ui-id="ui.tasks.detail.save">
{t("tasks.detail.actions.save")}
</Button>
) : null}
</span>
}
/>
<PageBody>
{/* Metadata */}
<Card>
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t("tasks.detail.section.overview")}</span>
<div className="grid w-full grid-cols-1 gap-4 sm:grid-cols-2">
<div data-ui-id="ui.tasks.detail.deadline" data-overdue={data.isOverdue}>
<MetaRow label={t("tasks.detail.field.deadline")} value={deadlineLabel} />
</div>
<MetaRow label={t("tasks.detail.field.assignee")} value={data.assignee ? data.assignee.label : t("tasks.detail.unassigned")} />
{data.linkedEntity ? (
<div data-ui-id="ui.tasks.detail.linkedEntity">
<MetaRow label={t(data.linkedEntity.labelKey)} value={data.linkedEntity.entityId} />
</div>
) : null}
<MetaRow label={t("tasks.detail.field.created")} value={dateFormatter.format(new Date(data.createdAtIso))} />
</div>
{data.description ? (
<div className="flex flex-col gap-1">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("tasks.detail.section.description")}</span>
<span className="whitespace-pre-wrap text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{data.description}</span>
</div>
) : null}
</Card>
{/* Edit card (writers only): master data, assignment and watchers all in
one tile, all written by the single save button in the header. Every
control spans its grid cell so the tile's width is actually used. */}
{data.canWrite ? (
<Card>
<span className={sectionHeading}>{t("tasks.detail.section.edit")}</span>
<form id={TASK_EDIT_FORM_ID} method="post" className="flex w-full flex-col gap-4" data-ui-id="ui.tasks.detail.edit">
<input type="hidden" name="intent" value="update" />
<TextInput className="max-w-none" label={t("tasks.detail.field.title")} showLabel>
<TextInput.Input name="title" defaultValue={data.title} required />
</TextInput>
<TextArea className="max-w-none" label={t("tasks.detail.field.description")} showLabel>
<TextArea.Input name="description" defaultValue={data.description ?? ""} />
</TextArea>
<div className="grid w-full gap-4 sm:grid-cols-3">
<Select className="max-w-none" name="status" label={t("tasks.detail.field.status")} showLabel defaultValue={data.status}>
{STATUSES.map((status) => (
<Select.Item key={status} value={status}>
{t(`tasks.status.${status}`)}
</Select.Item>
))}
</Select>
<Select className="max-w-none" name="priority" label={t("tasks.detail.field.priority")} showLabel defaultValue={data.priority}>
{PRIORITIES.map((priority) => (
<Select.Item key={priority} value={priority}>
{t(`tasks.priority.${priority}`)}
</Select.Item>
))}
</Select>
<DateField label={t("tasks.detail.field.deadline")} name="deadline" defaultValue={data.deadlineIso ? data.deadlineIso.slice(0, 10) : ""} />
</div>
{/* --- Assignment --- */}
<span className={sectionHeading}>{t("tasks.detail.section.assign")}</span>
<div className="grid w-full gap-4 sm:grid-cols-2" data-ui-id="ui.tasks.detail.reassign">
<Select className="max-w-none" name="assigneeType" label={t("tasks.detail.field.assigneeType")} showLabel defaultValue="user">
<Select.Item value="user">{t("tasks.detail.assignee.user")}</Select.Item>
<Select.Item value="group">{t("tasks.detail.assignee.group")}</Select.Item>
</Select>
<Select
className="max-w-none"
name="assigneeId"
label={t("tasks.detail.field.assignee")}
showLabel
defaultValue={data.assignableUsers[0]?.id ?? data.assignableGroups[0]?.id}
>
{data.assignableUsers.map((user) => (
<Select.Item key={`u-${user.id}`} value={user.id}>
{user.label}
</Select.Item>
))}
{data.assignableGroups.map((group) => (
<Select.Item key={`g-${group.id}`} value={group.id}>
{group.label}
</Select.Item>
))}
</Select>
</div>
{/* --- Watchers: adding one is part of the same save; removing is
not (it is a delete), so each remove button submits its own tiny
form declared after this one via the `form` attribute. --- */}
<span className={sectionHeading}>{t("tasks.detail.section.watchers")}</span>
<div className="flex w-full flex-col gap-3" data-ui-id="ui.tasks.detail.watchers">
<WatcherList watchers={data.watchers} canWrite removeLabel={t("tasks.detail.actions.removeWatcher")} emptyLabel={t("tasks.detail.watchers.empty")} />
{data.assignableUsers.length > 0 ? (
<Select className="max-w-none" name="watcherUserId" label={t("tasks.detail.field.watcher")} showLabel defaultValue={NO_WATCHER}>
<Select.Item value={NO_WATCHER}>{t("tasks.detail.watchers.none")}</Select.Item>
{data.assignableUsers.map((user) => (
<Select.Item key={`w-${user.id}`} value={user.id}>
{user.label}
</Select.Item>
))}
</Select>
) : null}
</div>
</form>
{/* Remove-watcher forms live outside the edit form — nesting forms is
invalid HTML, so the buttons above associate via `form=`. */}
{data.watchers.map((watcher) => (
<form key={`rm-${watcher.userId}`} id={watcherRemoveFormId(watcher.userId)} method="post" className="hidden">
<input type="hidden" name="intent" value="removeWatcher" />
<input type="hidden" name="userId" value={watcher.userId} />
</form>
))}
</Card>
) : (
<Card>
<span className={sectionHeading}>{t("tasks.detail.section.watchers")}</span>
<div className="flex w-full flex-col gap-3" data-ui-id="ui.tasks.detail.watchers">
<WatcherList watchers={data.watchers} canWrite={false} removeLabel="" emptyLabel={t("tasks.detail.watchers.empty")} />
</div>
</Card>
)}
{/* Delete confirmation dialog (real ~/ui Dialog, size XS) */}
<Dialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<Dialog.Content className="w-[420px] max-w-[90vw] p-8">
<div className="flex w-full flex-col gap-6">
<div className="flex flex-col gap-2">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("tasks.detail.delete.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("tasks.detail.delete.body")}</span>
</div>
<div className="flex items-center justify-end gap-2">
<Button type="button" variant="tertiary" onClick={() => setDeleteOpen(false)}>
{t("tasks.detail.dialog.cancel")}
</Button>
<form method="post">
<Button type="submit" name="intent" value="delete" variant="destructive" icon={<FeatherTrash2 />}>
{t("tasks.detail.delete.confirm")}
</Button>
</form>
</div>
</div>
</Dialog.Content>
</Dialog>
</PageBody>
</PageShell>
);
}