Files
gerbeur/src/contexts/WSContext.ts
khannurien 6c4f410d9b
Some checks failed
Build and Publish Docker Image / build-and-push (push) Has been cancelled
v3: added a chatbox
2026-06-29 20:25:10 +00:00

120 lines
3.2 KiB
TypeScript

import { createContext } from "react";
import type {
ChatMessage,
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<string, number>;
myVotes: Set<string>;
likeCounts: Record<string, number>;
myLikes: Set<string>;
recentDumps: Dump[];
deletedDumpIds: Set<string>;
lastVoteEvent: VoteEvent | null;
lastLikeEvent: LikeEvent | null;
lastDumpEvent: Dump | null;
lastPlaylistEvent: PlaylistEvent | null;
deletedPlaylistIds: Set<string>;
lastCommentEvent: CommentEvent | null;
lastUserEvent: UserEvent | null;
unreadNotificationCount: number;
lastNotification: Notification | null;
/** Live buffer of chat messages received this session (capped). */
chatMessages: ChatMessage[];
/** Ids of chat messages deleted live this session, so fetched history can
* filter them out. */
deletedChatIds: Set<string>;
unreadChatCount: number;
/** 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;
sendChatMessage: (body: string) => void;
editChatMessage: (id: string, body: string) => void;
deleteChatMessage: (id: string) => void;
clearUnreadChat: () => void;
/** Inform the server whether the chatbox is open (suppresses redundant
* mention notifications while the user is reading chat). */
setChatOpen: (open: boolean) => void;
}
export const WSContext = createContext<WSContextValue>({
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,
chatMessages: [],
deletedChatIds: new Set(),
unreadChatCount: 0,
connectionEpoch: 0,
castVote: () => {},
removeVote: () => {},
castCommentLike: () => {},
removeCommentLike: () => {},
injectDump: () => {},
clearUnreadNotifications: () => {},
sendChatMessage: () => {},
editChatMessage: () => {},
deleteChatMessage: () => {},
clearUnreadChat: () => {},
setChatOpen: () => {},
});