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
@@ -18,31 +18,23 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import { RepositoryContext } from "contexts/RepositoryContext";
|
||||
import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery";
|
||||
import type { RepositoryId } from "models/RepositoryId";
|
||||
import React, { type ReactNode, useCallback, useMemo, useState, useSyncExternalStore } from "react";
|
||||
|
||||
function subscribeToHash(callback: () => void): () => void {
|
||||
window.addEventListener("hashchange", callback);
|
||||
return () => window.removeEventListener("hashchange", callback);
|
||||
}
|
||||
|
||||
function getHashSnapshot(): string {
|
||||
return window.location.hash.replace("#", "");
|
||||
}
|
||||
import React, { type ReactNode, useCallback, useMemo, useState } from "react";
|
||||
|
||||
export function RepositoryProvider({ children }: { children: ReactNode }): React.JSX.Element {
|
||||
const [repositories, setRepositories] = useState<RepositoryId[]>([]);
|
||||
const hash = useSyncExternalStore(subscribeToHash, getHashSnapshot);
|
||||
const repositoryKey = new URLSearchParams(useLocationQuery()).get("repository_id");
|
||||
|
||||
const currentRepository = useMemo(() => {
|
||||
if (repositories.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return repositories.find(repository => repository.key === hash) ?? repositories[0] ?? null;
|
||||
}, [repositories, hash]);
|
||||
return repositories.find(repository => repository.key === repositoryKey) ?? repositories[0] ?? null;
|
||||
}, [repositories, repositoryKey]);
|
||||
|
||||
const setCurrentRepository = useCallback((repository: RepositoryId) => {
|
||||
window.location.hash = repository.key;
|
||||
navigate(packageHref(repository.key, null));
|
||||
}, []);
|
||||
|
||||
const value = useMemo(() => ({
|
||||
|
||||
Reference in new issue
Block a user