git-subtree-dir: apps/web git-subtree-mainline:eaa6d2276agit-subtree-split:459af9d46a
148 lines
5.1 KiB
React
148 lines
5.1 KiB
React
// modules/admin/components/assets/AssetsTable.jsx
|
|
|
|
import { useMemo, useRef, useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import { useAssets } from "@/contexts/AdminAssetsContext";
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
|
|
import DataTable from "@/components/generic/Table/DataTable";
|
|
import { FilterSheet } from "@/components/generic/Sheet/FilterSheet";
|
|
import { ArchiveDialog } from "@/components/generic/Dialogs/ArchiveDialog";
|
|
|
|
import { buildDataColumns, columnPinning } from "../../config/assets/columns.config";
|
|
import { buildToolbarActions } from "../../config/assets/toolbar.config";
|
|
import { buildSelectionActions } from "../../config/assets/selection.config";
|
|
import { buildRowActions } from "../../config/assets/rowActions.config";
|
|
|
|
import { getTimestamp } from "@/utils/timestamp.util";
|
|
import { formatGeneratedBy } from "@/utils/generatedBy.util";
|
|
|
|
export default function AssetsTable() {
|
|
const [archiveTarget, setArchiveTarget] = useState(null);
|
|
const [archiveIds, setArchiveIds] = useState(null);
|
|
|
|
const tableRefsRef = useRef({
|
|
getFilters: () => [],
|
|
getSort: () => [],
|
|
resetSelection: () => { },
|
|
setFilters: () => { },
|
|
});
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const { user: currentUser } = useAuth();
|
|
|
|
const {
|
|
assets, attributes, pagination, setPagination, loading,
|
|
fetchAssets, archiveAsset, archiveAssets, fetchAssetFieldValues
|
|
} = useAssets();
|
|
|
|
const handleRefsReady = (refs) => {
|
|
tableRefsRef.current = refs;
|
|
};
|
|
|
|
const exportConfig = {
|
|
allData: assets,
|
|
attributes,
|
|
filename: `${getTimestamp()}_Files`,
|
|
sheetName: "Files",
|
|
generatedBy: formatGeneratedBy(currentUser),
|
|
};
|
|
|
|
const resolveViewPath = (row) => {
|
|
switch (row.file_type) {
|
|
case "video": return `view/video/${row.asset_id}`;
|
|
case "image": return `view/image/${row.asset_id}`;
|
|
case "document": return `view/document/${row.asset_id}`;
|
|
case "audio": return `view/audio/${row.asset_id}`;
|
|
default: return `view/image/${row.asset_id}`;
|
|
}
|
|
};
|
|
|
|
const rowActions = buildRowActions({
|
|
onView: (row) => navigate(resolveViewPath(row)),
|
|
onArchive: (row) => setArchiveTarget(row),
|
|
});
|
|
|
|
const toolbarActions = buildToolbarActions({
|
|
fetchAssets, pagination, exportConfig, navigate,
|
|
getFilters: () => tableRefsRef.current.getFilters(),
|
|
getSort: () => tableRefsRef.current.getSort(),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
|
|
const selectionActions = buildSelectionActions({
|
|
exportConfig,
|
|
onArchive: (row) => setArchiveTarget(row),
|
|
onArchiveMany: (ids) => setArchiveIds(ids),
|
|
getTableInstance: () => tableRefsRef.current.tableInstance,
|
|
});
|
|
|
|
const columns = useMemo(
|
|
() => buildDataColumns(attributes, rowActions),
|
|
[attributes]
|
|
);
|
|
|
|
const handleArchiveSuccess = () => {
|
|
setArchiveTarget(null);
|
|
setArchiveIds(null);
|
|
tableRefsRef.current.resetSelection?.();
|
|
fetchAssets({ page: 1, limit: pagination?.limit ?? 10 });
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<DataTable
|
|
title="Files"
|
|
data={assets}
|
|
columns={columns}
|
|
attributes={attributes}
|
|
pagination={pagination}
|
|
setPagination={setPagination}
|
|
loading={loading}
|
|
onFetch={fetchAssets}
|
|
onFetchFilterData={fetchAssetFieldValues}
|
|
onRefsReady={handleRefsReady}
|
|
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="file"
|
|
emptyMessage="No files match the current filters."
|
|
/>
|
|
|
|
{/* ── Single archive ── */}
|
|
<ArchiveDialog
|
|
open={!!archiveTarget}
|
|
onOpenChange={(v) => !v && setArchiveTarget(null)}
|
|
entity={archiveTarget}
|
|
entityLabel="File"
|
|
getName={(a) => a?.display_name ?? a?.original_name}
|
|
onArchive={(a) => archiveAsset(a?.asset_id)}
|
|
loading={loading}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
|
|
{/* ── Bulk archive ── */}
|
|
<ArchiveDialog
|
|
open={!!archiveIds}
|
|
onOpenChange={(v) => !v && setArchiveIds(null)}
|
|
ids={archiveIds ?? []}
|
|
entityLabel="File"
|
|
onArchive={(ids) => archiveAssets(ids)}
|
|
loading={loading}
|
|
onSuccess={handleArchiveSuccess}
|
|
/>
|
|
</>
|
|
);
|
|
} |