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.

8.2 KB
Raw Blame
'use client';

import { ActionIcon, Button, Empty, Flexbox, SearchBar, Select, Text } from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import { RefreshCwIcon } from 'lucide-react';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';

import SkeletonBar from '@/components/Skeleton/Bar';
import VisibilityFilter, {
  matchesVisibilityFilter,
  type VisibilityFilterValue,
} from '@/components/VisibilityFilter';
import { cacheScope } from '@/libs/replica';
import { agentQuotaSelectors, type QuotaAccount, useAgentQuotaStore } from '@/store/agentQuota';

import AccountDetailPanel from './AccountDetailPanel';
import AccountRow, { AccountListHeader, LIST_MIN_WIDTH, PROVIDER_NAME } from './AccountRow';
import {
  isSubscriptionProvider,
  matchesKeyword,
  sortAccounts,
  SUBSCRIPTION_PROVIDERS,
  type SubscriptionProvider,
} from './accountView';
import QuotaCalendar from './QuotaCalendar';

type ProviderAccount = QuotaAccount & { provider: SubscriptionProvider };

const ALL = 'all';

/**
 * Every coding-agent subscription the workspace's devices have reported, as a
 * list with their live quota — the shared capacity agents are balanced onto.
 * Accounts register themselves when a CLI login reports quota; this page only
 * decides which of them stay in rotation and what they are called.
 *
 * In a workspace the shared accounts and the caller's own private ones list
 * together, the private ones marked with a lock (as on the Devices page).
 */
const SubscriptionAccounts = () => {
  const { t } = useTranslation('setting');
  const useFetchQuotaAccounts = useAgentQuotaStore((s) => s.useFetchQuotaAccounts);
  const readQuotaReadings = useAgentQuotaStore((s) => s.readQuotaReadings);
  const { error, isValidating, revalidate } = useFetchQuotaAccounts();
  const [refreshing, setRefreshing] = useState(false);
  const scope = cacheScope.use();
  const confirmed = useAgentQuotaStore(agentQuotaSelectors.isAccountsConfirmed(scope));
  const loaded = useAgentQuotaStore(agentQuotaSelectors.accounts);
  const [provider, setProvider] = useState<SubscriptionProvider | typeof ALL>(ALL);
  const [keyword, setKeyword] = useState('');
  const [visibilityFilter, setVisibilityFilter] = useState<VisibilityFilterValue>('all');
  const [openId, setOpenId] = useState<string>();
  const accounts = sortAccounts(
    (loaded ?? []).filter((a): a is ProviderAccount => isSubscriptionProvider(a.provider)),
  );
  const present = SUBSCRIPTION_PROVIDERS.filter((p) => accounts.some((a) => a.provider === p));
  // A provider filter that no longer has accounts falls back to everything.
  const active = provider !== ALL && present.includes(provider) ? provider : ALL;
  // Only workspace accounts carry a visibility, so the filter shows only in a
  // workspace, where shared and the caller's private accounts list together.
  const isPrivateAccount = (a: ProviderAccount) => !!a.workspaceId && a.visibility === 'private';
  const showVisibilityFilter = accounts.some((a) => !!a.workspaceId);
  const visible = accounts.filter(
    (a) =>
      (active === ALL || a.provider === active) &&
      (!showVisibilityFilter || matchesVisibilityFilter(isPrivateAccount(a), visibilityFilter)) &&
      matchesKeyword(a, keyword),
  );
  const openAccount = accounts.find((a) => a.id === openId);
  // The summary counts what the filters leave, so it moves with them.
  const inRotation = visible.filter((a) => a.enabled !== false).length;

  // The meters read each account's own readings, which the list sync does not
  // touch: refreshing re-reads them too, or a newer sample would never show.
  const refresh = async () => {
    setRefreshing(true);
    try {
      await Promise.allSettled([
        revalidate(),
        ...accounts.map((account) => readQuotaReadings(account.id, { fresh: true })),
      ]);
    } finally {
      setRefreshing(false);
    }
  };

  // A hydrated empty list may be stale: only the server's answer settles "none yet".
  const settling = !confirmed && !error && accounts.length === 0;

  const body = settling ? (
    <Flexbox gap={4}>
      {Array.from({ length: 5 }, (_, i) => (
        <SkeletonBar height={56} key={i} radius={cssVar.borderRadius} />
      ))}
    </Flexbox>
  ) : error && accounts.length === 0 ? (
    // A failed load must not read as "no accounts" — they would look identical.
    <Flexbox align={'center'} gap={12} paddingBlock={40}>
      <Empty description={t('subscriptionAccounts.loadFailed')} />
      <Button onClick={() => void revalidate()}>{t('retry', { ns: 'common' })}</Button>
    </Flexbox>
  ) : accounts.length === 0 ? (
    <Empty
      description={t('subscriptionAccounts.empty.desc')}
      style={{ paddingBlock: 40 }}
      title={t('subscriptionAccounts.empty.title')}
    />
  ) : (
    <Flexbox gap={16}>
      {visible.length > 0 && (
        <QuotaCalendar accounts={visible} activeId={openId} onOpen={setOpenId} />
      )}
      {/* The settings pane hides horizontal overflow, so a narrow pane scrolls the
          list sideways rather than clipping the switch and menu off the row. */}
      <div style={{ overflowX: 'auto' }}>
        <Flexbox gap={2} style={{ minWidth: LIST_MIN_WIDTH }}>
          <AccountListHeader />
          {visible.map((account) => (
            <AccountRow
              account={account}
              active={account.id === openId}
              key={account.id}
              onOpen={() => setOpenId(account.id)}
            />
          ))}
          {visible.length === 0 && (
            <Empty
              description={t('subscriptionAccounts.filterEmpty')}
              style={{ paddingBlock: 40 }}
            />
          )}
        </Flexbox>
      </div>
    </Flexbox>
  );

  const summary = (
    <Text fontSize={13} type={'secondary'}>
      {accounts.length > 0 &&
        t('subscriptionAccounts.summary', { count: visible.length, inRotation })}
    </Text>
  );

  return (
    <Flexbox gap={16} width={'100%'}>
      {/* The summary and the toolbar share one row. */}
      <Flexbox horizontal align={'center'} gap={12} justify={'space-between'} wrap={'wrap'}>
        {summary}
        <Flexbox horizontal align={'center'} gap={8}>
          {/* Nothing to search or filter in an empty list. Filters sit on the right
              as a dropdown: the provider list grows with every coding plan we
              support, which a segmented control cannot hold. */}
          {accounts.length > 0 && (
            <>
              <SearchBar
                allowClear
                placeholder={t('subscriptionAccounts.searchPlaceholder')}
                style={{ width: 240 }}
                value={keyword}
                variant={'filled'}
                onChange={(e) => setKeyword(e.target.value)}
              />
              {showVisibilityFilter && (
                <VisibilityFilter value={visibilityFilter} onChange={setVisibilityFilter} />
              )}
              <Select
                style={{ minWidth: 168 }}
                value={active}
                variant={'filled'}
                options={[
                  {
                    label: t('subscriptionAccounts.filter.allProviders'),
                    value: ALL,
                  },
                  ...present.map((p) => ({
                    label: PROVIDER_NAME[p],
                    value: p,
                  })),
                ]}
                onChange={(value) => setProvider(value as SubscriptionProvider | typeof ALL)}
              />
            </>
          )}
          <ActionIcon
            loading={isValidating || refreshing}
            title={t('subscriptionAccounts.refresh')}
            onClick={() => void refresh()}
            icon={RefreshCwIcon}
            // A row that runs short of width shrinks its children; the icon keeps its square.
            style={{ flex: 'none' }}
          />
        </Flexbox>
      </Flexbox>
      <Flexbox horizontal gap={20} style={{ minWidth: 0 }}>
        <Flexbox flex={1} style={{ minWidth: 0 }}>
          {body}
        </Flexbox>
        {openAccount && (
          <AccountDetailPanel
            account={openAccount}
            key={openAccount.id}
            onClose={() => setOpenId(undefined)}
          />
        )}
      </Flexbox>
    </Flexbox>
  );
};

export default SubscriptionAccounts;