关闭后前台隐藏私聊/群聊入口并由后端拦截;同步去掉公告页重复计数胶囊。 Co-authored-by: Cursor <cursoragent@cursor.com>
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
"use client";
|
||
|
||
import {
|
||
createContext,
|
||
useContext,
|
||
useEffect,
|
||
useRef,
|
||
useState,
|
||
type ReactNode,
|
||
} from "react";
|
||
import { useRouter } from "next/navigation";
|
||
import { realtime, RT_SETTINGS_CHANGED, type RtSettingsData } from "@/lib/realtime";
|
||
|
||
const AllowMessagesContext = createContext(true);
|
||
|
||
/** 前台是否开放私聊/群聊(SSR 初值 + 后台开关热更新) */
|
||
export function useAllowMessages(): boolean {
|
||
return useContext(AllowMessagesContext);
|
||
}
|
||
|
||
/**
|
||
* 根布局注入:关闭消息后前台立即隐藏入口;开启则刷新以拉回会话。
|
||
*/
|
||
export default function MessagesPolicyProvider({
|
||
allowMessages,
|
||
children,
|
||
}: {
|
||
allowMessages: boolean;
|
||
children: ReactNode;
|
||
}) {
|
||
const router = useRouter();
|
||
const [open, setOpen] = useState(allowMessages);
|
||
const openRef = useRef(open);
|
||
openRef.current = open;
|
||
|
||
useEffect(() => {
|
||
setOpen(allowMessages);
|
||
}, [allowMessages]);
|
||
|
||
useEffect(() => {
|
||
return realtime.on<RtSettingsData>(RT_SETTINGS_CHANGED, (data) => {
|
||
if (typeof data?.allow_messages !== "boolean") return;
|
||
if (openRef.current === data.allow_messages) return;
|
||
setOpen(data.allow_messages);
|
||
router.refresh();
|
||
});
|
||
}, [router]);
|
||
|
||
return (
|
||
<AllowMessagesContext.Provider value={open}>{children}</AllowMessagesContext.Provider>
|
||
);
|
||
}
|