All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 40s
98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
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<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;
|
|
/** 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<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,
|
|
connectionEpoch: 0,
|
|
castVote: () => {},
|
|
removeVote: () => {},
|
|
castCommentLike: () => {},
|
|
removeCommentLike: () => {},
|
|
injectDump: () => {},
|
|
clearUnreadNotifications: () => {},
|
|
});
|