Files
ahriman/frontend/src/hooks/useLocationQuery.ts
T
arcanis f776b33310 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
2026-10-22 11:33:06 +03:00

53 lines
1.8 KiB
TypeScript

/*
* 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"));
}