Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

6.1 KB
Raw Blame
'use client';

import {
  ActionIcon,
  Avatar,
  createStaticStyles,
  cssVar,
  DropdownMenu,
  Flexbox,
  Icon,
  Tag,
  Text,
} from '@lobehub/ui';
import {
  ArrowLeftIcon,
  GlobeIcon,
  MessageSquareTextIcon,
  MoreHorizontal,
  SparklesIcon,
} from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import ShareButton from '@/business/client/features/PageShare/ShareButton';
import { AutoSaveHint } from '@/features/EditorCanvas';
import { FavoriteButton } from '@/features/Favorites';
import NavHeader from '@/features/NavHeader';
import ToggleRightPanelButton from '@/features/RightPanel/ToggleRightPanelButton';
import { usePermission } from '@/hooks/usePermission';
import { useHeaderActionSize } from '@/styles/density';

import { useDocumentComments } from '../DocumentComments/context';
import EditingIndicator from '../EditingIndicator';
import { useIsHtmlPage } from '../HtmlPageBody';
import { usePageAgentPanelControl } from '../RightPanel/OverrideContext';
import { selectors, usePageEditorStore } from '../store';
import Breadcrumb from './Breadcrumb';
import PresenceStack from './PresenceStack';
import { useMenu } from './useMenu';

const styles = createStaticStyles(({ css }) => ({
  /** The two sidebars share one segmented switch; the lit segment is the open one. */
  panelSwitch: css`
    padding: 2px;
    border: 1px solid ${cssVar.colorBorderSecondary};
    border-radius: ${cssVar.borderRadius};
  `,
}));

const Header = memo(() => {
  const headerActionSize = useHeaderActionSize(true);
  const { t } = useTranslation('file');
  const [documentId, emoji, title, parentId, onBack] = usePageEditorStore((s) => [
    s.documentId,
    s.emoji,
    s.title,
    s.parentId,
    s.onBack,
  ]);
  const rightPanelMode = usePageEditorStore(selectors.rightPanelMode);
  const isHtmlPage = useIsHtmlPage(documentId);
  const { allowed: hasEditPermission } = usePermission('edit_own_content');
  const { expand: showPageAgentPanel, toggle: togglePageAgentPanel } = usePageAgentPanelControl();
  const { menuItems } = useMenu();
  // Mirror the gate inside PageEditor/RightPanel: copilot is a document-editing
  // surface, so viewers can't open it; History is read-only and stays available
  // to everyone. Without this guard the button toggles the store, then disappears
  // via `hideWhenExpanded` while the panel refuses to open — a no-op control.
  const canExpandRightPanel = hasEditPermission || rightPanelMode === 'history';
  // Comments exist only for workspace documents with a comments panel to show
  // them in; the provider is absent otherwise.
  const comments = useDocumentComments();
  const [isCommentsPanelOpen, setCommentsPanelOpen] = usePageEditorStore((s) => [
    s.commentsPanelOpen,
    s.setCommentsPanelOpen,
  ]);

  return (
    <NavHeader
      left={
        <>
          {onBack && <ActionIcon icon={ArrowLeftIcon} size={headerActionSize} onClick={onBack} />}
          {/* Breadcrumb - show when page has a parent folder */}
          {parentId && <Breadcrumb />}
          {/* Show icon and title only when there's no parent folder */}
          {!parentId && (
            <>
              {/* Icon */}
              {emoji && <Avatar avatar={emoji} shape={'square'} size={28} />}
              {/* Title */}
              <Text ellipsis style={{ marginLeft: 4 }} weight={500}>
                {title || t('pageEditor.titlePlaceholder')}
              </Text>
            </>
          )}
          {documentId && (
            <FavoriteButton
              icon={emoji ?? null}
              path={`/page/${documentId}`}
              // An untitled page is loaded too; only `undefined` means still loading.
              snapshotReady={title !== undefined}
              targetId={documentId}
              title={title || t('pageEditor.titlePlaceholder')}
              type={'page'}
            />
          )}
          {/* An artifact's body is never edited here, so a save status would only
              ever read "latest version"; name what the page is instead. */}
          {isHtmlPage ? (
            <Tag icon={<Icon icon={GlobeIcon} />} style={{ marginLeft: 6 }}>
              {t('pageEditor.artifactTag')}
            </Tag>
          ) : (
            documentId && <AutoSaveHint documentId={documentId} style={{ marginLeft: 6 }} />
          )}
        </>
      }
      right={
        <>
          <EditingIndicator />
          <PresenceStack />
          {documentId && <ShareButton documentId={documentId} />}
          {/* Three-dot menu */}
          <DropdownMenu
            iconSpaceMode="group"
            items={menuItems}
            placement="bottomRight"
            popupProps={{
              style: {
                minWidth: 200,
              },
            }}
          >
            <ActionIcon icon={MoreHorizontal} size={headerActionSize} />
          </DropdownMenu>
          {(comments?.panelAvailable || canExpandRightPanel) && (
            // The framed segment control only earns its frame with two
            // segments; a lone toggle (a personal page has no comments, a
            // viewer has no copilot) stays a plain icon.
            <Flexbox
              horizontal
              gap={2}
              className={
                comments?.panelAvailable && canExpandRightPanel ? styles.panelSwitch : undefined
              }
            >
              {comments?.panelAvailable && (
                <ActionIcon
                  active={isCommentsPanelOpen}
                  icon={MessageSquareTextIcon}
                  size={headerActionSize}
                  title={t('pageEditor.comments.toggle')}
                  onClick={() => setCommentsPanelOpen(!isCommentsPanelOpen)}
                />
              )}
              {canExpandRightPanel && (
                <ToggleRightPanelButton
                  showActive
                  expand={showPageAgentPanel}
                  icon={SparklesIcon}
                  title={t('pageEditor.copilot.toggle')}
                  onToggle={() => togglePageAgentPanel()}
                />
              )}
            </Flexbox>
          )}
        </>
      }
    />
  );
});

export default Header;