.
This commit is contained in:
1 parent
30dfb7ce58
commit
5af2c80cd1
33 files changed
+3806
-1440
No files matched your search
@@ -0,0 +1,90 @@
|
||||
import { useSyncExternalStore } from 'react';
|
||||
|
||||
export type DevCaptainSide = 'spectator' | 'blue' | 'red';
|
||||
|
||||
const AUTH_USER_ID_STORAGE_KEY = 'lolympus.authUserId';
|
||||
const DEV_CAPTAIN_SIDE_STORAGE_KEY = 'lolympus.devCaptainSide';
|
||||
const AUTH_USER_CHANGED_EVENT = 'lolympus:auth-user-changed';
|
||||
const CAPTAIN_SIDE_CHANGED_EVENT = 'lolympus:captain-side-changed';
|
||||
|
||||
export const API_BASE_URL = (
|
||||
import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3000'
|
||||
).replace(/\/$/, '');
|
||||
|
||||
export const DEFAULT_COMMUNITY_ID = (
|
||||
import.meta.env.VITE_DEFAULT_COMMUNITY_ID ?? ''
|
||||
).trim();
|
||||
|
||||
function dispatchWindowEvent(eventName: string) {
|
||||
if (typeof window === 'undefined') return;
|
||||
window.dispatchEvent(new Event(eventName));
|
||||
}
|
||||
|
||||
function subscribeToWindowChanges(eventName: string, callback: () => void) {
|
||||
if (typeof window === 'undefined') {
|
||||
return () => undefined;
|
||||
}
|
||||
|
||||
const handleChange = () => callback();
|
||||
window.addEventListener(eventName, handleChange);
|
||||
window.addEventListener('storage', handleChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener(eventName, handleChange);
|
||||
window.removeEventListener('storage', handleChange);
|
||||
};
|
||||
}
|
||||
|
||||
export function getAuthUserId(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
const value = window.localStorage.getItem(AUTH_USER_ID_STORAGE_KEY)?.trim();
|
||||
return value ? value : null;
|
||||
}
|
||||
|
||||
export function setAuthUserId(userId: string | null) {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
if (!userId?.trim()) {
|
||||
window.localStorage.removeItem(AUTH_USER_ID_STORAGE_KEY);
|
||||
} else {
|
||||
window.localStorage.setItem(AUTH_USER_ID_STORAGE_KEY, userId.trim());
|
||||
}
|
||||
|
||||
dispatchWindowEvent(AUTH_USER_CHANGED_EVENT);
|
||||
}
|
||||
|
||||
export function getDevCaptainSide(): DevCaptainSide {
|
||||
if (typeof window === 'undefined') return 'spectator';
|
||||
|
||||
const value = window.localStorage.getItem(
|
||||
DEV_CAPTAIN_SIDE_STORAGE_KEY,
|
||||
) as DevCaptainSide | null;
|
||||
|
||||
if (value === 'blue' || value === 'red' || value === 'spectator') {
|
||||
return value;
|
||||
}
|
||||
|
||||
return 'spectator';
|
||||
}
|
||||
|
||||
export function setDevCaptainSide(side: DevCaptainSide) {
|
||||
if (typeof window === 'undefined') return;
|
||||
window.localStorage.setItem(DEV_CAPTAIN_SIDE_STORAGE_KEY, side);
|
||||
dispatchWindowEvent(CAPTAIN_SIDE_CHANGED_EVENT);
|
||||
}
|
||||
|
||||
export function useAuthUserId(): string | null {
|
||||
return useSyncExternalStore<string | null>(
|
||||
(callback) => subscribeToWindowChanges(AUTH_USER_CHANGED_EVENT, callback),
|
||||
getAuthUserId,
|
||||
() => null,
|
||||
);
|
||||
}
|
||||
|
||||
export function useDevCaptainSide(): DevCaptainSide {
|
||||
return useSyncExternalStore<DevCaptainSide>(
|
||||
(callback) => subscribeToWindowChanges(CAPTAIN_SIDE_CHANGED_EVENT, callback),
|
||||
getDevCaptainSide,
|
||||
() => 'spectator',
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user