Skip to content

Commit f12d393

Browse files
fix(ui): unify loading and refresh feedback across clients (#9561)
Co-authored-by: Julius Marminge <[email protected]>
1 parent be53bbd commit f12d393

42 files changed

Lines changed: 506 additions & 183 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
formatUsd,
1212
makeWindow,
1313
} from "@t3tools/shared/usageFormat";
14-
import { useMemo, useState } from "react";
14+
import { useMemo, useRef, useState } from "react";
1515
import { Platform, Pressable, RefreshControl, ScrollView, View } from "react-native";
1616
import { useSafeAreaInsets } from "react-native-safe-area-context";
1717

@@ -91,10 +91,8 @@ export function UsageRouteScreen() {
9191
[isPast24Hours, merged.daily, merged.hourly],
9292
);
9393

94-
// The pull spinner tracks re-scans of environments that have answered
95-
// before. The initial scan renders its own placeholder, and an unreachable
96-
// environment stays pending forever — neither may pin the spinner on.
97-
const refreshingUsage = environments.some((entry) => entry.isPending && entry.summary !== null);
94+
const [refreshingUsage, setRefreshingUsage] = useState(false);
95+
const refreshingRef = useRef(false);
9896
const showingLimits = tab === "limits";
9997
const selectWindow = (days: number) => {
10098
setWindowSelection({
@@ -103,17 +101,22 @@ export function UsageRouteScreen() {
103101
});
104102
};
105103
const refreshWindow = () => {
104+
if (refreshingRef.current) return;
106105
const nextWindow = makeWindow(windowDays, undefined, isPast24Hours ? "hour" : "day");
107106
if (
108-
nextWindow.sinceDay === window.sinceDay &&
109-
nextWindow.untilDay === window.untilDay &&
110-
nextWindow.sinceTime === window.sinceTime &&
111-
nextWindow.untilTime === window.untilTime
107+
nextWindow.sinceDay !== window.sinceDay ||
108+
nextWindow.untilDay !== window.untilDay ||
109+
nextWindow.sinceTime !== window.sinceTime ||
110+
nextWindow.untilTime !== window.untilTime
112111
) {
113-
refresh();
114-
} else {
115112
setWindowSelection({ days: windowDays, window: nextWindow });
116113
}
114+
refreshingRef.current = true;
115+
setRefreshingUsage(true);
116+
void refresh(nextWindow).finally(() => {
117+
refreshingRef.current = false;
118+
setRefreshingUsage(false);
119+
});
117120
};
118121

119122
return (

‎apps/mobile/src/state/usage.ts‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
type UsageSummary,
1717
type UsageSummaryInput,
1818
} from "@t3tools/contracts";
19-
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
19+
import { refreshUsage } from "@t3tools/client-runtime/state/usage";
2020
import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge";
2121
import * as Option from "effect/Option";
2222
import { AsyncResult, Atom } from "effect/unstable/reactivity";
@@ -72,7 +72,7 @@ export interface UsageView {
7272
* improve by waiting on them, so they must not read as "still reporting".
7373
*/
7474
readonly isPartial: boolean;
75-
readonly refresh: () => void;
75+
readonly refresh: (input?: UsageSummaryInput) => Promise<void>;
7676
}
7777

7878
export function useUsage(input: UsageSummaryInput): UsageView {
@@ -98,26 +98,17 @@ export function useUsage(input: UsageSummaryInput): UsageView {
9898
const atom = usageByWindowAtom(windowKey);
9999
const environments = useAtomValue(atom);
100100

101-
// Refreshing only the derived atom would re-read the per-environment SWR
102-
// queries within their stale window and change nothing. Refresh each
103-
// environment's query so pull-to-refresh always rescans.
104-
//
105-
// Each environment refetches model pricing first, so a model released since
106-
// its last daily fetch gets priced by the rescan. The rescan runs whether or
107-
// not the refetch succeeds: an offline environment still recounts tokens.
108-
const refresh = useCallback(() => {
109-
const input = JSON.parse(windowKey) as UsageSummaryInput;
110-
for (const environment of environments) {
111-
const { environmentId } = environment;
112-
const query = serverEnvironment.usageSummary({ environmentId, input });
113-
void runAtomCommand(
114-
appAtomRegistry,
115-
serverEnvironment.refreshUsageRates,
116-
{ environmentId, input: {} },
117-
{ reportFailure: false },
118-
).finally(() => appAtomRegistry.refresh(query));
119-
}
120-
}, [environments, windowKey]);
101+
const refresh = useCallback(
102+
(nextInput?: UsageSummaryInput) =>
103+
refreshUsage({
104+
registry: appAtomRegistry,
105+
server: serverEnvironment,
106+
presentations: environmentPresentations,
107+
environmentIds: environments.map(({ environmentId }) => environmentId),
108+
input: nextInput ?? (JSON.parse(windowKey) as UsageSummaryInput),
109+
}),
110+
[environments, windowKey],
111+
);
121112

122113
const merged = useMemo(() => {
123114
const answered: EnvironmentUsage[] = environments.flatMap((environment) =>

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { RefreshIcon } from "~/components/ui/refresh-icon";
12
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
23
import {
34
isAtomCommandInterrupted,
45
squashAtomCommandFailure,
56
} from "@t3tools/client-runtime/state/runtime";
67
import type { ContextMenuItem, EnvironmentId, VcsRef, ThreadId } from "@t3tools/contracts";
78
import { LegendList, type LegendListRef } from "@legendapp/list/react";
8-
import { ChevronDownIcon, GitBranchIcon, RefreshCwIcon, SearchIcon } from "lucide-react";
9+
import { ChevronDownIcon, GitBranchIcon, SearchIcon } from "lucide-react";
910
import {
1011
useCallback,
1112
useDeferredValue,
@@ -864,7 +865,7 @@ export function BranchToolbarBranchSelector({
864865
className="flex cursor-pointer items-center justify-between gap-3 border-t border-border/60 px-3 py-2 text-xs"
865866
>
866867
<span className="flex min-w-0 items-center gap-1.5 font-medium text-muted-foreground">
867-
<RefreshCwIcon aria-hidden="true" className="size-3 shrink-0 opacity-70" />
868+
<RefreshIcon aria-hidden="true" className="size-3 shrink-0 opacity-70" />
868869
<span className="truncate">Start from origin</span>
869870
</span>
870871
<Switch

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RefreshIcon } from "~/components/ui/refresh-icon";
12
import { useAtomValue } from "@effect/atom-react";
23
import type { FileDiffContentsLoader } from "@pierre/diffs";
34
import { useParams } from "@tanstack/react-router";
@@ -17,7 +18,6 @@ import {
1718
Columns2Icon,
1819
FolderTreeIcon,
1920
PilcrowIcon,
20-
RefreshCwIcon,
2121
Rows3Icon,
2222
SearchIcon,
2323
TextWrapIcon,
@@ -766,9 +766,7 @@ export default function DiffPanel({
766766
/>
767767
}
768768
>
769-
<RefreshCwIcon
770-
className={cn("size-3.5", branchDiffPreview.isPending && "animate-spin")}
771-
/>
769+
<RefreshIcon className="size-3.5" refreshing={branchDiffPreview.isPending} />
772770
</TooltipTrigger>
773771
<TooltipPopup side="top">
774772
{branchDiffPreview.isPending ? "Refreshing diff…" : "Refresh diff"}

‎apps/web/src/components/LegacySidebar.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Spinner } from "~/components/ui/spinner";
12
import {
23
ArchiveIcon,
34
ArrowUpDownIcon,
@@ -6,7 +7,6 @@ import {
67
ContainerIcon,
78
FolderPlusIcon,
89
Globe2Icon,
9-
LoaderIcon,
1010
SearchIcon,
1111
SquarePenIcon,
1212
TerminalIcon,
@@ -2648,7 +2648,7 @@ function LocalSecondaryStatus() {
26482648
variant="default"
26492649
className="rounded-2xl border-border/40 bg-accent/40 text-muted-foreground"
26502650
>
2651-
<LoaderIcon className="animate-spin" />
2651+
<Spinner />
26522652
<AlertTitle className="text-xs font-medium text-foreground">
26532653
Connecting {connecting.join(", ")}
26542654
</AlertTitle>

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RefreshIcon } from "~/components/ui/refresh-icon";
12
import type {
23
ApprovalRequestId,
34
AssistantCitation,
@@ -789,7 +790,6 @@ import {
789790
LockIcon,
790791
LockOpenIcon,
791792
PenLineIcon,
792-
RotateCcwIcon,
793793
SparklesIcon,
794794
XIcon,
795795
} from "lucide-react";
@@ -5273,7 +5273,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
52735273
/>
52745274
}
52755275
>
5276-
<RotateCcwIcon />
5276+
<RefreshIcon />
52775277
</TooltipTrigger>
52785278
<TooltipPopup
52795279
side="top"
@@ -5352,7 +5352,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
53525352
/>
53535353
}
53545354
>
5355-
<RotateCcwIcon />
5355+
<RefreshIcon />
53565356
</TooltipTrigger>
53575357
<TooltipPopup
53585358
side="top"
@@ -5425,7 +5425,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
54255425
/>
54265426
}
54275427
>
5428-
<RotateCcwIcon />
5428+
<RefreshIcon />
54295429
</TooltipTrigger>
54305430
<TooltipPopup
54315431
side="top"

‎apps/web/src/components/chat/ComposerActivityStatus.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { LoaderCircleIcon } from "lucide-react";
2-
import { observeVisibleAnimation } from "../../lib/visibleAnimation";
1+
import { Spinner } from "~/components/ui/spinner";
2+
33
import { threadSyncLabel, type ThreadSyncPhase } from "../../threadSync";
44
import { ComposerBanner } from "./ComposerBanner";
55

66
export function ComposerActivityRow({ phase }: { readonly phase: ThreadSyncPhase }) {
77
return (
88
<ComposerBanner.Row>
9-
<ComposerBanner.Icon ref={observeVisibleAnimation}>
10-
<LoaderCircleIcon className="motion-safe:visible-animate-spin" />
9+
<ComposerBanner.Icon>
10+
<Spinner />
1111
</ComposerBanner.Icon>
1212
<ComposerBanner.Content>
1313
<span

‎apps/web/src/components/chat/ComposerServerUpdateStatus.tsx‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { Spinner } from "~/components/ui/spinner";
12
import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
2-
import { CircleAlertIcon, DownloadIcon, LoaderCircleIcon } from "lucide-react";
3+
import { CircleAlertIcon, DownloadIcon } from "lucide-react";
34
import { useId, useState } from "react";
45

5-
import { observeVisibleAnimation } from "../../lib/visibleAnimation";
66
import { serverUpdateStageLabel } from "../ServerUpdateAction";
77
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
88
import { ComposerBanner } from "./ComposerBanner";
@@ -13,13 +13,7 @@ export function ComposerServerUpdateIcon({
1313
readonly status: ServerUpdateState["status"];
1414
}) {
1515
if (status === "running") {
16-
return (
17-
<LoaderCircleIcon
18-
aria-hidden
19-
ref={observeVisibleAnimation}
20-
className="motion-safe:visible-animate-spin"
21-
/>
22-
);
16+
return <Spinner aria-hidden />;
2317
}
2418
if (status === "failed") {
2519
return <CircleAlertIcon aria-hidden className="text-error" />;

‎apps/web/src/components/clerk/ClerkUserProfilePage.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RefreshCwIcon } from "lucide-react";
1+
import { RefreshIcon } from "~/components/ui/refresh-icon";
2+
23
import type { ReactNode } from "react";
34

45
import { cn } from "../../lib/utils";
@@ -55,7 +56,7 @@ export function ClerkUserProfileRefreshButton({
5556
disabled={disabled || isPending}
5657
onClick={onClick}
5758
>
58-
<RefreshCwIcon aria-hidden="true" className={cn("size-3.5", isPending && "animate-spin")} />
59+
<RefreshIcon aria-hidden="true" className="size-3.5" refreshing={isPending} />
5960
Refresh
6061
</Button>
6162
);

‎apps/web/src/components/files/FileBreadcrumbs.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { RefreshIcon } from "~/components/ui/refresh-icon";
2+
import { Spinner } from "~/components/ui/spinner";
13
import type { EnvironmentId } from "@t3tools/contracts";
2-
import { ArrowLeftIcon, ChevronRightIcon, LoaderCircleIcon, RotateCwIcon } from "lucide-react";
4+
import { ArrowLeftIcon, ChevronRightIcon } from "lucide-react";
35
import { useEffect, useMemo, useState } from "react";
46

57
import { PierreEntryIcon } from "~/components/chat/PierreEntryIcon";
@@ -124,12 +126,12 @@ function BreadcrumbMenuContent(props: {
124126
<MenuGroup key={props.directoryPath}>
125127
{entriesQuery.isPending && entriesQuery.data === null ? (
126128
<MenuItem disabled>
127-
<LoaderCircleIcon className="animate-spin" />
129+
<Spinner />
128130
Loading folder…
129131
</MenuItem>
130132
) : entriesQuery.error && entriesQuery.data === null ? (
131133
<MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
132-
<RotateCwIcon />
134+
<RefreshIcon refreshing={entriesQuery.isPending} />
133135
<span className="min-w-0 flex-1 truncate">Retry loading folder</span>
134136
</MenuItem>
135137
) : !directoryAvailable && !entriesTruncated ? (
@@ -177,7 +179,7 @@ function BreadcrumbMenuContent(props: {
177179
<>
178180
<MenuSeparator />
179181
<MenuItem closeOnClick={false} onClick={entriesQuery.refresh}>
180-
<RotateCwIcon />
182+
<RefreshIcon refreshing={entriesQuery.isPending} />
181183
Refresh failed — retry
182184
</MenuItem>
183185
</>

0 commit comments

Comments
 (0)