import { createContext } from "react"; import type { Comment, Dump, Notification, OnlineUser, Playlist, PublicUser, } from "../model.ts"; export interface VoteEvent { dumpId: string; voterId: string; action: "cast" | "remove"; } export interface LikeEvent { commentId: string; likerId: string; action: "cast" | "remove"; } export interface PlaylistEvent { type: "created" | "updated" | "deleted" | "dumps_updated"; playlistId: string; playlist?: Playlist; userId?: string; dumpIds?: string[]; } export interface CommentEvent { type: "created" | "deleted" | "updated"; dumpId: string; comment?: Comment; commentId?: string; } export interface UserEvent { user: PublicUser; } export interface WSContextValue { wsStatus: "connecting" | "connected" | "disconnected"; wsErrorMessage: string | null; onlineUsers: OnlineUser[]; voteCounts: Record; myVotes: Set; likeCounts: Record; myLikes: Set; recentDumps: Dump[]; deletedDumpIds: Set; lastVoteEvent: VoteEvent | null; lastLikeEvent: LikeEvent | null; lastDumpEvent: Dump | null; lastPlaylistEvent: PlaylistEvent | null; deletedPlaylistIds: Set; lastCommentEvent: CommentEvent | null; lastUserEvent: UserEvent | null; unreadNotificationCount: number; lastNotification: Notification | null; /** Increments on each WS reconnect so pages can backfill missed events. */ connectionEpoch: number; castVote: (dumpId: string) => void; removeVote: (dumpId: string) => void; castCommentLike: (commentId: string) => void; removeCommentLike: (commentId: string) => void; injectDump: (dump: Dump) => void; clearUnreadNotifications: () => void; } export const WSContext = createContext({ wsStatus: "connecting", wsErrorMessage: null, onlineUsers: [], voteCounts: {}, myVotes: new Set(), likeCounts: {}, myLikes: new Set(), recentDumps: [], deletedDumpIds: new Set(), lastVoteEvent: null, lastLikeEvent: null, lastDumpEvent: null, lastPlaylistEvent: null, deletedPlaylistIds: new Set(), lastCommentEvent: null, lastUserEvent: null, unreadNotificationCount: 0, lastNotification: null, connectionEpoch: 0, castVote: () => {}, removeVote: () => {}, castCommentLike: () => {}, removeCommentLike: () => {}, injectDump: () => {}, clearUnreadNotifications: () => {}, });