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.

12.4 KB
Raw Blame
import { isDesktop } from '@lobechat/const';
import { Avatar } from '@lobehub/ui';
import {
  AppWindowIcon,
  BellIcon,
  Brain,
  Building2,
  ChartColumnBigIcon,
  ChartPieIcon,
  Coins,
  ContainerIcon,
  CreditCard,
  Database,
  EllipsisIcon,
  FlaskConical,
  GaugeIcon,
  HandCoins,
  Info,
  KeyboardIcon,
  KeyIcon,
  Map,
  MessagesSquareIcon,
  MonitorSmartphoneIcon,
  PaletteIcon,
  ScrollText,
  Sparkles,
  TagIcon,
  Trash2,
  Users,
  UsersRound,
  WebhookIcon,
  ZapIcon,
} from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { usePermission } from '@/hooks/usePermission';
import { useElectronStore } from '@/store/electron';
import { electronSyncSelectors } from '@/store/electron/selectors';
import {
  featureFlagsSelectors,
  serverConfigSelectors,
  useServerConfigStore,
} from '@/store/serverConfig';
import { useUserStore } from '@/store/user';
import { labPreferSelectors, userProfileSelectors } from '@/store/user/selectors';
import { WorkspaceSettingsTabs } from '@/types/workspaceSettings';

export enum WorkspaceSettingsGroupKey {
  Account = 'account',
  Admin = 'admin',
  Agent = 'agent',
  Connection = 'connection',
  General = 'general',
  Resource = 'resource',
  Subscription = 'subscription',
  System = 'system',
}

export interface WorkspaceSettingCategoryItem {
  icon: any;
  key: WorkspaceSettingsTabs;
  label: string;
}

export interface WorkspaceSettingCategoryGroup {
  items: WorkspaceSettingCategoryItem[];
  key: WorkspaceSettingsGroupKey;
  title: string;
}

export const useWorkspaceSettingCategory = (): WorkspaceSettingCategoryGroup[] => {
  const { t } = useTranslation('setting');
  const { t: tAuth } = useTranslation('auth');
  const { t: tLabs } = useTranslation('labs');
  const { t: tSubscription } = useTranslation('subscription');
  const { allowed: canManageWorkspace } = usePermission('manage_settings');
  const { allowed: canViewBilling } = usePermission('view_billing');
  // API keys act as the member who issued them, so the tab follows the same
  // member-level gate the server enforces: `API_KEY_*` is granted from Member
  // up, never to Viewer. Without this the tab leads to a list request that
  // immediately 403s.
  const { allowed: canCreateContent } = usePermission('create_content');
  const enableOAuthApps = useUserStore(labPreferSelectors.enableOAuthApps);
  const { hideDocs } = useServerConfigStore(featureFlagsSelectors);
  const enableWebhook = useServerConfigStore(serverConfigSelectors.enableWebhook);
  const [avatar, username] = useUserStore((s) => [
    userProfileSelectors.userAvatar(s),
    userProfileSelectors.nickName(s),
  ]);
  const remoteServerUrl = useElectronStore(electronSyncSelectors.remoteServerUrl);

  const avatarUrl = useMemo(() => {
    if (!avatar) return undefined;
    if (isDesktop && avatar.startsWith('/') && remoteServerUrl) {
      return remoteServerUrl + avatar;
    }
    return avatar;
  }, [avatar, remoteServerUrl]);

  return useMemo(
    () =>
      [
        // Account-level settings (profile / appearance / hotkeys)
        // follow the user, not the workspace. They are mirrored here so members
        // can reach them without leaving the workspace; the pages are the
        // personal ones.
        // Leads the sidebar so the user's own identity stays on top, like the
        // personal settings sidebar.
        {
          items: [
            {
              icon: avatarUrl ? (
                <Avatar avatar={avatarUrl} shape={'square'} size={26} />
              ) : undefined,
              key: WorkspaceSettingsTabs.Profile,
              label: username || tAuth('tab.profile'),
            },
            {
              icon: PaletteIcon,
              key: WorkspaceSettingsTabs.Appearance,
              label: t('tab.appearance'),
            },
            {
              icon: KeyboardIcon,
              key: WorkspaceSettingsTabs.Hotkey,
              label: t('tab.hotkey'),
            },
            // Conversation preferences (reply language, chat assist, send key…)
            // act on the member's own chat surface, so every role configures
            // them here — the workspace Service Model page stays admin-only.
            {
              icon: MessagesSquareIcon,
              key: WorkspaceSettingsTabs.Conversation,
              label: t('tab.conversation'),
            },
          ],
          key: WorkspaceSettingsGroupKey.Account,
          title: t('group.profile'),
        },
        {
          items: [
            {
              icon: Building2,
              key: WorkspaceSettingsTabs.General,
              label: t('workspaceSetting.tab.general'),
            },
            {
              icon: Users,
              key: WorkspaceSettingsTabs.Members,
              label: t('workspaceSetting.tab.members'),
            },
            {
              icon: UsersRound,
              key: WorkspaceSettingsTabs.Teams,
              label: t('workspaceSetting.tab.teams'),
            },
            {
              icon: BellIcon,
              key: WorkspaceSettingsTabs.Notification,
              label: t('tab.notification'),
            },
            {
              icon: ChartColumnBigIcon,
              key: WorkspaceSettingsTabs.Stats,
              label: tAuth('tab.stats'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.General,
          title: t('workspaceSetting.group.workspace'),
        },
        {
          items: [
            {
              icon: Map,
              key: WorkspaceSettingsTabs.Plans,
              label: tSubscription('tab.plans'),
            },
            {
              icon: ChartPieIcon,
              key: WorkspaceSettingsTabs.Usage,
              label: t('tab.usage'),
            },
            // Credits / Billing are readable by Admin-or-higher; the pages
            // themselves keep the money-moving controls (top-up, payment
            // methods, plan changes) behind the narrower subscription gate.
            canViewBilling && {
              icon: Coins,
              key: WorkspaceSettingsTabs.Credits,
              label: tSubscription('tab.credits'),
            },
            // Spend governance (budget pools + member caps) — admin task,
            // same visibility gate as the other money pages.
            canViewBilling && {
              icon: HandCoins,
              key: WorkspaceSettingsTabs.Budget,
              label: tSubscription('tab.budget'),
            },
            canViewBilling && {
              icon: CreditCard,
              key: WorkspaceSettingsTabs.Billing,
              label: tSubscription('tab.billing'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.Subscription,
          title: t('group.subscription'),
        },
        {
          items: [
            // AI provider config (keys/endpoints) is shared workspace infra —
            // Admin-or-higher, hidden from members entirely.
            canManageWorkspace && {
              icon: Brain,
              key: WorkspaceSettingsTabs.Provider,
              label: t('tab.provider'),
            },
            // Service-model preferences steer the shared workspace model
            // policy — Admin-or-higher, hidden from members like Provider.
            canManageWorkspace && {
              icon: Sparkles,
              key: WorkspaceSettingsTabs.ServiceModel,
              label: t('tab.serviceModel'),
            },
            // Label registry is readable by everyone; the page itself keeps
            // management actions behind the admin gate (disabled, not hidden).
            {
              icon: TagIcon,
              key: WorkspaceSettingsTabs.Labels,
              label: t('workspaceSetting.tab.labels'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.Agent,
          title: t('workspaceSetting.group.agent'),
        },
        enableWebhook && {
          items: [
            // Every member sees the workspace's event subscriptions, so it is
            // clear what fires the shared agents; managing one is per row.
            {
              icon: ZapIcon,
              key: WorkspaceSettingsTabs.Automations,
              label: t('tab.automations'),
            },
            // The workspace's agents' webhooks; made on each agent's page, managed here too.
            {
              icon: WebhookIcon,
              key: WorkspaceSettingsTabs.Webhooks,
              label: t('tab.webhooks'),
            },
          ] as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.Connection,
          title: t('group.connection'),
        },
        // What the workspace's agents run on and spend: machines, coding-agent
        // subscriptions, code environments and the drive. Each item keeps its
        // own gate; the group only puts the shared capacity in one place.
        {
          items: [
            {
              icon: MonitorSmartphoneIcon,
              key: WorkspaceSettingsTabs.Devices,
              label: t('tab.devices'),
            },
            // The coding-agent accounts are shared capacity every agent routes
            // onto, so taking one in or out of rotation is an admin decision.
            canManageWorkspace && {
              icon: GaugeIcon,
              key: WorkspaceSettingsTabs.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: WorkspaceSettingsTabs.Environments,
              label: t('tab.environmentsAndCreds'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.Resource,
          title: t('group.resource'),
        },
        // The Admin group is available to Admin and Owner.
        canManageWorkspace && {
          items: [
            {
              icon: Database,
              key: WorkspaceSettingsTabs.Storage,
              label: t('tab.storage'),
            },
            {
              icon: ScrollText,
              key: WorkspaceSettingsTabs.AuditLog,
              label: t('workspaceSetting.tab.auditLog'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.Admin,
          title: t('workspaceSetting.group.admin'),
        },
        // System & developer sits last and starts collapsed, mirroring the
        // personal sidebar. Storage stays in Admin because it is
        // workspace-scoped there. Trash is visible to every role — the server
        // shows owners the whole bin and members only what they deleted.
        // Advanced and Labs are user preferences; API Key / OAuth apps keep
        // their gates.
        {
          items: [
            {
              icon: Trash2,
              key: WorkspaceSettingsTabs.Trash,
              label: t('tab.trash'),
            },
            {
              icon: EllipsisIcon,
              key: WorkspaceSettingsTabs.Advanced,
              label: t('tab.advanced'),
            },
            canCreateContent && {
              icon: KeyIcon,
              key: WorkspaceSettingsTabs.APIKey,
              label: tAuth('tab.apikey'),
            },
            enableOAuthApps && {
              icon: AppWindowIcon,
              key: WorkspaceSettingsTabs.OAuthApps,
              label: tAuth('tab.oauthApps'),
            },
            {
              icon: FlaskConical,
              key: WorkspaceSettingsTabs.Labs,
              label: tLabs('title'),
            },
            !hideDocs && {
              icon: Info,
              key: WorkspaceSettingsTabs.About,
              label: t('tab.about'),
            },
          ].filter(Boolean) as WorkspaceSettingCategoryItem[],
          key: WorkspaceSettingsGroupKey.System,
          title: t('group.systemDeveloper'),
        },
      ].filter(Boolean) as WorkspaceSettingCategoryGroup[],
    [
      t,
      tAuth,
      tLabs,
      tSubscription,
      enableOAuthApps,
      canManageWorkspace,
      canViewBilling,
      canCreateContent,
      hideDocs,
      enableWebhook,
      avatarUrl,
      username,
    ],
  );
};