mirror of
https://github.com/arkorty/Osborne.git
synced 2026-03-18 00:57:14 +00:00
fix websocket connection stuff
This commit is contained in:
50
client/lib/cookies.ts
Normal file
50
client/lib/cookies.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Cookie utility functions for browser-based cookie management
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get a cookie value by name
|
||||
* @param name - The name of the cookie
|
||||
* @returns The cookie value or null if not found
|
||||
*/
|
||||
export const getCookie = (name: string): string | null => {
|
||||
if (typeof window === 'undefined') return null;
|
||||
const value = `; ${document.cookie}`;
|
||||
const parts = value.split(`; ${name}=`);
|
||||
if (parts.length === 2) {
|
||||
const cookieValue = parts.pop()?.split(';').shift();
|
||||
return cookieValue ? decodeURIComponent(cookieValue) : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set a cookie with an expiration date
|
||||
* @param name - The name of the cookie
|
||||
* @param value - The value to store
|
||||
* @param days - Number of days until expiration (default: 30)
|
||||
*/
|
||||
export const setCookie = (name: string, value: string, days: number = 30) => {
|
||||
if (typeof window === 'undefined') return;
|
||||
const expires = new Date();
|
||||
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
|
||||
document.cookie = `${name}=${encodeURIComponent(value)};expires=${expires.toUTCString()};path=/;SameSite=Lax`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a cookie by setting its expiration to the past
|
||||
* @param name - The name of the cookie to delete
|
||||
*/
|
||||
export const deleteCookie = (name: string) => {
|
||||
if (typeof window === 'undefined') return;
|
||||
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;SameSite=Lax`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a cookie exists
|
||||
* @param name - The name of the cookie
|
||||
* @returns True if the cookie exists, false otherwise
|
||||
*/
|
||||
export const cookieExists = (name: string): boolean => {
|
||||
return getCookie(name) !== null;
|
||||
};
|
||||
@@ -377,11 +377,11 @@ export const getNextTheme = (currentThemeId: string): ThemeConfig => {
|
||||
|
||||
// Cookie utilities
|
||||
export const saveThemeToCookie = (themeId: string): void => {
|
||||
document.cookie = `theme=${themeId}; path=/; max-age=${60 * 60 * 24 * 365}`; // 1 year
|
||||
document.cookie = `osborne-theme=${themeId}; path=/; max-age=${60 * 60 * 24 * 365}`; // 1 year
|
||||
};
|
||||
|
||||
export const getThemeFromCookie = (): string | null => {
|
||||
const match = document.cookie.match(/(?:^|; )theme=([^;]*)/);
|
||||
const match = document.cookie.match(/(?:^|; )osborne-theme=([^;]*)/);
|
||||
return match ? decodeURIComponent(match[1]) : null;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user