feat : update
This commit is contained in:
+24
-6
@@ -4,8 +4,15 @@ import { useEffect, useEffectEvent, useState } from "react";
|
||||
|
||||
import { PRESENCE_HEARTBEAT_MS } from "@/lib/presence-constants";
|
||||
|
||||
type VisitorCounts = { global: number; guide: number | null };
|
||||
export type EditorsByGuide = Record<string, Array<{ userId: string; name: string }>>;
|
||||
type VisitorCounts = {
|
||||
global: number;
|
||||
guide: number | null;
|
||||
byGuide: Record<string, number>;
|
||||
};
|
||||
export type EditorsByGuide = Record<
|
||||
string,
|
||||
Array<{ userId: string; name: string; sections: string[] }>
|
||||
>;
|
||||
|
||||
function tabId(storageKey: string): string {
|
||||
const existing = window.sessionStorage.getItem(storageKey);
|
||||
@@ -18,9 +25,11 @@ function tabId(storageKey: string): string {
|
||||
function useHeartbeat<T>({
|
||||
endpoint,
|
||||
guideId,
|
||||
section,
|
||||
}: {
|
||||
endpoint: string;
|
||||
guideId: string | null;
|
||||
section?: string | null;
|
||||
}) {
|
||||
const [value, setValue] = useState<T | null>(null);
|
||||
const send = useEffectEvent(async (active = true) => {
|
||||
@@ -28,7 +37,12 @@ function useHeartbeat<T>({
|
||||
const response = await fetch(endpoint, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ tabId: tabId(`buzz-sheet:${endpoint}:tab`), guideId, active }),
|
||||
body: JSON.stringify({
|
||||
tabId: tabId(`buzz-sheet:${endpoint}:tab`),
|
||||
guideId,
|
||||
...(section !== undefined ? { section } : {}),
|
||||
active,
|
||||
}),
|
||||
cache: "no-store",
|
||||
keepalive: !active,
|
||||
});
|
||||
@@ -56,7 +70,7 @@ function useHeartbeat<T>({
|
||||
window.removeEventListener("pagehide", leave);
|
||||
document.removeEventListener("visibilitychange", refresh);
|
||||
};
|
||||
}, [endpoint, guideId]);
|
||||
}, [endpoint, guideId, section]);
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -65,6 +79,10 @@ export function useVisitorPresence(guideId: string | null) {
|
||||
return useHeartbeat<VisitorCounts>({ endpoint: "/api/presence/visitors", guideId });
|
||||
}
|
||||
|
||||
export function useAdminPresence(guideId: string | null) {
|
||||
return useHeartbeat<{ editorsByGuide: EditorsByGuide }>({ endpoint: "/api/presence/admin", guideId });
|
||||
export function useAdminPresence(guideId: string | null, section: string | null = null) {
|
||||
return useHeartbeat<{ editorsByGuide: EditorsByGuide }>({
|
||||
endpoint: "/api/presence/admin",
|
||||
guideId,
|
||||
section,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user