first commit
linter / quality (push) Canceled after 0s
tests / ci (8.3) (push) Canceled after 0s
tests / ci (8.4) (push) Canceled after 0s
tests / ci (8.5) (push) Canceled after 0s

This commit is contained in:
salam committed 2026-10-06 18:53:03 +02:00
commit e687b4437b
418 files changed
+33577

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
import type { LinkComponentBaseProps } from '@inertiajs/core';
import { page } from '@inertiajs/svelte';
import { toUrl } from '@/lib/utils';
export type CurrentUrlState = {
readonly currentUrl: string;
isCurrentUrl: (
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
currentUrl: string,
) => boolean;
isCurrentOrParentUrl: (
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
currentUrl: string,
) => boolean;
whenCurrentUrl: <TIfTrue, TIfFalse>(
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
currentUrl: string,
ifTrue: TIfTrue,
ifFalse: TIfFalse,
) => TIfTrue | TIfFalse;
};
export function currentUrlState(): CurrentUrlState {
const currentUrl = $derived.by(() => {
const origin =
typeof window === 'undefined'
? 'http://localhost'
: window.location.origin;
try {
return new URL(page.url, origin).pathname;
} catch {
return page.url;
}
});
function isCurrentUrl(
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
current: string,
): boolean {
const resolved = toUrl(urlToCheck);
if (typeof resolved !== 'string') {
return false;
}
return current === resolved;
}
function isCurrentOrParentUrl(
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
current: string,
): boolean {
const resolved = toUrl(urlToCheck);
if (typeof resolved !== 'string') {
return false;
}
return current.startsWith(resolved);
}
function whenCurrentUrl<TIfTrue, TIfFalse>(
urlToCheck: NonNullable<LinkComponentBaseProps['href']>,
current: string,
ifTrue: TIfTrue,
ifFalse: TIfFalse,
): TIfTrue | TIfFalse {
return isCurrentUrl(urlToCheck, current) ? ifTrue : ifFalse;
}
return {
get currentUrl() {
return currentUrl;
},
isCurrentUrl,
isCurrentOrParentUrl,
whenCurrentUrl,
};
}