"use client"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Checkbox } from "@suphark/ui/components/ui/checkbox"; import { cn } from "@suphark/ui/lib/utils"; import { Fragment, type ReactNode, useMemo } from "react"; export interface ResourcePermissionAction { id: string; label: string; } export interface ResourcePermissionResource { id: string; label: ReactNode; /** ชื่อสำหรับ aria-label ของช่องติ๊ก (ค่าเริ่มต้น `label` ถ้าเป็นข้อความ, ไม่งั้น `id`) */ name?: string; description?: ReactNode; /** หัวข้อกลุ่ม — แถวที่ต่อกันและกลุ่มเดียวกันอยู่ใต้หัวข้อเดียว */ group?: string; /** action ที่ใช้ได้กับทรัพยากรนี้ (ไม่ส่ง = ทุก action) — ช่องที่ใช้ไม่ได้แสดงขีด */ actions?: ReadonlyArray; } export interface ResourcePermissionMatrixProps { resources: ReadonlyArray; actions: ReadonlyArray; /** id สิทธิ์ที่ได้ (รูปแบบจาก `getPermissionId`) */ value: ReadonlyArray; /** ไม่ส่ง = อ่านอย่างเดียว */ onChange?: (next: string[]) => void; /** id ของสิทธิ์จากคู่ (ทรัพยากร, action) — ค่าเริ่มต้น `":"` */ getPermissionId?: (resourceId: string, actionId: string) => string; /** ค่าที่บันทึกไว้ — ช่องที่ต่างจากนี้ถูกไฮไลต์ */ baseline?: ReadonlyArray; /** ชื่อ field ของฟอร์ม — input ซ่อนต่อช่องที่ติ๊ก */ name?: string; /** ความสูงสูงสุดของกรอบ (เลื่อนในกรอบ หัวตารางติดบน) — เช่น "28rem" · ไม่ส่ง = สูงตามเนื้อหา */ maxHeight?: string; className?: string; } const INDETERMINATE = "data-indeterminate:border-primary data-indeterminate:bg-primary data-indeterminate:text-primary-foreground data-indeterminate:before:absolute data-indeterminate:before:h-0.5 data-indeterminate:before:w-2 data-indeterminate:before:rounded-full data-indeterminate:before:bg-current [&[data-indeterminate]_svg]:hidden"; const defaultId = (resource: string, action: string) => `${resource}:${action}`; /** * สิทธิ์ของบทบาทเดียว: ทรัพยากร (แถว) × การกระทำ (คอลัมน์) — ติ๊กทีละช่อง, ทั้งแถว (ช่องหน้าชื่อ), ทั้งคอลัมน์ (ช่องในหัว) * หัวตารางติดบน + คอลัมน์ชื่อติดซ้ายเมื่อเลื่อน · ช่องที่ทรัพยากรไม่มี action นั้นแสดงขีด · ไฮไลต์ช่องที่แก้เทียบ `baseline` * (หลายบทบาท × สิทธิ์ → `PermissionMatrix`) * * */ export function ResourcePermissionMatrix({ resources, actions, value, onChange, getPermissionId = defaultId, baseline, name, maxHeight, className, }: ResourcePermissionMatrixProps) { const t = useUiText().permissionMatrix; const readOnly = !onChange; const set = useMemo(() => new Set(value), [value]); const baseSet = useMemo(() => (baseline ? new Set(baseline) : null), [baseline]); const cellIds = (resource: ResourcePermissionResource) => actions .filter((a) => !resource.actions || resource.actions.includes(a.id)) .map((a) => getPermissionId(resource.id, a.id)); const columnIds = (actionId: string) => resources .filter((r) => !r.actions || r.actions.includes(actionId)) .map((r) => getPermissionId(r.id, actionId)); const stateOf = (ids: ReadonlyArray) => { const on = ids.filter((id) => set.has(id)).length; return { checked: ids.length > 0 && on === ids.length, indeterminate: on > 0 && on < ids.length, }; }; const setMany = (ids: ReadonlyArray, on: boolean) => { if (!onChange) return; const next = new Set(set); for (const id of ids) { if (on) next.add(id); else next.delete(id); } // คงลำดับตามตาราง (diff อ่านง่าย) — id อื่นที่ไม่อยู่ในตารางต่อท้ายไว้ไม่ทิ้ง const ordered = resources.flatMap(cellIds).filter((id) => next.has(id)); const extra = [...next].filter((id) => !ordered.includes(id)); onChange([...ordered, ...extra]); }; const nameOf = (r: ResourcePermissionResource) => r.name ?? (typeof r.label === "string" ? r.label : r.id); const allRow = t?.allRow ?? "ทุกการกระทำของ {name}"; const allColumn = t?.allColumn ?? "{name} ทุกรายการ"; let lastGroup: string | undefined; return (
{actions.map((action) => { const ids = columnIds(action.id); const s = stateOf(ids); return ( ); })} {resources.map((resource) => { const ids = cellIds(resource); const s = stateOf(ids); const groupRow = resource.group && resource.group !== lastGroup; lastGroup = resource.group; return ( {groupRow && ( )} {actions.map((action) => { const available = !resource.actions || resource.actions.includes(action.id); const id = getPermissionId(resource.id, action.id); const on = set.has(id); const changed = baseSet ? baseSet.has(id) !== on : false; return ( ); })} ); })}
{t?.resource ?? "รายการ"} {action.label} {!readOnly && ids.length > 0 && ( setMany(ids, on === true)} /> )}
{resource.group}
{!readOnly && ids.length > 0 && ( setMany(ids, on === true)} /> )} {resource.label} {resource.description && ( {resource.description} )} {available ? ( <> setMany([id], next === true)} /> {name && on && } ) : ( <> {t?.notApplicable ?? "ไม่มี"} )}
); }