/* * 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 { useQueryClient } from "@tanstack/react-query"; import { ApiError } from "api/client/ApiError"; import { QueryKeys } from "hooks/QueryKeys"; import { useClient } from "hooks/useClient"; import { useNotification } from "hooks/useNotification"; import { useRepository } from "hooks/useRepository"; export interface UsePackageActionsResult { handleReload: () => void; handleUpdate: () => Promise; handleRefreshDb: () => Promise; handleRemove: () => Promise; } export function usePackageActions( selectionModel: string[], setSelectionModel: (model: string[]) => void, ): UsePackageActionsResult { const client = useClient(); const { current } = useRepository(); const { showSuccess, showError } = useNotification(); const queryClient = useQueryClient(); const handleReload: () => void = () => { if (!current) { return; } void queryClient.invalidateQueries({ queryKey: QueryKeys.packages(current) }); void queryClient.invalidateQueries({ queryKey: QueryKeys.status(current) }); }; const handleUpdate: () => Promise = async () => { if (!current) { return; } try { if (selectionModel.length === 0) { await client.servicePackageUpdate(current, { packages: [] }); showSuccess("Success", "Repository update has been run"); } else { await client.servicePackageAdd(current, { packages: selectionModel }); showSuccess("Success", `Run update for packages ${selectionModel.join(", ")}`); } setSelectionModel([]); void queryClient.invalidateQueries({ queryKey: QueryKeys.packages(current) }); void queryClient.invalidateQueries({ queryKey: QueryKeys.status(current) }); } catch (exception) { const detail = ApiError.errorDetail(exception); showError("Action failed", `Packages update failed: ${detail}`); } }; const handleRefreshDb: () => Promise = async () => { if (!current) { return; } try { await client.servicePackageUpdate(current, { packages: [], refresh: true, aur: false, local: false, manual: false, }); showSuccess("Success", "Pacman database update has been requested"); setSelectionModel([]); void queryClient.invalidateQueries({ queryKey: QueryKeys.packages(current) }); void queryClient.invalidateQueries({ queryKey: QueryKeys.status(current) }); } catch (exception) { const detail = ApiError.errorDetail(exception); showError("Action failed", `Could not update pacman databases: ${detail}`); } }; const handleRemove: () => Promise = async () => { if (!current) { return; } if (selectionModel.length === 0) { return; } try { await client.servicePackageRemove(current, selectionModel); showSuccess("Success", `Packages ${selectionModel.join(", ")} have been removed`); setSelectionModel([]); void queryClient.invalidateQueries({ queryKey: QueryKeys.packages(current) }); void queryClient.invalidateQueries({ queryKey: QueryKeys.status(current) }); } catch (exception) { const detail = ApiError.errorDetail(exception); showError("Action failed", `Could not remove packages: ${detail}`); } }; return { handleReload, handleUpdate, handleRefreshDb, handleRemove, }; }