first commit

This commit is contained in:
2026-02-04 23:23:42 +07:00
commit ee28ea1a2d
202 changed files with 34797 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import type { InertiaLinkProps } from '@inertiajs/react';
import { usePage } from '@inertiajs/react';
import { toUrl } from '@/lib/utils';
export type IsCurrentUrlFn = (
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl?: string,
) => boolean;
export type WhenCurrentUrlFn = <TIfTrue, TIfFalse = null>(
urlToCheck: NonNullable<InertiaLinkProps['href']>,
ifTrue: TIfTrue,
ifFalse?: TIfFalse,
) => TIfTrue | TIfFalse;
export type UseCurrentUrlReturn = {
currentUrl: string;
isCurrentUrl: IsCurrentUrlFn;
whenCurrentUrl: WhenCurrentUrlFn;
};
export function useCurrentUrl(): UseCurrentUrlReturn {
const page = usePage();
const currentUrlPath = new URL(page.url, window?.location.origin).pathname;
const isCurrentUrl: IsCurrentUrlFn = (
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl?: string,
) => {
const urlToCompare = currentUrl ?? currentUrlPath;
const urlString = toUrl(urlToCheck);
if (!urlString.startsWith('http')) {
return urlString === urlToCompare;
}
try {
const absoluteUrl = new URL(urlString);
return absoluteUrl.pathname === urlToCompare;
} catch {
return false;
}
};
const whenCurrentUrl: WhenCurrentUrlFn = <TIfTrue, TIfFalse = null>(
urlToCheck: NonNullable<InertiaLinkProps['href']>,
ifTrue: TIfTrue,
ifFalse: TIfFalse = null as TIfFalse,
): TIfTrue | TIfFalse => {
return isCurrentUrl(urlToCheck) ? ifTrue : ifFalse;
};
return {
currentUrl: currentUrlPath,
isCurrentUrl,
whenCurrentUrl,
};
}