Initial commit baby!

This commit is contained in:
Rich Teslow
2026-04-13 18:15:41 -07:00
parent 10e6227931
commit 6b0fc9fdf1
99 changed files with 34064 additions and 3 deletions
+6
View File
@@ -0,0 +1,6 @@
import { useAuthStore } from '../store/authStore';
export function useCanWrite(): boolean {
const user = useAuthStore((state) => state.user);
return user?.role !== 'viewer';
}
+45
View File
@@ -0,0 +1,45 @@
import { useEffect, useRef } from 'react';
import { io, Socket } from 'socket.io-client';
import { useAuthStore } from '../store/authStore';
let globalSocket: Socket | null = null;
export function getSocket(): Socket | null {
return globalSocket;
}
export function useSocket(
events: Record<string, (data: unknown) => void>
): void {
const isAuthenticated = useAuthStore((s) => s.isAuthenticated);
const eventsRef = useRef(events);
eventsRef.current = events;
useEffect(() => {
if (!isAuthenticated) return;
if (!globalSocket) {
globalSocket = io('/', {
path: '/socket.io',
transports: ['websocket'],
reconnection: true,
reconnectionDelay: 2000,
});
}
const socket = globalSocket;
const handlers: Array<[string, (d: unknown) => void]> = [];
for (const [event, handler] of Object.entries(eventsRef.current)) {
const wrapped = (data: unknown) => handler(data);
socket.on(event, wrapped);
handlers.push([event, wrapped]);
}
return () => {
for (const [event, handler] of handlers) {
socket.off(event, handler);
}
};
}, [isAuthenticated]);
}