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.

5.1 KB
Raw Blame
'use client';

import { Flexbox } from '@lobehub/ui';
import { ChatHeader } from '@lobehub/ui/mobile';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { useMatch, useParams, useSearchParams } from 'react-router';

import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useShowMobileWorkspace } from '@/hooks/useShowMobileWorkspace';
import { useAgentStore } from '@/store/agent';
import { SettingsTabs } from '@/store/global/initialState';
import { mobileHeaderSticky } from '@/styles/mobileHeader';

// Explicit tab → i18n key map. Covers:
// - Cross-namespace entries (subscription / auth).
// - Kebab-case SettingsTabs (e.g. 'service-model') whose URL slug doesn't match the camelCase locale key.
//   Without an explicit entry, `setting:tab.${tab}` would resolve to a missing key and render the raw string.
// - Profile: prefer shorter "Profile" (`auth:profile.title`) over "My Account" (`auth:tab.profile`) on mobile.
const TAB_TITLE_KEY: Partial<Record<SettingsTabs, string>> = {
  [SettingsTabs.Billing]: 'subscription:tab.billing',
  [SettingsTabs.Credits]: 'subscription:tab.credits',
  [SettingsTabs.Labs]: 'labs:title',
  [SettingsTabs.Plans]: 'subscription:tab.plans',
  [SettingsTabs.Profile]: 'auth:profile.title',
  [SettingsTabs.Referral]: 'subscription:tab.referral',
  [SettingsTabs.ServiceModel]: 'setting:tab.serviceModel',
  [SettingsTabs.Stats]: 'auth:tab.stats',
  [SettingsTabs.SystemTools]: 'setting:tab.systemTools',
  [SettingsTabs.Usage]: 'subscription:tab.usage',
};

const WORKSPACE_TAB_TITLE_KEY: Record<string, string> = {
  'audit-log': 'setting:workspaceSetting.tab.auditLog',
  'budget': 'subscription:tab.budget',
  'connector': 'setting:workspaceSetting.tab.connector',
  'credential': 'setting:tab.creds',
  'general': 'setting:workspaceSetting.tab.general',
  'labels': 'setting:workspaceSetting.tab.labels',
  'members': 'setting:workspaceSetting.tab.members',
  'oauth-apps': 'auth:tab.oauthApps',
  'skill': 'setting:workspaceSetting.tab.skill',
  'statistics': 'auth:tab.stats',
  'teams': 'setting:workspaceSetting.tab.teams',
};

/** URL slugs are kebab-case (`subscription-accounts`); `setting:tab.*` keys are camelCase. */
const camelCaseSlug = (slug: string) =>
  slug.replaceAll(/-([a-z])/g, (_, char) => char.toUpperCase());

const Header = memo(() => {
  const { t } = useTranslation(['setting', 'auth', 'labs', 'subscription']);
  const showMobileWorkspace = useShowMobileWorkspace();
  const navigate = useWorkspaceAwareNavigate();
  const params = useParams<{ providerId?: string; tab?: string; workspaceSlug?: string }>();
  const [searchParams] = useSearchParams();
  const workspaceSettingsMatch = useMatch('/:workspaceSlug/settings/:workspaceTab/*');
  // Known tabs are static routes (no `:tab` param), so read the tab off the URL.
  const personalSettingsMatch = useMatch('/settings/:tab/*');

  const isAgentActive = useAgentStore((s) => !!s.activeAgentId);
  // Personal provider details carry the id in the path; the workspace provider
  // page canonicalizes it into the `provider` query param instead.
  const queryProvider = searchParams.get('provider');
  const providerId =
    params.providerId ?? (queryProvider && queryProvider !== 'all' ? queryProvider : undefined);
  const isProvider = providerId && providerId !== 'all';

  const handleBackClick = () => {
    if (isAgentActive && showMobileWorkspace) {
      navigate('/agent');
    } else if (params.providerId && params.providerId !== 'all') {
      navigate('/settings/provider/all', { escape: true });
    } else if (isProvider) {
      // Query-selected provider (workspace form): back to the workspace
      // provider list instead of escaping to personal settings.
      navigate('/settings/provider');
    } else if (params.workspaceSlug) {
      // Workspace tabs go back to the workspace settings menu; the menu goes
      // back to the workspace. Workspace-aware paths keep the slug.
      navigate(workspaceSettingsMatch ? '/settings' : '/');
    } else {
      navigate('/me/settings', { escape: true });
    }
  };

  const workspaceTab = workspaceSettingsMatch?.params.workspaceTab;
  const tab = (params.tab ?? personalSettingsMatch?.params.tab ?? workspaceTab) as
    SettingsTabs | undefined;
  const tabTitleKey = tab
    ? (WORKSPACE_TAB_TITLE_KEY[workspaceTab ?? ''] ??
      TAB_TITLE_KEY[tab] ??
      `setting:tab.${camelCaseSlug(tab)}`)
    : 'setting:tab.all';
  // i18next's strict key union rejects dynamic strings. `Parameters<typeof t>[0]` would push TS
  // onto the wrong overload and infer the return as `unknown`, so we fall back to `as any`.
  // Unknown keys surface visibly as raw text, which is acceptable.
  const tabTitle = t(tabTitleKey as any);

  return (
    <ChatHeader
      showBackButton
      style={mobileHeaderSticky}
      center={
        <ChatHeader.Title
          title={
            <Flexbox horizontal align={'center'} gap={8}>
              <span style={{ lineHeight: 1.2 }}>{isProvider ? providerId : tabTitle}</span>
            </Flexbox>
          }
        />
      }
      onBackClick={handleBackClick}
    />
  );
});

export default Header;