feat: 后台增加开放消息开关

关闭后前台隐藏私聊/群聊入口并由后端拦截;同步去掉公告页重复计数胶囊。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-17 04:16:10 +08:00
parent 9a55c2f6f2
commit 4b93739468
18 changed files with 275 additions and 91 deletions

View File

@@ -0,0 +1,52 @@
"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>
);
}