first commit
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-28 09:47:41 +02:00
commit 90a6848721
165 changed files with 27025 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
import { useSyncExternalStore } from 'react';
export type ResolvedAppearance = 'light' | 'dark';
export type Appearance = ResolvedAppearance | 'system';
export type UseAppearanceReturn = {
readonly appearance: Appearance;
readonly resolvedAppearance: ResolvedAppearance;
readonly updateAppearance: (mode: Appearance) => void;
};
const listeners = new Set<() => void>();
let currentAppearance: Appearance = 'system';
const prefersDark = (): boolean => {
if (typeof window === 'undefined') {
return false;
}
return window.matchMedia('(prefers-color-scheme: dark)').matches;
};
const setCookie = (name: string, value: string, days = 365): void => {
if (typeof document === 'undefined') {
return;
}
const maxAge = days * 24 * 60 * 60;
document.cookie = `${name}=${value};path=/;max-age=${maxAge};SameSite=Lax`;
};
const getStoredAppearance = (): Appearance => {
if (typeof window === 'undefined') {
return 'system';
}
return (localStorage.getItem('appearance') as Appearance) || 'system';
};
const isDarkMode = (appearance: Appearance): boolean => {
return appearance === 'dark' || (appearance === 'system' && prefersDark());
};
const applyTheme = (appearance: Appearance): void => {
if (typeof document === 'undefined') {
return;
}
const isDark = isDarkMode(appearance);
document.documentElement.classList.toggle('dark', isDark);
document.documentElement.style.colorScheme = isDark ? 'dark' : 'light';
};
const subscribe = (callback: () => void) => {
listeners.add(callback);
return () => listeners.delete(callback);
};
const notify = (): void => listeners.forEach((listener) => listener());
const mediaQuery = (): MediaQueryList | null => {
if (typeof window === 'undefined') {
return null;
}
return window.matchMedia('(prefers-color-scheme: dark)');
};
const handleSystemThemeChange = (): void => applyTheme(currentAppearance);
export function initializeTheme(): void {
if (typeof window === 'undefined') {
return;
}
if (!localStorage.getItem('appearance')) {
localStorage.setItem('appearance', 'system');
setCookie('appearance', 'system');
}
currentAppearance = getStoredAppearance();
applyTheme(currentAppearance);
// Set up system theme change listener
mediaQuery()?.addEventListener('change', handleSystemThemeChange);
}
export function useAppearance(): UseAppearanceReturn {
const appearance: Appearance = useSyncExternalStore(
subscribe,
() => currentAppearance,
() => 'system',
);
const resolvedAppearance: ResolvedAppearance = isDarkMode(appearance)
? 'dark'
: 'light';
const updateAppearance = (mode: Appearance): void => {
currentAppearance = mode;
// Store in localStorage for client-side persistence...
localStorage.setItem('appearance', mode);
// Store in cookie for SSR...
setCookie('appearance', mode);
applyTheme(mode);
notify();
};
return { appearance, resolvedAppearance, updateAppearance } as const;
}
+32
View File
@@ -0,0 +1,32 @@
// Credit: https://usehooks-ts.com/
import { useState } from 'react';
export type CopiedValue = string | null;
export type CopyFn = (text: string) => Promise<boolean>;
export type UseClipboardReturn = [CopiedValue, CopyFn];
export function useClipboard(): UseClipboardReturn {
const [copiedText, setCopiedText] = useState<CopiedValue>(null);
const copy: CopyFn = async (text) => {
if (!navigator?.clipboard) {
console.warn('Clipboard not supported');
return false;
}
try {
await navigator.clipboard.writeText(text);
setCopiedText(text);
return true;
} catch (error) {
console.warn('Copy failed', error);
setCopiedText(null);
return false;
}
};
return [copiedText, copy];
}
+83
View File
@@ -0,0 +1,83 @@
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,
startsWith?: boolean,
) => boolean;
export type IsCurrentOrParentUrlFn = (
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;
isCurrentOrParentUrl: IsCurrentOrParentUrlFn;
whenCurrentUrl: WhenCurrentUrlFn;
};
export function useCurrentUrl(): UseCurrentUrlReturn {
const page = usePage();
const currentUrlPath = new URL(
page.url,
typeof window !== 'undefined'
? window.location.origin
: 'http://localhost',
).pathname;
const isCurrentUrl: IsCurrentUrlFn = (
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl?: string,
startsWith: boolean = false,
) => {
const urlToCompare = currentUrl ?? currentUrlPath;
const urlString = toUrl(urlToCheck);
const comparePath = (path: string): boolean =>
startsWith ? urlToCompare.startsWith(path) : path === urlToCompare;
if (!urlString.startsWith('http')) {
return comparePath(urlString);
}
try {
const absoluteUrl = new URL(urlString);
return comparePath(absoluteUrl.pathname);
} catch {
return false;
}
};
const isCurrentOrParentUrl: IsCurrentOrParentUrlFn = (
urlToCheck: NonNullable<InertiaLinkProps['href']>,
currentUrl?: string,
) => {
return isCurrentUrl(urlToCheck, currentUrl, true);
};
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,
isCurrentOrParentUrl,
whenCurrentUrl,
};
}
+19
View File
@@ -0,0 +1,19 @@
import { router } from '@inertiajs/react';
import { useEffect } from 'react';
import { toast } from 'sonner';
import type { FlashToast } from '@/types/ui';
export function useFlashToast(): void {
useEffect(() => {
return router.on('flash', (event) => {
const flash = (event as CustomEvent).detail?.flash;
const data = flash?.toast as FlashToast | undefined;
if (!data) {
return;
}
toast[data.type](data.message);
});
}, []);
}
+26
View File
@@ -0,0 +1,26 @@
import { useCallback } from 'react';
export type GetInitialsFn = (fullName: string) => string;
function getInitial(name: string): string {
return Array.from(name)[0] ?? '';
}
export function useInitials(): GetInitialsFn {
return useCallback((fullName: string): string => {
const names = fullName.trim().split(/\s+/u).filter(Boolean);
if (names.length === 0) {
return '';
}
if (names.length === 1) {
return getInitial(names[0]).toUpperCase();
}
const firstInitial = getInitial(names[0]);
const lastInitial = getInitial(names[names.length - 1]);
return `${firstInitial}${lastInitial}`.toUpperCase();
}, []);
}
@@ -0,0 +1,10 @@
import { useCallback } from 'react';
export type CleanupFn = () => void;
export function useMobileNavigation(): CleanupFn {
return useCallback(() => {
// Remove pointer-events style from body...
document.body.style.removeProperty('pointer-events');
}, []);
}
+36
View File
@@ -0,0 +1,36 @@
import { useSyncExternalStore } from 'react';
const MOBILE_BREAKPOINT = 768;
const mql =
typeof window === 'undefined'
? undefined
: window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
function mediaQueryListener(callback: (event: MediaQueryListEvent) => void) {
if (!mql) {
return () => {};
}
mql.addEventListener('change', callback);
return () => {
mql.removeEventListener('change', callback);
};
}
function isSmallerThanBreakpoint(): boolean {
return mql?.matches ?? false;
}
function getServerSnapshot(): boolean {
return false;
}
export function useIsMobile(): boolean {
return useSyncExternalStore(
mediaQueryListener,
isSmallerThanBreakpoint,
getServerSnapshot,
);
}