Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
1.0 kB ยท 40 lines
TypeScript
at incident-api
1234567891011121314151617181920212223242526272829303132333435363738394041"use client";
import { useCallback, useEffect, useState } from "react";
export function useCookieState<T extends string>( name: string, defaultValue?: T, config?: { expires?: number },) { const [state, setState] = useState<T>();
const handleChange = useCallback( (value: T) => { if (document) { // `expires` is a duration in ms; the cookie needs an absolute date const ttl = config?.expires ?? 365 * 24 * 60 * 60 * 1000; // one year const expires = new Date(Date.now() + ttl).toUTCString(); document.cookie = `${name}=${value}; path=/; expires=${expires}`; setState(value); } }, [name, config], );
useEffect(() => { if (document) { const cookie = document.cookie .split("; ") .find((row) => row.startsWith(name)); if (!cookie) { setState(defaultValue); return; } const value = cookie.split("=")[1] as T; setState(value); } }, []);
return [state, handleChange] as const;}