非管理员隐藏头部

This commit is contained in:
2026-01-16 18:07:42 +08:00
parent bac3a97720
commit 40305bd4ca
3 changed files with 55 additions and 4 deletions

View File

@@ -1,13 +1,15 @@
'use client'
import { useEffect, useRef } from 'react'
import { useSelector } from '@/context/app-context'
/**
* IframePostMessage
* 页面加载完成后向父页面发送一次消息,并每 5 秒发送一次。
* 仅当当前窗口运行在 iframe 中时才发送。
* 仅当当前窗口运行在 iframe 中时才发送本地缓存消息
*/
const IframePostMessage = () => {
const initializedRef = useRef(false)
const isCurrentWorkspaceManager = useSelector(s => s.isCurrentWorkspaceManager)
/**
* sendMessage
@@ -47,6 +49,27 @@ const IframePostMessage = () => {
}
}
/**
* applyHeaderVisibility
* 使用原生 JS 根据管理员状态隐藏或显示头部容器:
* - 非管理员owner/admin 之外)隐藏
* - 管理员显示
* 不区分是否在 iframe 中,统一处理。
*/
const applyHeaderVisibility = () => {
try {
if (typeof document === 'undefined') return
const headerEl = document.querySelector('.app-global-header') as HTMLElement | null
if (!headerEl) return
if (!isCurrentWorkspaceManager)
headerEl.style.display = 'none'
else
headerEl.style.display = ''
} catch {
// 忽略可能的运行时错误
}
}
useEffect(() => {
if (initializedRef.current) return
initializedRef.current = true
@@ -74,7 +97,14 @@ const IframePostMessage = () => {
}
}, [])
/**
* 根据管理员状态应用头部可见性
*/
useEffect(() => {
applyHeaderVisibility()
}, [isCurrentWorkspaceManager])
return null
}
export default IframePostMessage
export default IframePostMessage