mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
106 lines
3.9 KiB
React
106 lines
3.9 KiB
React
import { useMemo, useRef, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import { useUsers } from "@/contexts/AdminUserContext";
|
|
import DataTable from "@/components/generic/Table/DataTable";
|
|
import { ArchiveUserDialog } from "./ArchiveUserDialog";
|
|
import { FilterSheet } from "@/components/generic/Sheet/FilterSheet";
|
|
|
|
import { buildUserColumns, columnPinning } from "../config/columns.config";
|
|
import { buildToolbarActions } from "../config/toolbar.config";
|
|
import { buildSelectionActions } from "../config/selection.config";
|
|
import { buildRowActions } from "../config/rowActions.config";
|
|
|
|
import { getTimestamp } from "@/utils/timestamp.util";
|
|
|
|
export default function UsersTable() {
|
|
const [archiveTarget, setArchiveTarget] = useState(null); // single: row object
|
|
const [archiveIds, setArchiveIds] = useState(null); // bulk: array of ids
|
|
const tableRefsRef = useRef({ getFilters: () => [], getSort: () => [], resetSelection: () => {} });
|
|
const navigate = useNavigate();
|
|
|
|
const {
|
|
users,
|
|
attributes,
|
|
pagination,
|
|
setPagination,
|
|
loading,
|
|
fetchUsers,
|
|
fetchUserFieldValues,
|
|
} = useUsers();
|
|
|
|
// Shared export config — passed into toolbar + selection configs
|
|
const exportConfig = {
|
|
allData: users,
|
|
attributes,
|
|
filename: `${getTimestamp()}_Users`,
|
|
sheetName: "Users",
|
|
};
|
|
|
|
const rowActions = buildRowActions({ navigate, onArchive: (row) => setArchiveTarget(row) });
|
|
const toolbarActions = buildToolbarActions({
|
|
fetchUsers, pagination, exportConfig, navigate,
|
|
getFilters: () => tableRefsRef.current.getFilters(),
|
|
getSort: () => tableRefsRef.current.getSort(),
|
|
});
|
|
const selectionActions = buildSelectionActions({
|
|
exportConfig,
|
|
archiveUser: (row) => setArchiveTarget(row), // single
|
|
archiveUsers: (ids) => setArchiveIds(ids), // bulk
|
|
});
|
|
const columns = useMemo(() => buildUserColumns(attributes, rowActions), [attributes]);
|
|
|
|
const handleArchiveSuccess = () => {
|
|
setArchiveTarget(null);
|
|
setArchiveIds(null);
|
|
tableRefsRef.current.resetSelection?.(); // ← clear selection
|
|
fetchUsers({ page: 1, limit: pagination.limit });
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<DataTable
|
|
title="Users"
|
|
data={users}
|
|
columns={columns}
|
|
attributes={attributes}
|
|
pagination={pagination}
|
|
setPagination={setPagination}
|
|
loading={loading}
|
|
onFetch={fetchUsers}
|
|
onFetchFilterData={fetchUserFieldValues}
|
|
onRefsReady={(refs) => tableRefsRef.current = refs}
|
|
renderFilterSheet={({ open, onOpenChange, column, attr, data, loading }) => (
|
|
<FilterSheet
|
|
open={open}
|
|
onOpenChange={onOpenChange}
|
|
column={column}
|
|
attr={attr}
|
|
data={data}
|
|
loading={loading}
|
|
/>
|
|
)}
|
|
columnPinning={columnPinning}
|
|
toolbarActions={toolbarActions}
|
|
selectionActions={selectionActions}
|
|
recordLabel="user"
|
|
emptyMessage="No users match the current filters."
|
|
/>
|
|
{/* Single archive */}
|
|
<ArchiveUserDialog
|
|
open={!!archiveTarget}
|
|
onOpenChange={(v) => !v && setArchiveTarget(null)}
|
|
user={archiveTarget}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
|
|
{/* Bulk archive */}
|
|
<ArchiveUserDialog
|
|
open={!!archiveIds}
|
|
onOpenChange={(v) => !v && setArchiveIds(null)}
|
|
ids={archiveIds ?? []}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
</>
|
|
);
|
|
} |