import { useCallback, useEffect, useRef, useState } from "react"; import type { ReactNode } from "react"; import { ConfirmDialog } from "../../ui/ConfirmDialog"; import { Busy } from "../../ui/Busy"; import { ActionsError, completeAction, deleteAction, listActions, subscribeActions } from "./actionsApi"; import type { ActionGrain, ActionPage, ActionStatus, SkuAction } from "./actionsApi"; import "../../styles/sdlc81/sku-actions.css"; export interface ActionListProps { appId: string; canEdit?: boolean; canRead?: boolean; grain?: ActionGrain; entityKey?: string; pageSize?: number; onOpenEntity?: (action: SkuAction) => void; onSelectCompleted?: (action: SkuAction) => void; onPermissionsChange?: (canEdit: boolean) => void; toolbar?: ReactNode; children?: ReactNode; } type Load = { phase: "loading" } | { phase: "error"; error: unknown } | { phase: "ready"; page: ActionPage }; const STATUSES: [ActionStatus, string][] = [["open", "Open"], ["due", "Due"], ["overdue", "Overdue"], ["done", "Completed"]]; export function actionDate(value: string | null): string { return value ? new Date(`${value}T00:00:00Z`).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric", timeZone: "UTC" }) : "Not set"; } export function ActionList(props: ActionListProps) { return ; } function ActionListPage({ appId, canEdit = false, canRead = true, grain, entityKey, pageSize = 20, onOpenEntity, onSelectCompleted, onPermissionsChange, toolbar, children }: ActionListProps) { const [status, setStatus] = useState("open"); const [cursors, setCursors] = useState<(string | null)[]>([null]); const [attempt, setAttempt] = useState(0); const [load, setLoad] = useState<{ key: string; value: Load }>({ key: "", value: { phase: "loading" } }); const [busy, setBusy] = useState(null); const [error, setError] = useState(""); const [deleting, setDeleting] = useState(null); const mutation = useRef(false); const permissions = useRef(onPermissionsChange); permissions.current = onPermissionsChange; const cursor = cursors.at(-1); const requestKey = JSON.stringify([appId, grain, entityKey, status, cursor, pageSize, attempt]); const state = load.key === requestKey ? load.value : { phase: "loading" } as Load; const editable = canEdit && state.phase === "ready" && state.page.canEdit; const reload = useCallback(() => { setCursors([null]); setAttempt(n => n + 1); }, []); useEffect(() => subscribeActions(appId, reload), [appId, reload]); useEffect(() => { permissions.current?.(false); if (!canRead) return; const controller = new AbortController(); void listActions({ appId, grain, entityKey, status, cursor, limit: pageSize }, controller.signal) .then(page => { if (controller.signal.aborted) return; setLoad({ key: requestKey, value: { phase: "ready", page } }); permissions.current?.(page.canEdit); }).catch(reason => { if (!controller.signal.aborted) setLoad({ key: requestKey, value: { phase: "error", error: reason } }); }); return () => controller.abort(); }, [appId, grain, entityKey, status, cursor, pageSize, attempt, requestKey, canRead]); const change = async (row: SkuAction, remove: boolean) => { if (!editable || mutation.current) return; mutation.current = true; setBusy(row.id); setError(""); try { if (remove) await deleteAction(appId, row.id); else await completeAction(appId, row.id); setDeleting(null); } catch (reason) { if (reason instanceof ActionsError && [401, 403, 404].includes(reason.status)) reload(); setError(reason instanceof ActionsError && reason.status === 403 ? "You can view actions but cannot change them" : remove ? "Could not delete this action" : "Could not complete this action"); setDeleting(null); } finally { mutation.current = false; setBusy(null); } }; return

Actions

{toolbar}
{children} {!canRead ?

You do not have access to these actions

: state.phase === "loading" ?
: state.phase === "error" ?

{state.error instanceof ActionsError ? state.error.message : "Could not load actions"}{" "}

: <> {!editable &&

View only

} {state.page.records.length === 0 ?

{status === "done" ? "No completed actions" : `No ${status} actions`}

:
    {state.page.records.map(row =>
  • {row.title} {row.status === "done" ? "Completed" : STATUSES.find(([value]) => value === status)?.[1]}
    {entityKey === undefined && (onOpenEntity ? : {row.key})}
    Owner
    {row.owner || "Unassigned"}
    Start date
    {actionDate(row.start_on)}
    Deadline
    {actionDate(row.deadline_on)}
    {row.completed_on &&
    Completed
    {actionDate(row.completed_on)}
    }
    {row.status === "done" && onSelectCompleted && } {editable &&
    {row.status !== "done" && }
    }
  • )}
} } {error &&

{error}

} setDeleting(null)} onConfirm={() => { if (deleting) void change(deleting, true); }} />
; }