Skip to content

Commit 5e665a3

Browse files
committed
fix(cursor): read usage from the Cursor SDK account
1 parent eeb6e23 commit 5e665a3

40 files changed

Lines changed: 190 additions & 1415 deletions

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,11 @@ export function UsageLimitsSection({
408408
now,
409409
failedLabels,
410410
selectedEnvironmentIds,
411-
cursorPrompt,
412411
wide = false,
413412
}: {
414413
readonly now: number;
415414
readonly failedLabels: readonly string[];
416415
readonly selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null;
417-
readonly cursorPrompt?: ReactNode;
418416
readonly wide?: boolean;
419417
}) {
420418
const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom);
@@ -426,29 +424,22 @@ export function UsageLimitsSection({
426424
const notices = collectLimitNotices(selected);
427425
const externalLinks = collectExternalUsageLinks(selected);
428426
const colors = useProviderColors();
429-
const cursorPromptAt =
430-
Math.max(
431-
pools.findIndex((pool) => pool.driver === "codex"),
432-
pools.findIndex((pool) => pool.driver === "claudeAgent"),
433-
) + 1;
434427
return (
435428
<View className="gap-6">
436429
{pools.length === 0 &&
437430
notices.length === 0 &&
438431
failedLabels.length === 0 &&
439-
!cursorPrompt &&
440432
externalLinks.length === 0 ? (
441433
<Text className="py-12 text-center text-base text-foreground-muted">
442434
{selected.size === 0
443435
? "Select an environment to see limits."
444436
: "No provider on the selected environments reports subscription limits."}
445437
</Text>
446438
) : null}
447-
{pools.map((pool, index) => {
439+
{pools.map((pool) => {
448440
const windows = displayLimitWindows(pool);
449441
return (
450442
<Fragment key={pool.driver}>
451-
{index === cursorPromptAt ? cursorPrompt : null}
452443
<View className="gap-3">
453444
<View className="flex-row items-center gap-2 px-1">
454445
<ProviderIcon provider={pool.driver} size={18} />
@@ -478,7 +469,6 @@ export function UsageLimitsSection({
478469
</Fragment>
479470
);
480471
})}
481-
{cursorPromptAt === pools.length ? cursorPrompt : null}
482472
{externalLinks.map((link) => (
483473
<View key={link.url} className="gap-3 rounded-xl border border-border-subtle p-4">
484474
<Text className="text-base font-t3-medium text-foreground">{link.label}</Text>

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,16 @@ export function useRefreshLimits(
345345
.filter(([environmentId]) => failedIds.has(environmentId))
346346
.map(([, presentation]) => presentation.entry.target.label);
347347

348-
const refresh = (afterPending: boolean) =>
348+
const refresh = () =>
349349
Promise.all(
350-
connectedIds.map((environmentId) =>
351-
refreshEnvironmentLimits(registry, environmentId, { afterPending }),
352-
),
350+
connectedIds.map((environmentId) => refreshEnvironmentLimits(registry, environmentId)),
353351
);
354352
// Android RefreshControl needs a true-to-false cycle to stop, even with nothing to probe.
355353
const refreshManually = async () => {
356354
if (refreshingRef.current) return;
357355
refreshingRef.current = true;
358356
setRefreshing(true);
359-
await refresh(false).finally(() => {
357+
await refresh().finally(() => {
360358
refreshingRef.current = false;
361359
setRefreshing(false);
362360
});
@@ -366,6 +364,5 @@ export function useRefreshLimits(
366364
refreshing,
367365
failedLabels,
368366
refresh: refreshManually,
369-
refreshAfterEnable: () => refresh(true),
370367
};
371368
}

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

Lines changed: 3 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { Button } from "../../components/ui/Button";
21
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
32
import { ChatGptUsageSummary } from "./ChatGptUsageSummary";
43
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
54
import { EnvironmentId, USAGE_CONTRACT_VERSION, type UsageProviderKind } from "@t3tools/contracts";
65
import { type RouteProp, useIsFocused, useRoute } from "@react-navigation/native";
7-
import { cursorKeychainAccessEnvironments } from "@t3tools/client-runtime/state/usage";
86
import {
97
updatingProvidersLabel,
108
usageEnvironmentProgress,
@@ -38,12 +36,9 @@ import { SegmentedControl } from "../../components/ui/SegmentedControl";
3836
import { useReduceMotionEnabled } from "../../lib/appState";
3937
import { AppText as Text } from "../../components/ui/AppText";
4038
import { useWorkspaceSettledContentWidth } from "../layout/workspace-content-width";
41-
import { ProviderIcon } from "../../components/ProviderIcon";
4239
import { cn } from "../../lib/cn";
4340
import { SettingsScreen } from "../settings/components/SettingsScreen";
4441
import { useUsage, type EnvironmentUsageStatus } from "../../state/usage";
45-
import { serverEnvironment } from "../../state/server";
46-
import { useAtomCommand } from "../../state/use-atom-command";
4742
import { SettingsSection } from "../settings/components/SettingsSection";
4843
import { UsageDailyChart } from "./UsageDailyChart";
4944
import { toggleUsageEnvironment } from "./usageEnvironmentSelection";
@@ -72,7 +67,6 @@ const WIDE_CHART_HEIGHT = 260;
7267
const WIDE_WIDTH = 600;
7368
const MAX_CONTENT_WIDTH = 1080;
7469
const providerLabel = (provider: UsageProviderKind) => PROVIDER_LABEL[provider];
75-
const CURSOR_KEYCHAIN_COPY = "Requires access to your Cursor login in macOS Keychain.";
7670

7771
export function UsageRouteScreen(props: { readonly workspace?: boolean }) {
7872
const route = useRoute<RouteProp<{ Usage: { tab?: string } | undefined }, "Usage">>();
@@ -114,11 +108,6 @@ function UsageScreen(props: {
114108
);
115109
const isFocused = useIsFocused();
116110
const limits = useRefreshLimits(selectedEnvironmentIds, isFocused && view === "limits");
117-
const cursorAccessEnvironments = cursorKeychainAccessEnvironments(selectedEnvironments);
118-
const refreshAfterCursorEnable = () => {
119-
void refresh();
120-
void limits.refreshAfterEnable();
121-
};
122111
const sourceMessages = usageSourceMessages(
123112
selectedEnvironments.flatMap((environment) => environment.summary?.sources ?? []),
124113
);
@@ -183,16 +172,7 @@ function UsageScreen(props: {
183172
isPast24Hours,
184173
timeZone: window.timeZone,
185174
};
186-
const providers = (
187-
<ProviderSection
188-
merged={merged}
189-
metric={metric}
190-
cursorAccessEnvironments={cursorAccessEnvironments}
191-
showCursorEnvironment={selectedEnvironments.length > 1}
192-
onCursorEnabled={refreshAfterCursorEnable}
193-
compact={wide}
194-
/>
195-
);
175+
const providers = <ProviderSection merged={merged} metric={metric} compact={wide} />;
196176

197177
const showEnvironmentFilter = environments.length > 0 || selectedEnvironmentIds !== null;
198178
const selectEnvironment = (value: string) => {
@@ -291,14 +271,6 @@ function UsageScreen(props: {
291271
failedLabels={limits.failedLabels}
292272
selectedEnvironmentIds={selectedEnvironmentIds}
293273
wide={wide}
294-
cursorPrompt={
295-
cursorAccessEnvironments.length > 0 ? (
296-
<CursorEnableLimits
297-
environments={cursorAccessEnvironments}
298-
onEnabled={refreshAfterCursorEnable}
299-
/>
300-
) : null
301-
}
302274
/>
303275
) : (
304276
<>
@@ -429,105 +401,6 @@ function UsageUpdating({
429401
);
430402
}
431403

432-
function CursorEnableAction({
433-
environmentId,
434-
label,
435-
onEnabled,
436-
buttonText = "Enable",
437-
}: {
438-
readonly environmentId: EnvironmentId;
439-
readonly label: string;
440-
readonly onEnabled: () => void;
441-
readonly buttonText?: string;
442-
}) {
443-
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
444-
label: "enable Cursor account usage",
445-
});
446-
const [pending, setPending] = useState(false);
447-
const enable = async () => {
448-
setPending(true);
449-
const result = await updateSettings({
450-
environmentId,
451-
input: { patch: { cursorKeychainUsageEnabled: true } },
452-
}).finally(() => setPending(false));
453-
if (result._tag === "Success") onEnabled();
454-
};
455-
return (
456-
<Button
457-
label={buttonText}
458-
tone="primary"
459-
accessibilityLabel={`Enable Cursor usage from ${label}`}
460-
accessibilityHint={CURSOR_KEYCHAIN_COPY}
461-
disabled={pending}
462-
onPress={() => void enable()}
463-
loading={pending}
464-
/>
465-
);
466-
}
467-
468-
function CursorEnableRow({
469-
environmentId,
470-
label,
471-
showEnvironment,
472-
bordered,
473-
onEnabled,
474-
}: {
475-
readonly environmentId: EnvironmentId;
476-
readonly label: string;
477-
readonly showEnvironment: boolean;
478-
readonly bordered: boolean;
479-
readonly onEnabled: () => void;
480-
}) {
481-
const colors = useProviderColors();
482-
return (
483-
<View
484-
className={cn(
485-
"flex-row items-center justify-between gap-3 p-4",
486-
bordered && "border-t border-border-subtle",
487-
)}
488-
>
489-
<View className="min-w-0 flex-1 flex-row items-center gap-2">
490-
<View className="size-2.5 rounded-full" style={{ backgroundColor: colors.cursor }} />
491-
<Text className="shrink text-lg text-foreground">
492-
Cursor{showEnvironment ? ` · ${label}` : ""}
493-
</Text>
494-
</View>
495-
<CursorEnableAction environmentId={environmentId} label={label} onEnabled={onEnabled} />
496-
</View>
497-
);
498-
}
499-
500-
function CursorEnableLimits({
501-
environments,
502-
onEnabled,
503-
}: {
504-
readonly environments: readonly EnvironmentUsageStatus[];
505-
readonly onEnabled: () => void;
506-
}) {
507-
return (
508-
<View className="gap-3">
509-
<View className="flex-row items-center gap-2 px-1">
510-
<ProviderIcon provider="cursor" size={18} />
511-
<Text className="text-base font-t3-medium text-foreground">Cursor</Text>
512-
</View>
513-
<View className="items-start gap-3 rounded-[24px] border-continuous bg-grouped-card p-4">
514-
<Text className="text-xs text-foreground-muted">{CURSOR_KEYCHAIN_COPY}</Text>
515-
<View className="flex-row flex-wrap gap-2">
516-
{environments.map((environment) => (
517-
<CursorEnableAction
518-
key={environment.environmentId}
519-
environmentId={environment.environmentId}
520-
label={environment.label}
521-
buttonText={environments.length > 1 ? `Enable on ${environment.label}` : "Enable"}
522-
onEnabled={onEnabled}
523-
/>
524-
))}
525-
</View>
526-
</View>
527-
</View>
528-
);
529-
}
530-
531404
function ChartCard(props: {
532405
readonly merged: MergedUsage;
533406
readonly days: readonly string[];
@@ -632,54 +505,21 @@ function UsageSummary(props: { readonly merged: MergedUsage; readonly metric: Us
632505
function ProviderSection(props: {
633506
readonly merged: MergedUsage;
634507
readonly metric: UsageChartMetric;
635-
readonly cursorAccessEnvironments: readonly EnvironmentUsageStatus[];
636-
readonly showCursorEnvironment: boolean;
637-
readonly onCursorEnabled: () => void;
638508
/** Drops the share bar so the list fits beside the chart. */
639509
readonly compact: boolean;
640510
}) {
641511
const { merged, metric } = props;
642512
const colors = useProviderColors();
643-
if (merged.providers.length === 0 && props.cursorAccessEnvironments.length === 0) return null;
513+
if (merged.providers.length === 0) return null;
644514

645515
// Hermes lacks toSorted.
646516
const ordered = [...merged.providers].sort((a, b) =>
647517
metric === "cost" ? b.costUsd - a.costUsd : b.totalTokens - a.totalTokens,
648518
);
649-
const rows: Array<
650-
| { readonly kind: "usage"; readonly provider: (typeof ordered)[number] }
651-
| { readonly kind: "enable"; readonly environment: EnvironmentUsageStatus }
652-
> = ordered.map((provider) => ({ kind: "usage", provider }));
653-
const cursorInsertAt =
654-
Math.max(
655-
ordered.findIndex((provider) => provider.provider === "codex"),
656-
ordered.findIndex((provider) => provider.provider === "claude"),
657-
) + 1;
658-
rows.splice(
659-
cursorInsertAt,
660-
0,
661-
...props.cursorAccessEnvironments.map((environment) => ({
662-
kind: "enable" as const,
663-
environment,
664-
})),
665-
);
666519

667520
return (
668521
<SettingsSection title="Providers">
669-
{rows.map((row, index) => {
670-
if (row.kind === "enable") {
671-
return (
672-
<CursorEnableRow
673-
key={`enable:${row.environment.environmentId}`}
674-
environmentId={row.environment.environmentId}
675-
label={row.environment.label}
676-
showEnvironment={props.showCursorEnvironment}
677-
bordered={index > 0}
678-
onEnabled={props.onCursorEnabled}
679-
/>
680-
);
681-
}
682-
const provider = row.provider;
522+
{ordered.map((provider, index) => {
683523
const share = metric === "cost" ? provider.costShare : provider.tokenShare;
684524
return (
685525
<View

‎apps/mobile/src/features/usage/usageLimitsRefresh.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const limitsRefreshFailedAtom = Atom.family((environmentId: EnvironmentId) =>
1616
export async function refreshEnvironmentLimits(
1717
registry: AtomRegistry.AtomRegistry,
1818
environmentId: EnvironmentId,
19-
options: { readonly automatic?: boolean; readonly afterPending?: boolean } = {},
19+
options: { readonly automatic?: boolean } = {},
2020
): Promise<void> {
2121
// One refresh runs per environment at a time, so results land in the order
2222
// they were started and the newest outcome always wins.
@@ -30,7 +30,6 @@ export async function refreshEnvironmentLimits(
3030
{ reportFailure: false },
3131
),
3232
options.automatic,
33-
options.afterPending,
3433
);
3534
if (result === undefined) return;
3635
registry.set(limitsRefreshFailedAtom(environmentId), result._tag === "Failure");

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
type UsageSummary,
77
type UsageSummaryInput,
88
} from "@t3tools/contracts";
9-
import {
10-
needsCursorKeychainAccess,
11-
refreshUsage,
12-
usageSummaryInput,
13-
} from "@t3tools/client-runtime/state/usage";
9+
import { refreshUsage, usageSummaryInput } from "@t3tools/client-runtime/state/usage";
1410
import { resolveUsageAccess } from "@t3tools/client-runtime/state/usage-access";
1511
import { mergeUsage, type EnvironmentUsage, type MergedUsage } from "@t3tools/shared/usageMerge";
1612
import { AsyncResult, Atom } from "effect/reactivity";
@@ -29,7 +25,6 @@ export interface EnvironmentUsageStatus {
2925
readonly canReadDiagnostics: boolean;
3026
readonly error: string | null;
3127
readonly summary: UsageSummary | null;
32-
readonly needsCursorKeychainAccess: boolean;
3328
}
3429

3530
const usageByWindowAtom = Atom.family((windowKey: string) =>
@@ -52,7 +47,6 @@ const usageByWindowAtom = Atom.family((windowKey: string) =>
5247
isConnected: presentation.connection.phase === "connected",
5348
...access,
5449
summary: null,
55-
needsCursorKeychainAccess: false,
5650
});
5751
continue;
5852
}
@@ -74,10 +68,6 @@ const usageByWindowAtom = Atom.family((windowKey: string) =>
7468
canReadDiagnostics: true,
7569
error: result._tag === "Failure" ? "This environment could not report usage." : null,
7670
summary,
77-
needsCursorKeychainAccess: needsCursorKeychainAccess(
78-
summary,
79-
get(serverEnvironment.providersValueAtom(environmentId)),
80-
),
8171
});
8272
}
8373
return statuses;

0 commit comments

Comments
 (0)