forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

user-table.tsx198 lines, 6.9 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, IconEdit, 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(" ") || user.username;
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 const demotesAdmin = () => props.user.role === "admin" && pendingRole() !== "admin";
32
33 return (
34 <>
35 {/* The confirm button sits beside the select, not under it, so a row
36 with a pending role change keeps the same height as the others. */}
37 <div class="flex min-w-0 items-center gap-1.5">
38 <Select
39 wrapperClass="min-w-0 flex-1"
40 // 40px on phones and touch screens (touch-target minimum), 32px from sm.
41 class="h-10 rounded-md text-xs sm:h-8 touch:h-10"
42 value={pendingRole()}
43 disabled={isSelf()}
44 onChange={(e) => setPendingRole(e.currentTarget.value as Role)}
45 aria-label={t("admin.role")}
46 >
47 {ROLES.map((r) => (
48 <option value={r}>{t(`role.${r}` as MessageKey)}</option>
49 ))}
50 </Select>
51 <Show when={!isSelf() && dirty()}>
52 <Button
53 type="button"
54 size="icon"
55 class="h-10 w-10 shrink-0 rounded-md sm:h-8 sm:w-8 touch:h-10 touch:w-10"
56 aria-label={t("common.update")}
57 title={t("common.update")}
58 onClick={(e) => { e.stopPropagation(); setConfirmOpen(true); }}
59 >
60 <IconCheck />
61 </Button>
62 </Show>
63 </div>
64 <ConfirmDialog
65 open={confirmOpen()}
66 onOpenChange={setConfirmOpen}
67 title={t("confirm.updateTitle")}
68 description={demotesAdmin() ? t("admin.demoteAdminWarning") : undefined}
69 variant={demotesAdmin() ? "destructive" : "default"}
70 confirmLabel={t("confirm.confirmUpdate")}
71 summary={t("confirm.updateRole", {
72 user: props.user.username,
73 from: t(`role.${props.user.role}` as MessageKey),
74 to: t(`role.${pendingRole()}` as MessageKey),
75 })}
76 onConfirm={async () => {
77 await props.onRoleChange(props.user.id, pendingRole());
78 }}
79 />
80 </>
81 );
82}
83
84export function UserTable(props: {
85 users: User[];
86 currentUserId: string;
87 onRoleChange: (userId: string, role: Role) => Promise<void>;
88 onUserClick?: (user: User) => void;
89 onParentClick?: (user: User) => void;
90 /** The detail page's "Düzenle" (profile edit), offered in the row menu when given. */
91 onEditClick?: (user: User) => void;
92 title?: string;
93 description?: string;
94 actions?: JSX.Element;
95}) {
96 const t = useT();
97 const navigate = useNavigate();
98 const searchUser = (user: User, query: string) =>
99 matchesSearch(query, user.username, user.phone, displayName(user), user.email, t(`role.${user.role}` as MessageKey));
100 const columns = createMemo<ColumnDef<User>[]>(() => [
101 {
102 id: "name",
103 accessorFn: displayName,
104 header: t("profile.name"),
105 size: 220,
106 minSize: 140,
107 meta: { cellClass: "truncate font-medium" },
108 cell: (cell) => displayName(cell.row.original),
109 },
110 {
111 id: "phone",
112 accessorFn: (row) => row.phone || undefined,
113 header: t("profile.phone"),
114 size: 180,
115 minSize: 130,
116 meta: { cellClass: "truncate" },
117 cell: (cell) => cell.row.original.phone || "—",
118 },
119 {
120 accessorKey: "email",
121 header: t("profile.email"),
122 size: 240,
123 minSize: 180,
124 meta: { cellClass: "truncate text-muted-foreground" },
125 cell: (cell) => cell.row.original.email || "—",
126 },
127 {
128 id: "update",
129 header: t("admin.role"),
130 size: 208,
131 minSize: 208,
132 enableHiding: false,
133 meta: { headerClass: "w-52", cellClass: "w-52" },
134 cell: (cell) => (
135 <UserRoleActions user={cell.row.original} currentUserId={props.currentUserId} onRoleChange={props.onRoleChange} onParentClick={props.onParentClick} />
136 ),
137 },
138 {
139 id: "actions",
140 header: t("common.actions"),
141 size: 110,
142 minSize: 110,
143 enableHiding: false,
144 meta: {
145 headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap",
146 cellClass: "text-center",
147 },
148 cell: (cell) => (
149 <TableRowActions
150 label={t("common.actions")}
151 actions={[
152 {
153 label: t("common.view"),
154 icon: <IconEye class="h-4 w-4" />,
155 onSelect: () => props.onUserClick?.(cell.row.original),
156 },
157 ...(props.onEditClick
158 ? [{ label: t("common.edit"), icon: <IconEdit class="h-4 w-4" />, onSelect: () => props.onEditClick?.(cell.row.original) }]
159 : []),
160 {
161 label: t("profile.viewProfile"),
162 icon: <IconExternalLink class="h-4 w-4" />,
163 onSelect: () => void navigate({ to: "/profile/$userId", params: { userId: cell.row.original.id } }),
164 },
165 ...(cell.row.original.role === "parent"
166 ? [
167 {
168 label: t("parentLink.manage"),
169 icon: <IconUsers class="h-4 w-4" />,
170 onSelect: () => props.onParentClick?.(cell.row.original),
171 },
172 ]
173 : []),
174 ]}
175 />
176 ),
177 },
178 ]);
179
180 return (
181 <DataTable
182 title={props.title ?? t("nav.users")}
183 description={props.description}
184 actions={props.actions}
185 columns={columns()}
186 data={props.users}
187 empty={t("admin.noUsers")}
188 storageKey="admin-users"
189 searchPredicate={searchUser}
190 filterHint={t("search.hint.users")}
191 surfaceSections
192 enablePagination
193 // The shared default (10, halved on phones), like every other list.
194 urlState
195 onRowClick={props.onUserClick}
196 />
197 );
198}