21 lines
829 B
TypeScript
21 lines
829 B
TypeScript
import { replaceState } from '$app/navigation';
|
|
import { page } from '$app/stores';
|
|
import { get } from 'svelte/store';
|
|
|
|
/** Keep library working views shareable and restore them on browser Back. */
|
|
export function saveLibraryContext(
|
|
values: Record<string, string | number>,
|
|
defaults: Record<string, string | number> = {}
|
|
) {
|
|
const url = new URL(window.location.href);
|
|
for (const [key, value] of Object.entries(values)) {
|
|
if (value === '' || value === defaults[key]) url.searchParams.delete(key);
|
|
else url.searchParams.set(key, String(value));
|
|
}
|
|
if (url.href !== window.location.href) replaceState(url, get(page).state);
|
|
}
|
|
|
|
export function libraryLimit(params: URLSearchParams): number {
|
|
return Math.max(25, Math.min(10000, Number.parseInt(params.get('limit') ?? '25', 10) || 25));
|
|
}
|