import { createMemo, createSignal, Show, type JSX } from "solid-js"; import { useNavigate } from "@tanstack/solid-router"; import type { ColumnDef } from "@tanstack/solid-table"; import type { Role, User } from "@/api/client"; import type { MessageKey } from "@/i18n/messages"; import { Select } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DataTable } from "@/components/ui/data-table"; import { TableRowActions } from "@/components/ui/table-row-actions"; import { IconCheck, IconEdit, IconExternalLink, IconEye, IconUsers } from "@/components/ui/icons"; import { ROLES } from "@/lib/roles"; import { matchesSearch } from "@/lib/search-text"; import { useT } from "@/stores/preferences-context"; function displayName(user: User): string { return [user.name, user.surname].filter(Boolean).join(" ") || user.username; } function UserRoleActions(props: { user: User; currentUserId: string; onRoleChange: (userId: string, role: Role) => Promise; onParentClick?: (user: User) => void; }) { const t = useT(); const isSelf = () => props.user.id === props.currentUserId; const [pendingRole, setPendingRole] = createSignal(props.user.role); const [confirmOpen, setConfirmOpen] = createSignal(false); const dirty = () => pendingRole() !== props.user.role; const demotesAdmin = () => props.user.role === "admin" && pendingRole() !== "admin"; return ( <> {/* The confirm button sits beside the select, not under it, so a row with a pending role change keeps the same height as the others. */}
{ await props.onRoleChange(props.user.id, pendingRole()); }} /> ); } export function UserTable(props: { users: User[]; currentUserId: string; onRoleChange: (userId: string, role: Role) => Promise; onUserClick?: (user: User) => void; onParentClick?: (user: User) => void; /** The detail page's "Düzenle" (profile edit), offered in the row menu when given. */ onEditClick?: (user: User) => void; title?: string; description?: string; actions?: JSX.Element; }) { const t = useT(); const navigate = useNavigate(); const searchUser = (user: User, query: string) => matchesSearch(query, user.username, user.phone, displayName(user), user.email, t(`role.${user.role}` as MessageKey)); const columns = createMemo[]>(() => [ { id: "name", accessorFn: displayName, header: t("profile.name"), size: 220, minSize: 140, meta: { cellClass: "truncate font-medium" }, cell: (cell) => displayName(cell.row.original), }, { id: "phone", accessorFn: (row) => row.phone || undefined, header: t("profile.phone"), size: 180, minSize: 130, meta: { cellClass: "truncate" }, cell: (cell) => cell.row.original.phone || "—", }, { accessorKey: "email", header: t("profile.email"), size: 240, minSize: 180, meta: { cellClass: "truncate text-muted-foreground" }, cell: (cell) => cell.row.original.email || "—", }, { id: "update", header: t("admin.role"), size: 208, minSize: 208, enableHiding: false, meta: { headerClass: "w-52", cellClass: "w-52" }, cell: (cell) => ( ), }, { id: "actions", header: t("common.actions"), size: 110, minSize: 110, enableHiding: false, meta: { headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", cellClass: "text-center", }, cell: (cell) => ( , onSelect: () => props.onUserClick?.(cell.row.original), }, ...(props.onEditClick ? [{ label: t("common.edit"), icon: , onSelect: () => props.onEditClick?.(cell.row.original) }] : []), { label: t("profile.viewProfile"), icon: , onSelect: () => void navigate({ to: "/profile/$userId", params: { userId: cell.row.original.id } }), }, ...(cell.row.original.role === "parent" ? [ { label: t("parentLink.manage"), icon: , onSelect: () => props.onParentClick?.(cell.row.original), }, ] : []), ]} /> ), }, ]); return ( ); }