π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π style(device): show skeletons while device usage metrics load (#20882)
Rdmclin2f1ee9feb0f3e
diff --git a/src/features/DeviceManager/DeviceCard.tsx b/src/features/DeviceManager/DeviceCard.tsxindex 48c67610d20..7919d1798ce 100644--- a/src/features/DeviceManager/DeviceCard.tsx+++ b/src/features/DeviceManager/DeviceCard.tsx@@ -24,9 +24,9 @@ import { userProfileSelectors } from '@/store/user/selectors';import { refreshDeviceList } from './const';import { DeviceRunningSummaryValue } from './DeviceActivity/RunningUsers';-import { buildHealthTimeline } from './DeviceHealth/buildHealthTimeline';import { useDeviceMetricSeries } from './DeviceHealth/shared';-import UsageMeter, { UsageMeterGroup } from './DeviceHealth/UsageMeter';+import UsageMeter, { UsageMeterGroup, UsageMeterSkeleton } from './DeviceHealth/UsageMeter';+import { latestUsageView } from './DeviceHealth/viewState';import { getDeviceIcon, getPlatformLabel } from './getDeviceIcon';import { openShareDeviceModal } from './ShareDeviceModal';import { useCanEditDevice } from './useCanEditDevice';@@ -122,7 +122,7 @@ const DeviceCard = memo<DeviceCardProps>(({ device, isCurrent, onOpen }) => {// Cards are not virtualized; only cards on screen keep their usage meters polling.const cardRef = useRef<HTMLDivElement>(null);const [inViewport] = useInViewport(cardRef);- const { data } = useDeviceMetricSeries(device.deviceId, !!inViewport);+ const { data, error } = useDeviceMetricSeries(device.deviceId, !!inViewport);const { t } = useTranslation('setting');const { t: tCommon } = useTranslation('common');const canEdit = useCanEditDevice()(device);@@ -264,7 +264,7 @@ const DeviceCard = memo<DeviceCardProps>(({ device, isCurrent, onOpen }) => {device.enroller?.username ||t('workspaceSetting.devices.unknownEnroller');const platformLabel = getPlatformLabel(device.platform, device.architecture);- const latest = data && data.points.length > 0 ? buildHealthTimeline(data).latest : null;+ const usage = latestUsageView({ data, error });return (<div@@ -372,10 +372,17 @@ const DeviceCard = memo<DeviceCardProps>(({ device, isCurrent, onOpen }) => {</Flexbox></div>- <UsageMeterGroup>- <UsageMeter label={t('devices.card.cpuUsage')} value={latest?.cpuPercent} />- <UsageMeter label={t('devices.card.memoryUsage')} value={latest?.memoryPercent} />- </UsageMeterGroup>+ {usage.loading ? (+ <UsageMeterGroup loading>+ <UsageMeterSkeleton />+ <UsageMeterSkeleton />+ </UsageMeterGroup>+ ) : (+ <UsageMeterGroup>+ <UsageMeter label={t('devices.card.cpuUsage')} value={usage.latest?.cpuPercent} />+ <UsageMeter label={t('devices.card.memoryUsage')} value={usage.latest?.memoryPercent} />+ </UsageMeterGroup>+ )}</Flexbox><Flexbox horizontal align={'center'} className={styles.footer} gap={8}>diff --git a/src/features/DeviceManager/DeviceHealth/UsageMeter.tsx b/src/features/DeviceManager/DeviceHealth/UsageMeter.tsxindex 390a823b481..3202a5b74ae 100644--- a/src/features/DeviceManager/DeviceHealth/UsageMeter.tsx+++ b/src/features/DeviceManager/DeviceHealth/UsageMeter.tsx@@ -1,10 +1,13 @@'use client';+import { Flexbox } from '@lobehub/ui';import { Text } from '@lobehub/ui/base-ui';import { useSize } from 'ahooks';import { createStaticStyles, cssVar } from 'antd-style';import { type ReactNode, useRef } from 'react';+import SkeletonBar from '@/components/Skeleton/Bar';+import { formatPercent, usageLevel } from './format';import { USAGE_COLOR, usageTextColor } from './shared';@@ -89,8 +92,36 @@ interface UsageMeterProps {value: number | null | undefined;}-export const UsageMeterGroup = ({ children }: { children: ReactNode }) => (- <div className={styles.group}>{children}</div>+export const UsageMeterGroup = ({+ children,+ loading,+}: {+ children: ReactNode;+ loading?: boolean;+}) => (+ <div aria-busy={loading || undefined} className={styles.group}>+ {children}+ </div>+);++/** A text-line placeholder centered in the 20px line box of the meter's label / value. */+const SkeletonLine = ({ width }: { width: number }) => (+ <Flexbox justify={'center'} style={{ height: 20, minWidth: 0 }}>+ <SkeletonBar height={12} width={width} />+ </Flexbox>+);++/**+ * Placeholder for one {@link UsageMeter} while its reading loads, in the same+ * three grid cells and row height so the card keeps its height on loading β+ * loaded. The label column still settles to the real label's width.+ */+export const UsageMeterSkeleton = () => (+ <>+ <SkeletonLine width={72} />+ <SkeletonBar height={12} />+ <SkeletonLine width={28} />+ </>);/**diff --git a/src/features/DeviceManager/DeviceHealth/viewState.test.ts b/src/features/DeviceManager/DeviceHealth/viewState.test.tsindex b90887f173e..6ad76bbb49a 100644--- a/src/features/DeviceManager/DeviceHealth/viewState.test.ts+++ b/src/features/DeviceManager/DeviceHealth/viewState.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';-import { healthViewState } from './viewState';+import { healthViewState, latestUsageView } from './viewState';const series = (points: number) =>({@@ -27,3 +27,46 @@ describe('healthViewState', () => {expect(healthViewState({ data: series(0) })).toBe('empty');});});++describe('latestUsageView', () => {+ const BUCKET = 5 * 60_000;+ const to = 1000 * BUCKET;+ const reported = {+ bucketMs: BUCKET,+ cpuCount: 8,+ from: to - 12 * BUCKET,+ memoryTotalBytes: 200,+ points: [+ {+ connected: true,+ cpuPercent: 14,+ load1: 1,+ memoryPercent: 35,+ memoryUsedBytes: 70,+ observedAt: to,+ },+ ],+ to,+ };++ // Before the first read resolved, the meters rendered as "no reading" (unlit+ // bars + em dashes) and then jumped to the real values.+ it('is loading while the first read is in flight', () => {+ expect(latestUsageView({})).toEqual({ loading: true });+ });++ it('resolves to the newest reading once loaded', () => {+ const view = latestUsageView({ data: reported });++ expect(view.loading).toBe(false);+ expect(!view.loading && view.latest).toMatchObject({ cpuPercent: 14, memoryPercent: 35 });+ });++ it('resolves to no reading, not loading, for a silent device or a failed read', () => {+ expect(latestUsageView({ data: series(0) })).toEqual({ latest: null, loading: false });+ expect(latestUsageView({ error: new Error('gateway down') })).toEqual({+ latest: null,+ loading: false,+ });+ });+});diff --git a/src/features/DeviceManager/DeviceHealth/viewState.ts b/src/features/DeviceManager/DeviceHealth/viewState.tsindex 9d0c0727817..cf745705b91 100644--- a/src/features/DeviceManager/DeviceHealth/viewState.ts+++ b/src/features/DeviceManager/DeviceHealth/viewState.ts@@ -1,5 +1,7 @@import type { DeviceMetricSeries } from '@lobechat/types';+import { buildHealthTimeline, type HealthUsage } from './buildHealthTimeline';+export type HealthViewState = 'empty' | 'error' | 'loading' | 'ready';/**@@ -17,3 +19,23 @@ export const healthViewState = ({if (!data) return error ? 'error' : 'loading';return data.points.length === 0 ? 'empty' : 'ready';};++export type LatestUsageView = { latest: HealthUsage | null; loading: false } | { loading: true };++/**+ * The current CPU / memory / load reading for the card meters and detail stat+ * cards. Loading only while the first read is in flight; a device that never+ * reported, or a failed read, resolves to no reading (em dashes) instead of a+ * skeleton that never ends.+ */+export const latestUsageView = (state: {+ data?: DeviceMetricSeries;+ error?: unknown;+}): LatestUsageView => {+ if (healthViewState(state) === 'loading') return { loading: true };+ const { data } = state;+ return {+ latest: data && data.points.length > 0 ? buildHealthTimeline(data).latest : null,+ loading: false,+ };+};diff --git a/src/features/DeviceManager/DeviceOverview.tsx b/src/features/DeviceManager/DeviceOverview.tsxindex 09da7f11296..76ef97aee6b 100644--- a/src/features/DeviceManager/DeviceOverview.tsx+++ b/src/features/DeviceManager/DeviceOverview.tsx@@ -29,6 +29,7 @@ import {import type { ReactNode } from 'react';import { useTranslation } from 'react-i18next';+import SkeletonBar from '@/components/Skeleton/Bar';import { createWorkspaceLambdaClient } from '@/libs/trpc/client';import { formatSize } from '@/utils/format';@@ -36,10 +37,10 @@ import Connections from './Connections';import { refreshDeviceList } from './const';import { BoundAgentList, RunningTopicList } from './DeviceActivity';import DeviceHealth, { HealthLegend } from './DeviceHealth';-import { buildHealthTimeline } from './DeviceHealth/buildHealthTimeline';import { formatPercent, usageLevel } from './DeviceHealth/format';import { usageTextColor, useDeviceMetricSeries } from './DeviceHealth/shared';import { UsageBar } from './DeviceHealth/UsageMeter';+import { latestUsageView } from './DeviceHealth/viewState';import { getDeviceIcon, getPlatformLabel } from './getDeviceIcon';const styles = createStaticStyles(({ css }) => ({@@ -178,30 +179,41 @@ const Empty = () => <Text type={'secondary'}>β</Text>;interface StatCardProps {detail?: string;icon: LucideIcon;+ /** First read still in flight: placeholders in the reading's own line boxes. */+ loading?: boolean;title: string;value: number | null | undefined;}/** One reading at a glance: the big share, its absolute size, and the segmented bar. */-const StatCard = ({ detail, icon, title, value }: StatCardProps) => (+const StatCard = ({ detail, icon, loading, title, value }: StatCardProps) => (<DetailCard icon={icon} title={title}>- <Flexbox gap={14}>- <Flexbox horizontal align={'baseline'} distribution={'space-between'} gap={8}>- <Text- fontSize={28}- style={{ color: usageTextColor(usageLevel(value)), lineHeight: 1.2 }}- weight={600}- >- {formatPercent(value)}- </Text>- {detail && (- <Text ellipsis fontSize={13} type={'secondary'}>- {detail}+ {loading ? (+ <Flexbox aria-busy gap={14}>+ <Flexbox justify={'center'} style={{ height: 34 }}>+ <SkeletonBar height={24} width={72} />+ </Flexbox>+ <SkeletonBar height={8} />+ </Flexbox>+ ) : (+ <Flexbox gap={14}>+ <Flexbox horizontal align={'baseline'} distribution={'space-between'} gap={8}>+ <Text+ fontSize={28}+ style={{ color: usageTextColor(usageLevel(value)), lineHeight: 1.2 }}+ weight={600}+ >+ {formatPercent(value)}</Text>- )}+ {detail && (+ <Text ellipsis fontSize={13} type={'secondary'}>+ {detail}+ </Text>+ )}+ </Flexbox>+ <UsageBar height={8} value={value} /></Flexbox>- <UsageBar height={8} value={value} />- </Flexbox>+ )}</DetailCard>);@@ -217,13 +229,14 @@ interface DeviceOverviewProps {*/const DeviceOverview = ({ canOperate, device }: DeviceOverviewProps) => {const { t } = useTranslation('setting');- const { data } = useDeviceMetricSeries(device.deviceId);+ const { data, error } = useDeviceMetricSeries(device.deviceId);+ const usage = latestUsageView({ data, error });+ const latest = usage.loading ? null : usage.latest;const channels = device.channels ?? [];const online = channels.length > 0;const newest = data?.points.at(-1);- const latest = data && data.points.length > 0 ? buildHealthTimeline(data).latest : null;const cores = data?.cpuCount ?? null;const memoryTotal = data?.memoryTotalBytes ?? null;const memoryUsed = newest?.memoryUsedBytes ?? null;@@ -359,11 +372,13 @@ const DeviceOverview = ({ canOperate, device }: DeviceOverviewProps) => {<StatCarddetail={cores ? t('devices.detail.cores', { count: cores }) : undefined}icon={CpuIcon}+ loading={usage.loading}title={t('devices.card.cpuUsage')}value={latest?.cpuPercent}/><StatCardicon={MemoryStickIcon}+ loading={usage.loading}title={t('devices.card.memoryUsage')}value={latest?.memoryPercent}detail={@@ -374,6 +389,7 @@ const DeviceOverview = ({ canOperate, device }: DeviceOverviewProps) => {/><StatCardicon={GaugeIcon}+ loading={usage.loading}title={t('devices.detail.loadUsage')}value={latest?.loadPercent}detail={diff --git a/src/features/DeviceManager/Skeleton.tsx b/src/features/DeviceManager/Skeleton.tsxindex ca6926bfa89..e0d4f9b7e5e 100644--- a/src/features/DeviceManager/Skeleton.tsx+++ b/src/features/DeviceManager/Skeleton.tsx@@ -7,6 +7,7 @@ import { memo } from 'react';import SkeletonBar from '@/components/Skeleton/Bar';import DeviceGrid from './DeviceGrid';+import { UsageMeterSkeleton } from './DeviceHealth/UsageMeter';const styles = createStaticStyles(({ css }) => ({// Same frame and sections as `DeviceCard`.@@ -93,14 +94,6 @@ const Line = ({</Flexbox>);-const MeterRow = () => (- <>- <Line barHeight={12} height={20} width={72} />- <SkeletonBar height={12} />- <Line barHeight={12} height={20} width={28} />- </>-);-const CardSkeleton = () => (<div className={styles.card}><Flexbox className={styles.body} gap={20}>@@ -120,8 +113,8 @@ const CardSkeleton = () => ())}</div><div className={styles.meterGroup}>- <MeterRow />- <MeterRow />+ <UsageMeterSkeleton />+ <UsageMeterSkeleton /></div></Flexbox><Flexbox horizontal align={'center'} className={styles.footer} gap={8}>