Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
113 lines
2.8 KiB
TypeScript
113 lines
2.8 KiB
TypeScript
import { useSyncExternalStore } from 'react';
|
|
|
|
type Listener = () => void;
|
|
|
|
class SessionActivityStore {
|
|
private snapshot: Record<string, boolean> = {};
|
|
private listeners = new Set<Listener>();
|
|
|
|
getSnapshot = () => this.snapshot;
|
|
|
|
subscribe = (listener: Listener) => {
|
|
this.listeners.add(listener);
|
|
return () => this.listeners.delete(listener);
|
|
};
|
|
|
|
private emit() {
|
|
this.listeners.forEach((listener) => listener());
|
|
}
|
|
|
|
setTabActive = (tabId: string, hasActivity: boolean) => {
|
|
const alreadyActive = !!this.snapshot[tabId];
|
|
if (alreadyActive === hasActivity) return;
|
|
|
|
if (hasActivity) {
|
|
this.snapshot = { ...this.snapshot, [tabId]: true };
|
|
} else {
|
|
const { [tabId]: _removed, ...rest } = this.snapshot;
|
|
this.snapshot = rest;
|
|
}
|
|
|
|
this.emit();
|
|
};
|
|
|
|
clearTab = (tabId: string) => {
|
|
this.setTabActive(tabId, false);
|
|
};
|
|
|
|
clearTabs = (tabIds: Iterable<string>) => {
|
|
let changed = false;
|
|
const next = { ...this.snapshot };
|
|
|
|
for (const tabId of tabIds) {
|
|
if (!next[tabId]) continue;
|
|
delete next[tabId];
|
|
changed = true;
|
|
}
|
|
|
|
if (!changed) return;
|
|
this.snapshot = next;
|
|
this.emit();
|
|
};
|
|
|
|
prune = (validTabIds: Set<string>) => {
|
|
let changed = false;
|
|
const next: Record<string, boolean> = {};
|
|
|
|
for (const tabId of Object.keys(this.snapshot)) {
|
|
if (validTabIds.has(tabId)) {
|
|
next[tabId] = true;
|
|
} else {
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
if (!changed) return;
|
|
this.snapshot = next;
|
|
this.emit();
|
|
};
|
|
}
|
|
|
|
export const sessionActivityStore = new SessionActivityStore();
|
|
|
|
export const useSessionActivityMap = () => {
|
|
return useSyncExternalStore(
|
|
sessionActivityStore.subscribe,
|
|
sessionActivityStore.getSnapshot,
|
|
sessionActivityStore.getSnapshot,
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Per-tab activity boolean. Store notify is still global, but React skips re-render
|
|
* when this tab's boolean is unchanged (Object.is on the snapshot primitive).
|
|
*/
|
|
export const useSessionActivity = (tabId: string): boolean => {
|
|
return useSyncExternalStore(
|
|
sessionActivityStore.subscribe,
|
|
() => !!sessionActivityStore.getSnapshot()[tabId],
|
|
() => !!sessionActivityStore.getSnapshot()[tabId],
|
|
);
|
|
};
|
|
|
|
/** True if any of the given session ids currently has activity. */
|
|
export const useAnySessionActivity = (sessionIds: readonly string[]): boolean => {
|
|
return useSyncExternalStore(
|
|
sessionActivityStore.subscribe,
|
|
() => {
|
|
const snap = sessionActivityStore.getSnapshot();
|
|
for (const id of sessionIds) {
|
|
if (snap[id]) return true;
|
|
}
|
|
return false;
|
|
},
|
|
() => {
|
|
const snap = sessionActivityStore.getSnapshot();
|
|
for (const id of sessionIds) {
|
|
if (snap[id]) return true;
|
|
}
|
|
return false;
|
|
},
|
|
);
|
|
};
|