feat: enhance session event handling by including userId for client-side filtering and updating SessionLiveWrapper with currentUserId

This commit is contained in:
Julien Froidefond
2025-11-27 14:38:05 +01:00
parent 15ea89f477
commit 639bb159fe
5 changed files with 21 additions and 2 deletions

BIN
dev.db

Binary file not shown.

View File

@@ -66,6 +66,7 @@ export async function GET(
`data: ${JSON.stringify({ `data: ${JSON.stringify({
type: event.type, type: event.type,
payload: JSON.parse(event.payload), payload: JSON.parse(event.payload),
userId: event.userId, // Include userId for client-side filtering
user: event.user, user: event.user,
timestamp: event.createdAt, timestamp: event.createdAt,
})}\n\n` })}\n\n`

View File

@@ -66,6 +66,7 @@ export default async function SessionPage({ params }: SessionPageProps) {
<SessionLiveWrapper <SessionLiveWrapper
sessionId={session.id} sessionId={session.id}
sessionTitle={session.title} sessionTitle={session.title}
currentUserId={authSession.user.id}
shares={session.shares} shares={session.shares}
isOwner={session.isOwner} isOwner={session.isOwner}
canEdit={session.canEdit} canEdit={session.canEdit}

View File

@@ -23,6 +23,7 @@ interface Share {
interface SessionLiveWrapperProps { interface SessionLiveWrapperProps {
sessionId: string; sessionId: string;
sessionTitle: string; sessionTitle: string;
currentUserId: string;
shares: Share[]; shares: Share[];
isOwner: boolean; isOwner: boolean;
canEdit: boolean; canEdit: boolean;
@@ -32,6 +33,7 @@ interface SessionLiveWrapperProps {
export function SessionLiveWrapper({ export function SessionLiveWrapper({
sessionId, sessionId,
sessionTitle, sessionTitle,
currentUserId,
shares, shares,
isOwner, isOwner,
canEdit, canEdit,
@@ -51,6 +53,7 @@ export function SessionLiveWrapper({
const { isConnected, error } = useSessionLive({ const { isConnected, error } = useSessionLive({
sessionId, sessionId,
currentUserId,
onEvent: handleEvent, onEvent: handleEvent,
}); });

View File

@@ -6,12 +6,14 @@ import { useRouter } from 'next/navigation';
export type LiveEvent = { export type LiveEvent = {
type: string; type: string;
payload: Record<string, unknown>; payload: Record<string, unknown>;
userId?: string; // ID of the user who created the event
user?: { id: string; name: string | null; email: string }; user?: { id: string; name: string | null; email: string };
timestamp: string; timestamp: string;
}; };
interface UseSessionLiveOptions { interface UseSessionLiveOptions {
sessionId: string; sessionId: string;
currentUserId?: string; // Current user ID for client-side filtering
enabled?: boolean; enabled?: boolean;
onEvent?: (event: LiveEvent) => void; onEvent?: (event: LiveEvent) => void;
} }
@@ -24,6 +26,7 @@ interface UseSessionLiveReturn {
export function useSessionLive({ export function useSessionLive({
sessionId, sessionId,
currentUserId,
enabled = true, enabled = true,
onEvent, onEvent,
}: UseSessionLiveOptions): UseSessionLiveReturn { }: UseSessionLiveOptions): UseSessionLiveReturn {
@@ -35,12 +38,17 @@ export function useSessionLive({
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null); const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const reconnectAttemptsRef = useRef(0); const reconnectAttemptsRef = useRef(0);
const onEventRef = useRef(onEvent); const onEventRef = useRef(onEvent);
const currentUserIdRef = useRef(currentUserId);
// Keep onEvent ref updated // Keep refs updated
useEffect(() => { useEffect(() => {
onEventRef.current = onEvent; onEventRef.current = onEvent;
}, [onEvent]); }, [onEvent]);
useEffect(() => {
currentUserIdRef.current = currentUserId;
}, [currentUserId]);
useEffect(() => { useEffect(() => {
if (!enabled || typeof window === 'undefined') return; if (!enabled || typeof window === 'undefined') return;
@@ -69,10 +77,16 @@ export function useSessionLive({
return; return;
} }
// Client-side filter: ignore events created by current user
// This prevents duplicates when revalidatePath already refreshed the data
if (currentUserIdRef.current && data.userId === currentUserIdRef.current) {
return;
}
setLastEvent(data); setLastEvent(data);
onEventRef.current?.(data); onEventRef.current?.(data);
// Refresh the page data when we receive an event // Refresh the page data when we receive an event from another user
router.refresh(); router.refresh();
} catch (e) { } catch (e) {
console.error('Failed to parse SSE event:', e); console.error('Failed to parse SSE event:', e);