mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-11-22 09:39:30 +00:00
feat: preserve links to packages
This changes url handling in status page. Firstly, instead of using hash for repository, now they are handled by query parameter. Secondly, it adds a query parameter for package base. And lastly, it changes behaviour for clicking on the package link, now it opens status page, which allows to e.g. open a tab in background
This commit is contained in:
1 parent
eafa1b417a
commit
f776b33310
4 files changed
+87
-21
No files matched your search
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
function subscribe(callback: () => void): () => void {
|
||||
window.addEventListener("popstate", callback);
|
||||
return () => window.removeEventListener("popstate", callback);
|
||||
}
|
||||
|
||||
function getSnapshot(): string {
|
||||
return window.location.search;
|
||||
}
|
||||
|
||||
export function useLocationQuery(): string {
|
||||
return useSyncExternalStore(subscribe, getSnapshot);
|
||||
}
|
||||
|
||||
export function packageHref(repositoryKey: string, packageBase: string | null): string {
|
||||
const query = new URLSearchParams(window.location.search);
|
||||
query.set("repository_id", repositoryKey);
|
||||
if (packageBase) {
|
||||
query.set("package", packageBase);
|
||||
} else {
|
||||
query.delete("package");
|
||||
}
|
||||
return `${window.location.pathname}?${query}`;
|
||||
}
|
||||
|
||||
export function navigate(href: string): void {
|
||||
if (href === `${window.location.pathname}${window.location.search}`) {
|
||||
return;
|
||||
}
|
||||
window.history.pushState(null, "", href);
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
}
|
||||
@@ -19,6 +19,8 @@
|
||||
*/
|
||||
import type { GridFilterModel } from "@mui/x-data-grid";
|
||||
import { useLocalStorage } from "hooks/useLocalStorage";
|
||||
import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery";
|
||||
import { useRepository } from "hooks/useRepository";
|
||||
import { useState } from "react";
|
||||
|
||||
export type DialogType = "dashboard" | "add" | "rebuild" | "keyImport";
|
||||
@@ -43,7 +45,14 @@ export interface UseTableStateResult {
|
||||
export function useTableState(): UseTableStateResult {
|
||||
const [selectionModel, setSelectionModel] = useState<string[]>([]);
|
||||
const [dialogOpen, setDialogOpen] = useState<DialogType | null>(null);
|
||||
const [selectedPackage, setSelectedPackage] = useState<string | null>(null);
|
||||
const { currentRepository } = useRepository();
|
||||
const packageBase = new URLSearchParams(useLocationQuery()).get("package");
|
||||
const selectedPackage = currentRepository && packageBase ? packageBase : null;
|
||||
const setSelectedPackage = (base: string | null): void => {
|
||||
if (currentRepository) {
|
||||
navigate(packageHref(currentRepository.key, base));
|
||||
}
|
||||
};
|
||||
const [searchText, setSearchText] = useState("");
|
||||
|
||||
const [paginationModel, setPaginationModel] = useLocalStorage("ahriman-packages-pagination", {
|
||||
|
||||
Reference in new issue
Block a user