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.

3.0 KB
Raw Blame
'use client';

import { isDesktop } from '@lobechat/const';
import type { DeviceListItem, DeviceScope } from '@lobechat/types';
import { Button, Flexbox, Icon, Text } from '@lobehub/ui';
import { ArrowLeftIcon, MonitorXIcon } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import AsyncBoundary from '@/components/AsyncBoundary';
import { useDeviceList } from '@/store/device';
import { useElectronStore } from '@/store/electron';

import DeviceDetail from './DeviceDetail';
import { deviceDetailKey, getDeviceListBoundary } from './deviceListBoundary';
import { DeviceDetailSkeleton } from './Skeleton';

interface DeviceDetailPageProps {
  deviceId: string;
  /** Leave the detail for the list. Receives the resolved device when there is one. */
  onBack: (device?: DeviceListItem) => void;
  scope: DeviceScope;
}

/**
 * Resolves a `:sub` device id against the shared device list, then renders the
 * detail page — or a not-found state for a removed device or a stale link.
 */
const DeviceDetailPage = memo<DeviceDetailPageProps>(({ deviceId, onBack, scope }) => {
  const { t } = useTranslation('setting');
  const deviceList = useDeviceList();
  const { data, error, isConfirmed, mutate } = deviceList;
  const device = data?.find((d) => d.deviceId === deviceId && d.scope === scope);
  // A persisted copy missing this device may just be stale: only the server
  // can say it is gone.
  const boundary = getDeviceListBoundary(deviceList, !device);

  // The machine the user is on right now (desktop only) — personal pool only.
  const useFetchDeviceInfo = useElectronStore((s) => s.useFetchGatewayDeviceInfo);
  const gatewayDeviceInfo = useElectronStore((s) => s.gatewayDeviceInfo);
  useFetchDeviceInfo();
  const isCurrent =
    scope === 'personal' && isDesktop && !!device && gatewayDeviceInfo?.deviceId === deviceId;

  const notFound = (
    <Flexbox align={'center'} gap={12} paddingBlock={64} style={{ textAlign: 'center' }}>
      <Icon icon={MonitorXIcon} size={32} style={{ opacity: 0.45 }} />
      <Text fontSize={16} weight={600}>
        {t('devices.detail.notFound.title')}
      </Text>
      <Text type={'secondary'}>{t('devices.detail.notFound.desc')}</Text>
      <Button
        icon={<Icon icon={ArrowLeftIcon} />}
        style={{ marginBlockStart: 8 }}
        onClick={() => onBack()}
      >
        {t('devices.detail.back')}
      </Button>
    </Flexbox>
  );

  return (
    <AsyncBoundary
      data={boundary.data}
      empty={notFound}
      error={error}
      errorVariant={'block'}
      isEmpty={!device}
      isLoading={boundary.isLoading}
      loading={<DeviceDetailSkeleton />}
      onRetry={() => mutate()}
    >
      {device && (
        <DeviceDetail
          device={device}
          isConfirmed={isConfirmed}
          isCurrent={isCurrent}
          key={deviceDetailKey(device.deviceId, isConfirmed)}
          onBack={() => onBack(device)}
        />
      )}
    </AsyncBoundary>
  );
});

DeviceDetailPage.displayName = 'DeviceDetailPage';

export default DeviceDetailPage;