forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

user-table.tsx175 lines, 5.6 KiBHistoryRawDownload
1import { createMemo, createSignal, Show, type JSX } from "solid-js";
2import { useNavigate } from "@tanstack/solid-router";
3import type { ColumnDef } from "@tanstack/solid-table";
4import type { Role, User } from "@/api/client";
5import type { MessageKey } from "@/i18n/messages";
6import { Select } from "@/components/ui/select";
7import { Button } from "@/components/ui/button";
8import { ConfirmDialog } from "@/components/ui/confirm-dialog";
9import { DataTable } from "@/components/ui/data-table";
10import { TableRowActions } from "@/components/ui/table-row-actions";
11import { IconCheck, IconExternalLink, IconEye, IconUsers } from "@/components/ui/icons";
12import { ROLES } from "@/lib/roles";
13import { matchesSearch } from "@/lib/search-text";
14import { useT } from "@/stores/preferences-context";
15
16function displayName(user: User): string {
17 return [user.name, user.surname].filter(Boolean).join(" ") || "—";
18}
19
20function UserRoleActions(props: {
21 user: User;
22 currentUserId: string;
23 onRoleChange: (userId: string, role: Role) => Promise<void>;
24 onParentClick?: (user: User) => void;
25}) {
26 const t = useT();
27 const isSelf = () => props.user.id === props.currentUserId;
28 const [pendingRole, setPendingRole] = createSignal<Role>(props.user.role);
29 const [confirmOpen, setConfirmOpen] = createSignal(false);
30 const dirty = () => pendingRole() !== props.user.role;
31
32 return (
33 <>
34 <Select
35 class="h-9 rounded-md text-xs"
36 value={pendingRole()}
37 disabled={isSelf()}
38 onChange={(e) => setPendingRole(e.currentTarget.value as Role)}
39 aria-label={t("admin.role")}
40 >
41 {ROLES.map((r) => (
42 <option value={r}>{t(`role.${r}` as MessageKey)}</option>
43 ))}
44 </Select>
45 <Show when={!isSelf() && dirty()}>
46 <Button type="button" size="sm" class="mt-2" onClick={(e) => { e.stopPropagation(); setConfirmOpen(true); }}>
47 <IconCheck />
48 {t("common.update")}
49 </Button>
50 </Show>
51 <ConfirmDialog
52 open={confirmOpen()}
53 onOpenChange={setConfirmOpen}
54 title={t("confirm.updateTitle")}
55 summary={t("confirm.updateRole", {
56 user: props.user.username,
57 from: t(`role.${props.user.role}` as MessageKey),
58 to: t(`role.${pendingRole()}` as MessageKey),
59 })}
60 onConfirm={async () => {
61 await props.onRoleChange(props.user.id, pendingRole());
62 }}
63 />
64 </>
65 );
66}
67
68export function UserTable(props: {
69 users: User[];
70 currentUserId: string;
71 onRoleChange: (userId: string, role: Role) => Promise<void>;
72 onUserClick?: (user: User) => void;
73 onParentClick?: (user: User) => void;
74 title?: string;
75 description?: string;
76 actions?: JSX.Element;
77}) {
78 const t = useT();
79 const navigate = useNavigate();
80 const searchUser = (user: User, query: string) =>
81 matchesSearch(query, user.username, user.phone, displayName(user), user.email, t(`role.${user.role}` as MessageKey));
82 const columns = createMemo<ColumnDef<User>[]>(() => [
83 {
84 id: "name",
85 accessorFn: displayName,
86 header: t("profile.name"),
87 size: 220,
88 minSize: 140,
89 meta: { cellClass: "truncate font-medium" },
90 cell: (cell) => displayName(cell.row.original),
91 },
92 {
93 id: "phone",
94 header: t("profile.phone"),
95 size: 180,
96 minSize: 130,
97 meta: { cellClass: "truncate" },
98 cell: (cell) => cell.row.original.phone || "—",
99 },
100 {
101 accessorKey: "email",
102 header: t("profile.email"),
103 size: 240,
104 minSize: 180,
105 meta: { cellClass: "truncate text-muted-foreground" },
106 cell: (cell) => cell.row.original.email || "—",
107 },
108 {
109 id: "update",
110 header: t("admin.role"),
111 size: 208,
112 minSize: 208,
113 enableHiding: false,
114 meta: { headerClass: "w-52", cellClass: "w-52" },
115 cell: (cell) => (
116 <UserRoleActions user={cell.row.original} currentUserId={props.currentUserId} onRoleChange={props.onRoleChange} onParentClick={props.onParentClick} />
117 ),
118 },
119 {
120 id: "actions",
121 header: t("common.actions"),
122 size: 110,
123 minSize: 110,
124 enableHiding: false,
125 meta: {
126 headerClass: "w-[110px] min-w-[110px] max-w-[110px] text-center whitespace-nowrap",
127 cellClass: "w-[110px] min-w-[110px] max-w-[110px] text-center whitespace-nowrap",
128 },
129 cell: (cell) => (
130 <TableRowActions
131 label={t("common.actions")}
132 actions={[
133 {
134 label: t("common.view"),
135 icon: <IconEye class="h-4 w-4" />,
136 onSelect: () => props.onUserClick?.(cell.row.original),
137 },
138 {
139 label: t("profile.viewProfile"),
140 icon: <IconExternalLink class="h-4 w-4" />,
141 onSelect: () => void navigate({ to: "/profile/$userId", params: { userId: cell.row.original.id } }),
142 },
143 ...(cell.row.original.role === "parent"
144 ? [
145 {
146 label: t("parentLink.manage"),
147 icon: <IconUsers class="h-4 w-4" />,
148 onSelect: () => props.onParentClick?.(cell.row.original),
149 },
150 ]
151 : []),
152 ]}
153 />
154 ),
155 },
156 ]);
157
158 return (
159 <DataTable
160 title={props.title ?? t("nav.users")}
161 description={props.description}
162 actions={props.actions}
163 columns={columns()}
164 data={props.users}
165 empty={t("admin.noUsers")}
166 storageKey="admin-users"
167 searchPredicate={searchUser}
168 filterHint={t("search.hint.users")}
169 surfaceSections
170 enablePagination
171 pageSize={20}
172 onRowClick={props.onUserClick}
173 />
174 );
175}