Skip to content

Commit dfc7441

Browse files
committed
Eliminate frontend refresh chatter
1 parent 1302930 commit dfc7441

3 files changed

Lines changed: 2058 additions & 1363 deletions

File tree

frontend/src/App.tsx

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import {
66
completeCodexLogin,
77
getAccounts,
88
getEmbeddedBootstrapState,
9+
getLatestSnapshot,
910
getRemainingCreditsForAccount,
1011
getSavedTheme,
1112
importCurrentAccount,
1213
listenForCodexCallback,
1314
moveAccount,
1415
removeAccount,
15-
saveEmbeddedBootstrapState,
1616
saveTheme,
1717
stopCodexCallbackListener,
1818
switchAccount,
19+
updateUiPreferences,
1920
unarchiveAccount,
2021
type AccountSummary,
2122
type AccountBucket,
@@ -31,8 +32,6 @@ type Theme = "light" | "dark";
3132
type UsageRefreshDisplayMode = "date" | "remaining";
3233
const QUOTA_EPSILON = 0.0001;
3334
const DRAG_SELECT_LOCK_CLASS = "drag-select-lock";
34-
const AUTO_ARCHIVE_ZERO_QUOTA = true;
35-
const AUTO_UNARCHIVE_NON_ZERO_QUOTA = true;
3635
const AUTO_SWITCH_AWAY_FROM_DEPLETED_OR_FROZEN = true;
3736
const AUTO_REFRESH_ACTIVE_MIN_INTERVAL_SEC = 15;
3837
const AUTO_REFRESH_ACTIVE_MAX_INTERVAL_SEC = 21600;
@@ -445,7 +444,6 @@ function App() {
445444
let autoActiveRefreshInFlight = false;
446445
let lastActiveAutoRefreshAt = 0;
447446
let depletedAutoRefreshCooldownUntil = 0;
448-
let persistStateTimer: number | undefined;
449447
let addMenuRef: HTMLDivElement | undefined;
450448
let addButtonRef: HTMLButtonElement | undefined;
451449
let settingsMenuRef: HTMLDivElement | undefined;
@@ -520,45 +518,6 @@ function App() {
520518
.map((account) => account.id);
521519
};
522520

523-
const normalizedCreditsCache = (): Record<string, CreditsInfo> => {
524-
const current = creditsById();
525-
const next: Record<string, CreditsInfo> = {};
526-
for (const [accountId, credits] of Object.entries(current)) {
527-
if (credits) {
528-
next[accountId] = credits;
529-
}
530-
}
531-
return next;
532-
};
533-
534-
const schedulePersistEmbeddedState = () => {
535-
if (typeof window === "undefined") {
536-
return;
537-
}
538-
539-
if (persistStateTimer !== undefined) {
540-
window.clearTimeout(persistStateTimer);
541-
}
542-
543-
persistStateTimer = window.setTimeout(() => {
544-
persistStateTimer = undefined;
545-
const currentView = view();
546-
547-
void saveEmbeddedBootstrapState({
548-
theme: theme(),
549-
autoArchiveZeroQuota: AUTO_ARCHIVE_ZERO_QUOTA,
550-
autoUnarchiveNonZeroQuota: AUTO_UNARCHIVE_NON_ZERO_QUOTA,
551-
autoSwitchAwayFromArchived: AUTO_SWITCH_AWAY_FROM_DEPLETED_OR_FROZEN,
552-
autoRefreshActiveEnabled: autoRefreshActiveEnabled(),
553-
autoRefreshActiveIntervalSec: autoRefreshActiveIntervalSec(),
554-
usageRefreshDisplayMode: usageRefreshDisplayMode(),
555-
view: currentView,
556-
usageById: normalizedCreditsCache(),
557-
savedAt: nowEpoch(),
558-
}).catch(() => {});
559-
}, 120);
560-
};
561-
562521
const runAction = async <T,>(message: string, action: () => Promise<T>): Promise<T | undefined> => {
563522
batch(() => {
564523
setBusy(message);
@@ -584,7 +543,6 @@ function App() {
584543
setAddMenuOpen(true);
585544
}
586545
});
587-
schedulePersistEmbeddedState();
588546
};
589547

590548
const markRefreshing = (accountIds: string[], refreshing: boolean) => {
@@ -648,7 +606,6 @@ function App() {
648606

649607
return next;
650608
});
651-
schedulePersistEmbeddedState();
652609

653610
if (!quiet) {
654611
const failures = entries.filter((entry) => entry[1].status === "error");
@@ -685,7 +642,6 @@ function App() {
685642
...current,
686643
[id]: credits,
687644
}));
688-
schedulePersistEmbeddedState();
689645

690646
if (credits.status !== "error") {
691647
setNotice("Credits refreshed.");
@@ -916,7 +872,9 @@ function App() {
916872
if (next) {
917873
lastActiveAutoRefreshAt = 0;
918874
}
919-
schedulePersistEmbeddedState();
875+
void updateUiPreferences({
876+
autoRefreshActiveEnabled: next,
877+
}).catch(() => {});
920878
};
921879

922880
const handleAutoRefreshIntervalInput = (event: Event) => {
@@ -928,7 +886,9 @@ function App() {
928886
const parsed = Number.parseInt(input.value, 10);
929887
const next = normalizeAutoRefreshIntervalSec(Number.isFinite(parsed) ? parsed : null);
930888
setAutoRefreshActiveIntervalSec(next);
931-
schedulePersistEmbeddedState();
889+
void updateUiPreferences({
890+
autoRefreshActiveIntervalSec: next,
891+
}).catch(() => {});
932892
};
933893

934894
const handleUsageRefreshDisplayModeChange = (event: Event) => {
@@ -939,7 +899,9 @@ function App() {
939899

940900
const next = normalizeUsageRefreshDisplayMode(select.value);
941901
setUsageRefreshDisplayMode(next);
942-
schedulePersistEmbeddedState();
902+
void updateUiPreferences({
903+
usageRefreshDisplayMode: next,
904+
}).catch(() => {});
943905
};
944906

945907
const refreshAccounts = async (initialLoad = false) => {
@@ -956,6 +918,10 @@ function App() {
956918
try {
957919
const next = await getAccounts();
958920
setViewState(next);
921+
const snapshot = getLatestSnapshot();
922+
if (snapshot) {
923+
setCreditsById(snapshot.usageById);
924+
}
959925
if (!initialLoad) {
960926
await refreshCreditsForAccounts(quotaSyncAccountIds(next), { quiet: false });
961927
}
@@ -1457,15 +1423,13 @@ function App() {
14571423
delete nextCredits[id];
14581424
return nextCredits;
14591425
});
1460-
schedulePersistEmbeddedState();
14611426
setNotice("Account removed.");
14621427
};
14631428

14641429
const toggleTheme = () => {
14651430
const nextTheme: Theme = theme() === "light" ? "dark" : "light";
14661431
setTheme(nextTheme);
14671432
applyTheme(nextTheme);
1468-
schedulePersistEmbeddedState();
14691433
void saveTheme(nextTheme).catch(() => {});
14701434
};
14711435

@@ -1482,7 +1446,6 @@ function App() {
14821446
}
14831447
setTheme(initialTheme);
14841448
applyTheme(initialTheme);
1485-
schedulePersistEmbeddedState();
14861449
void saveTheme(initialTheme).catch(() => {});
14871450

14881451
const handlePointerDown = (event: PointerEvent) => {
@@ -1524,9 +1487,6 @@ function App() {
15241487
window.clearInterval(nowTickInterval);
15251488
}
15261489
document.body.classList.remove(DRAG_SELECT_LOCK_CLASS);
1527-
if (persistStateTimer !== undefined) {
1528-
window.clearTimeout(persistStateTimer);
1529-
}
15301490
removeDragPreview();
15311491
});
15321492

0 commit comments

Comments
 (0)