/* * Copyright (c) 2021-2026 ahriman team. * * This file is part of ahriman * (see https://github.com/arcan1s/ahriman). * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import type { GridFilterModel } from "@mui/x-data-grid"; import { usePackageActions } from "hooks/usePackageActions"; import { usePackageData } from "hooks/usePackageData"; import { useTableState } from "hooks/useTableState"; import type { AutoRefreshInterval } from "models/AutoRefreshInterval"; import type { BuildStatus } from "models/BuildStatus"; import type { PackageRow } from "models/PackageRow"; import { useEffect } from "react"; export interface UsePackageTableResult { autoRefreshInterval: number; columnVisibility: Record; dialogOpen: "dashboard" | "add" | "rebuild" | "keyImport" | null; filterModel: GridFilterModel; handleRefreshDatabase: () => Promise; handleReload: () => void; handleRemove: () => Promise; handleUpdate: () => Promise; isAuthorized: boolean; isLoading: boolean; onAutoRefreshIntervalChange: (interval: number) => void; paginationModel: { page: number; pageSize: number }; rows: PackageRow[]; searchText: string; selectedPackage: string | null; selectionModel: string[]; setColumnVisibility: (model: Record) => void; setDialogOpen: (dialog: "dashboard" | "add" | "rebuild" | "keyImport" | null) => void; setFilterModel: (model: GridFilterModel) => void; setPaginationModel: (model: { page: number; pageSize: number }) => void; setSearchText: (text: string) => void; setSelectedPackage: (base: string | null) => void; setSelectionModel: (model: string[]) => void; status: BuildStatus | undefined; } export function usePackageTable(autoRefreshIntervals: AutoRefreshInterval[]): UsePackageTableResult { const { rows, isLoading, isAuthorized, status, autoRefresh } = usePackageData(autoRefreshIntervals); const tableState = useTableState(); const actions = usePackageActions(tableState.selectionModel, tableState.setSelectionModel); // Pause auto-refresh when dialog is open const isDialogOpen = tableState.dialogOpen !== null || tableState.selectedPackage !== null; const setPaused = autoRefresh.setPaused; useEffect(() => { setPaused(isDialogOpen); }, [isDialogOpen, setPaused]); return { autoRefreshInterval: autoRefresh.interval, isLoading, isAuthorized, onAutoRefreshIntervalChange: autoRefresh.setInterval, rows, status, ...actions, ...tableState, }; }