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.

2.5 KB
Raw Blame
'use client';

import { HotkeyEnum } from '@lobechat/const/hotkeys';
import { ActionIcon, type ActionIconProps } from '@lobehub/ui';
import { PanelRightClose, PanelRightOpen } from 'lucide-react';
import { type ReactNode } from 'react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/selectors';
import { useHeaderActionSize } from '@/styles/density';

export const TOGGLE_BUTTON_ID = 'toggle_right_panel_button';

interface ToggleRightPanelButtonProps {
  /**
   * Override the panel's expanded state. When provided together with `onToggle`,
   * the button uses these instead of the global `showRightPanel` store.
   */
  expand?: boolean;
  hideWhenExpanded?: boolean;
  icon?: ActionIconProps['icon'];
  onToggle?: () => void;
  showActive?: boolean;
  size?: ActionIconProps['size'];
  title?: ReactNode;
}

const ToggleRightPanelButton = memo<ToggleRightPanelButtonProps>(
  ({ title, showActive, icon, hideWhenExpanded, size, expand: expandProp, onToggle }) => {
    const headerActionSize = useHeaderActionSize(true);
    const [globalExpand, globalToggle, isStatusInit] = useGlobalStore((s) => [
      systemStatusSelectors.showRightPanel(s),
      s.toggleRightPanel,
      systemStatusSelectors.isStatusInit(s),
    ]);
    const hotkey = useUserStore(settingsSelectors.getHotkeyById(HotkeyEnum.ToggleRightPanel));

    const { t } = useTranslation(['chat', 'hotkey']);

    const expand = expandProp ?? globalExpand;
    const handleClick = onToggle ?? (() => globalToggle());

    // Defer render until status hydrates when relying on the global store —
    // toggleRightPanel is a no-op while !isStatusInit and clicks would be
    // silently dropped. Callers that pass `expand`+`onToggle` override this.
    if (expandProp === undefined && !isStatusInit) return null;

    if (hideWhenExpanded && expand) return null;
    return (
      <ActionIcon
        active={showActive ? expand : undefined}
        icon={icon || (expand ? PanelRightClose : PanelRightOpen)}
        id={TOGGLE_BUTTON_ID}
        size={size || headerActionSize}
        title={title || t('toggleRightPanel.title', { ns: 'hotkey' })}
        tooltipProps={{
          hotkey,
          placement: 'bottom',
        }}
        onClick={handleClick}
      />
    );
  },
);

export default ToggleRightPanelButton;