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.

9.8 KB
Raw Blame
import { ENABLE_TOOL_CHANNEL_SETTINGS } from '@lobechat/business-const';
import { isDesktop } from '@lobechat/const';
import { Avatar } from '@lobehub/ui';
import {
  AppWindowIcon,
  BellIcon,
  Brain,
  BrainCircuit,
  ChartColumnBigIcon,
  ChartPieIcon,
  ContainerIcon,
  CreditCard,
  Database,
  EllipsisIcon,
  EthernetPort,
  FlaskConical,
  GaugeIcon,
  Gift,
  Info,
  KeyboardIcon,
  KeyIcon,
  Map,
  MessagesSquareIcon,
  MonitorSmartphoneIcon,
  PaletteIcon,
  Sparkles,
  TagIcon,
  TerminalSquare,
  Trash2,
  WebhookIcon,
  Wrench,
  ZapIcon,
} from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useElectronStore } from '@/store/electron';
import { electronSyncSelectors } from '@/store/electron/selectors';
import { SettingsTabs } from '@/store/global/initialState';
import {
  featureFlagsSelectors,
  serverConfigSelectors,
  useServerConfigStore,
} from '@/store/serverConfig';
import { useUserStore } from '@/store/user';
import { labPreferSelectors } from '@/store/user/selectors';
import { userProfileSelectors } from '@/store/user/slices/auth/selectors';

export enum SettingsGroupKey {
  Account = 'account',
  Agent = 'agent',
  Connection = 'connection',
  Resource = 'resource',
  Subscription = 'subscription',
  System = 'system',
}

export interface CategoryItem {
  /** Override the navigation URL. When omitted, Body derives the URL from `key`. */
  href?: string;
  icon: any;
  key: SettingsTabs;
  label: string;
}

export interface CategoryGroup {
  items: CategoryItem[];
  key: SettingsGroupKey;
  title: string;
}

export const useCategory = () => {
  const { t } = useTranslation('setting');
  const { t: tAuth } = useTranslation('auth');
  const { t: tLabs } = useTranslation('labs');
  const { t: tSubscription } = useTranslation('subscription');
  const mobile = useServerConfigStore((s) => s.isMobile);
  const { hideDocs, showApiKeyManage, showProvider } = useServerConfigStore(featureFlagsSelectors);
  const [avatar, username] = useUserStore((s) => [
    userProfileSelectors.userAvatar(s),
    userProfileSelectors.nickName(s),
  ]);
  const remoteServerUrl = useElectronStore(electronSyncSelectors.remoteServerUrl);
  const enableOAuthApps = useUserStore(labPreferSelectors.enableOAuthApps);
  const enableWebhook = useServerConfigStore(serverConfigSelectors.enableWebhook);
  // Advanced holds gateway mode and desktop diagnostics; on the web without
  // gateway mode it would be an empty page.
  const enableGatewayMode = useServerConfigStore(serverConfigSelectors.enableGatewayMode);
  const showAdvanced = isDesktop || enableGatewayMode;

  const avatarUrl = useMemo(() => {
    if (!avatar) return undefined;
    if (isDesktop && avatar.startsWith('/') && remoteServerUrl) {
      return remoteServerUrl + avatar;
    }
    return avatar;
  }, [avatar, remoteServerUrl]);
  const enableBusinessFeatures = useServerConfigStore(serverConfigSelectors.enableBusinessFeatures);
  const categoryGroups: CategoryGroup[] = useMemo(() => {
    const groups: CategoryGroup[] = [];

    // Account group β€” who the user is and how the app behaves for them.
    const accountItems: CategoryItem[] = [
      {
        icon: avatarUrl ? <Avatar avatar={avatarUrl} shape={'square'} size={26} /> : undefined,
        key: SettingsTabs.Profile,
        label: username || tAuth('tab.profile'),
      },
      {
        icon: PaletteIcon,
        key: SettingsTabs.Appearance,
        label: t('tab.appearance'),
      },
      {
        icon: MessagesSquareIcon,
        key: SettingsTabs.Conversation,
        label: t('tab.conversation'),
      },
      !mobile && {
        icon: KeyboardIcon,
        key: SettingsTabs.Hotkey,
        label: t('tab.hotkey'),
      },
      (enableBusinessFeatures || isDesktop) && {
        icon: BellIcon,
        key: SettingsTabs.Notification,
        label: t('tab.notification'),
      },
      {
        icon: ChartColumnBigIcon,
        key: SettingsTabs.Stats,
        label: tAuth('tab.stats'),
      },
    ].filter(Boolean) as CategoryItem[];

    groups.push({
      items: accountItems,
      key: SettingsGroupKey.Account,
      title: t('group.profile'),
    });

    // Personal subscription / billing items sit right under the account they
    // belong to. Always shown when business features are enabled β€” workspace
    // settings live under a separate `/:workspaceSlug/settings/*` surface and
    // never share this sidebar.
    if (enableBusinessFeatures) {
      const subscriptionItems: CategoryItem[] = [
        { icon: Map, key: SettingsTabs.Plans, label: tSubscription('tab.plans') },
        // One entry for usage and balance: `/settings/credits` redirects into it.
        {
          icon: ChartPieIcon,
          key: SettingsTabs.Usage,
          label: tSubscription('tab.usage'),
        },
        { icon: CreditCard, key: SettingsTabs.Billing, label: tSubscription('tab.billing') },
        { icon: Gift, key: SettingsTabs.Referral, label: tSubscription('tab.referral') },
      ];

      groups.push({
        items: subscriptionItems,
        key: SettingsGroupKey.Subscription,
        title: t('group.subscription'),
      });
    }

    // Agent group β€” which models an agent uses and what it can do.
    const agentItems: CategoryItem[] = [
      // Provider settings should not depend on Advanced tools: new users may need
      // non-LobeHub providers, and desktop users often bring their own API keys.
      showProvider && {
        icon: Brain,
        key: SettingsTabs.Provider,
        label: t('tab.provider'),
      },
      {
        icon: Sparkles,
        key: SettingsTabs.ServiceModel,
        label: t('tab.serviceModel'),
      },
      ENABLE_TOOL_CHANNEL_SETTINGS && {
        icon: Wrench,
        key: SettingsTabs.Tools,
        label: t('tab.tools'),
      },
      {
        icon: BrainCircuit,
        key: SettingsTabs.Memory,
        label: t('tab.memory'),
      },
      {
        icon: TagIcon,
        key: SettingsTabs.Labels,
        label: t('tab.labels'),
      },
    ].filter(Boolean) as CategoryItem[];

    groups.push({
      items: agentItems,
      key: SettingsGroupKey.Agent,
      title: t('group.aiConfig'),
    });

    // Connection group β€” the outside services agents can reach, whoever owns
    // the binding. Every entry here is already the user's own.
    const connectionItems: CategoryItem[] = [
      // What connectors fire on their own: the event subscriptions agents made
      // through them. Connectors themselves are managed in the Marketplace.
      enableWebhook && {
        icon: ZapIcon,
        key: SettingsTabs.Automations,
        label: t('tab.automations'),
      },
      // URLs outside systems call to wake an agent; made on each agent's page, managed here too.
      enableWebhook && {
        icon: WebhookIcon,
        key: SettingsTabs.Webhooks,
        label: t('tab.webhooks'),
      },
    ].filter(Boolean) as CategoryItem[];

    // Every entry is conditional, so a self-hosted deployment may have none.
    if (connectionItems.length > 0)
      groups.push({
        items: connectionItems,
        key: SettingsGroupKey.Connection,
        title: t('group.connection'),
      });

    // Resource group β€” where agents run.
    const resourceItems: CategoryItem[] = [
      {
        icon: MonitorSmartphoneIcon,
        key: SettingsTabs.Devices,
        label: t('tab.devices'),
      },
      // The coding-agent subscriptions this member's devices signed in to:
      // the capacity their agents are balanced onto.
      {
        icon: GaugeIcon,
        key: SettingsTabs.SubscriptionAccounts,
        label: t('tab.subscriptionAccounts'),
      },
      // Environments & Vault, always there: what a run starts from and
      // the secrets it reads, in one place. An environment is a definition,
      // written down before anything runs it, so it needs no drive.
      {
        icon: ContainerIcon,
        key: SettingsTabs.Environments,
        label: t('tab.environmentsAndCreds'),
      },
      isDesktop && {
        icon: EthernetPort,
        key: SettingsTabs.Proxy,
        label: t('tab.proxy'),
      },
      isDesktop && {
        icon: TerminalSquare,
        key: SettingsTabs.SystemTools,
        label: t('tab.systemTools'),
      },
    ].filter(Boolean) as CategoryItem[];

    groups.push({
      items: resourceItems,
      key: SettingsGroupKey.Resource,
      title: t('group.resource'),
    });

    // System & developer group β€” low-frequency, so the sidebar starts it
    // collapsed. API Key has a single home whichever flag turned it on.
    const systemItems: CategoryItem[] = [
      {
        icon: Database,
        key: SettingsTabs.Storage,
        label: t('tab.storage'),
      },
      {
        icon: Trash2,
        key: SettingsTabs.Trash,
        label: t('tab.trash'),
      },
      showAdvanced && {
        icon: EllipsisIcon,
        key: SettingsTabs.Advanced,
        label: t('tab.advanced'),
      },
      showApiKeyManage && {
        icon: KeyIcon,
        key: SettingsTabs.APIKey,
        label: tAuth('tab.apikey'),
      },
      enableOAuthApps && {
        icon: AppWindowIcon,
        key: SettingsTabs.OAuthApps,
        label: tAuth('tab.oauthApps'),
      },
      {
        icon: FlaskConical,
        key: SettingsTabs.Labs,
        label: tLabs('title'),
      },
      !hideDocs && {
        icon: Info,
        key: SettingsTabs.About,
        label: t('tab.about'),
      },
    ].filter(Boolean) as CategoryItem[];

    groups.push({
      items: systemItems,
      key: SettingsGroupKey.System,
      title: t('group.systemDeveloper'),
    });

    return groups;
  }, [
    t,
    tAuth,
    tLabs,
    tSubscription,
    enableBusinessFeatures,
    hideDocs,
    mobile,
    showApiKeyManage,
    showProvider,
    enableOAuthApps,
    enableWebhook,
    showAdvanced,
    avatarUrl,
    username,
  ]);

  return categoryGroups;
};