Files
jiang13-bbs/frontend/components/MessagesPolicyProvider.tsx
freefire 4b93739468 feat: 后台增加开放消息开关
关闭后前台隐藏私聊/群聊入口并由后端拦截;同步去掉公告页重复计数胶囊。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 04:16:10 +08:00

53 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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>
);
}