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.
974 B ยท 41 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142"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) { const date = new Date(); date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000); // in one year document.cookie = `${name}=${value}; path=/; expires=${ config?.expires ?? date.toUTCString() }`; 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;}