2026-09-11 12:53:55 -05:00
|
|
|
import { useCallback, useEffect, useMemo } from "react";
|
2026-09-03 15:58:13 -05:00
|
|
|
import axios from "axios";
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
import { useWs } from "@/api/ws";
|
2026-09-11 12:53:55 -05:00
|
|
|
import type { Notice } from "@/types/notice";
|
2026-09-03 15:58:13 -05:00
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
/** When a hidden notice was acknowledged or muted. */
|
|
|
|
|
export function hiddenAt(notice: Notice): number {
|
|
|
|
|
return notice.muted_at ?? notice.acknowledged_at ?? 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 15:58:13 -05:00
|
|
|
/**
|
2026-09-11 12:53:55 -05:00
|
|
|
* Active notices come from a REST snapshot, then from every `notices`
|
2026-09-24 16:39:18 -05:00
|
|
|
* websocket payload. Hidden notices are fetched only while the hidden list is
|
|
|
|
|
* shown, and again when the active list changes or the tab regains focus.
|
2026-09-03 15:58:13 -05:00
|
|
|
*/
|
2026-09-24 16:39:18 -05:00
|
|
|
export function useNotices(showHidden: boolean) {
|
2026-09-03 15:58:13 -05:00
|
|
|
const { data: initial, mutate } = useSWR<Notice[]>("notices", {
|
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
|
});
|
2026-09-24 16:39:18 -05:00
|
|
|
const { data: all, mutate: mutateHidden } = useSWR<Notice[]>(
|
|
|
|
|
showHidden ? ["notices", { include_hidden: true }] : null,
|
2026-09-03 15:58:13 -05:00
|
|
|
);
|
|
|
|
|
const {
|
|
|
|
|
value: { payload },
|
|
|
|
|
} = useWs("notices", "");
|
|
|
|
|
|
|
|
|
|
const live = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
payload && typeof payload === "string"
|
|
|
|
|
? (JSON.parse(payload) as Notice[])
|
|
|
|
|
: undefined,
|
|
|
|
|
[payload],
|
|
|
|
|
);
|
|
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
// once a websocket frame has arrived it is the source of truth; every
|
|
|
|
|
// acknowledge, mute, and unhide publishes a new frame
|
2026-09-03 15:58:13 -05:00
|
|
|
const notices = live ?? initial;
|
|
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
// refetch the hidden list whenever the active list changes; SWR ignores the
|
|
|
|
|
// call while the hidden list is not shown
|
2026-09-11 12:53:55 -05:00
|
|
|
useEffect(() => {
|
2026-09-24 16:39:18 -05:00
|
|
|
mutateHidden();
|
|
|
|
|
}, [live, mutateHidden]);
|
2026-09-11 12:53:55 -05:00
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
const hidden = useMemo(
|
2026-09-11 12:53:55 -05:00
|
|
|
() =>
|
2026-09-24 16:39:18 -05:00
|
|
|
all
|
|
|
|
|
?.filter((notice) => hiddenAt(notice) > 0)
|
|
|
|
|
.sort((a, b) => hiddenAt(b) - hiddenAt(a)),
|
|
|
|
|
[all],
|
2026-09-11 12:53:55 -05:00
|
|
|
);
|
2026-09-03 15:58:13 -05:00
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
const act = useCallback(
|
|
|
|
|
async (request: Promise<unknown>) => {
|
|
|
|
|
await request;
|
2026-09-03 15:58:13 -05:00
|
|
|
mutate();
|
|
|
|
|
},
|
2026-09-11 12:53:55 -05:00
|
|
|
[mutate],
|
2026-09-03 15:58:13 -05:00
|
|
|
);
|
|
|
|
|
|
2026-09-24 16:39:18 -05:00
|
|
|
const acknowledge = useCallback(
|
2026-11-30 12:36:04 -06:00
|
|
|
(id: string) =>
|
|
|
|
|
act(axios.post(`notices/${encodeURIComponent(id)}/acknowledge`)),
|
2026-09-24 16:39:18 -05:00
|
|
|
[act],
|
|
|
|
|
);
|
|
|
|
|
const mute = useCallback(
|
2026-11-30 12:36:04 -06:00
|
|
|
(id: string) => act(axios.post(`notices/${encodeURIComponent(id)}/mute`)),
|
2026-09-24 16:39:18 -05:00
|
|
|
[act],
|
|
|
|
|
);
|
|
|
|
|
const unhide = useCallback(
|
2026-11-30 12:36:04 -06:00
|
|
|
(id: string) =>
|
|
|
|
|
act(axios.delete(`notices/${encodeURIComponent(id)}/hidden`)),
|
2026-09-24 16:39:18 -05:00
|
|
|
[act],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { notices, hidden, acknowledge, mute, unhide, mutateHidden };
|
2026-09-03 15:58:13 -05:00
|
|
|
}
|