♻️(front) add shared date utils and useIsMobile hook

Add date formatting utilities and a custom hook to
detect mobile viewport for responsive behavior.
This commit is contained in:
Nathan Panchout
2026-03-11 10:43:08 +01:00
parent 2760770f3f
commit 75f26b59d2
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
import { useSyncExternalStore } from "react";
export const MOBILE_BREAKPOINT = 768;
const MEDIA_QUERY = `(max-width: ${MOBILE_BREAKPOINT}px)`;
function subscribe(callback: () => void): () => void {
const mql = window.matchMedia(MEDIA_QUERY);
mql.addEventListener("change", callback);
return () => mql.removeEventListener("change", callback);
}
function getSnapshot(): boolean {
return window.matchMedia(MEDIA_QUERY).matches;
}
function getServerSnapshot(): boolean {
return false;
}
export function useIsMobile(): boolean {
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}

View File

@@ -0,0 +1,31 @@
export function isSameDay(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
);
}
export function isToday(date: Date): boolean {
return isSameDay(date, new Date());
}
export function isWeekend(date: Date): boolean {
const day = date.getDay();
return day === 0 || day === 6;
}
export function addDays(date: Date, days: number): Date {
const d = new Date(date);
d.setDate(d.getDate() + days);
return d;
}
export function getWeekStart(date: Date, firstDayOfWeek: number): Date {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
const dayOfWeek = d.getDay();
const diff = (dayOfWeek - firstDayOfWeek + 7) % 7;
d.setDate(d.getDate() - diff);
return d;
}