Skip to content

Your always-on AI coworkers that move between text, calls, and Slack.

TypeScript

fix: clear the connection error once polling recovers (#73)

Charan Rathore1d38d426efc1
diff --git a/src/client/App.tsx b/src/client/App.tsx
index 353b631..8f1861b 100644
--- a/src/client/App.tsx
+++ b/src/client/App.tsx
@@ -32,6 +32,13 @@ import type {
WorkspaceState,
} from '../shared/types';
import { api, ApiError, authHeaders, setToken } from './api';
+import {
+ applyCaptureResult,
+ applyRefreshResult,
+ dismissNotice,
+ visibleNotice,
+ type Notices,
+} from './poll-notice';
import { Mascot } from './Mascot';
import { Chat } from './Chat';
import { ThreadList } from './ThreadList';
@@ -40,6 +47,14 @@ import { TaskRow } from './TaskPresentation';
import { TaskActions } from './TaskActions';
import { WorkspaceDialog, type Dialog } from './WorkspaceDialog';
+function describeFailure(error: unknown, fallback: string) {
+ return {
+ ok: false as const,
+ status: error instanceof ApiError ? error.status : undefined,
+ message: error instanceof Error ? error.message : fallback,
+ };
+}
+
export function App() {
const [state, setState] = useState<State>();
const [workspace, setWorkspace] = useState<WorkspaceState>();
@@ -92,7 +107,16 @@ export function App() {
openPageLink(`#/spaces/${space}${page ? `/pages/${page}` : ''}`);
};
- const [error, setError] = useState('');
+ const [notices, setNotices] = useState<Notices>({
+ refresh: '',
+ capture: '',
+ action: '',
+ });
+ const error = visibleNotice(notices);
+ const setError = (action: string) =>
+ setNotices((current) =>
+ current.action === action ? current : { ...current, action },
+ );
const [auth, setAuth] = useState('');
const [needsAuth, setNeedsAuth] = useState(false);
const [dialog, setDialog] = useState<Dialog>();
@@ -115,12 +139,15 @@ export function App() {
setWorkspace(w);
setNeedsAuth(false);
setSelectedDot((previous) => previous || w.dots[0]?.id || '');
+ setNotices((current) => applyRefreshResult(current, { ok: true }));
} catch (e) {
if (e instanceof ApiError && e.status === 401) setNeedsAuth(true);
- else
- setError(
- e instanceof Error ? e.message : 'Could not connect to the server.',
- );
+ setNotices((current) =>
+ applyRefreshResult(
+ current,
+ describeFailure(e, 'Could not connect to the server.'),
+ ),
+ );
}
}, []);
useEffect(() => {
@@ -135,10 +162,18 @@ export function App() {
const load = () =>
void api<Result | null>(`/conversations/${selectedThread}/capture`)
.then((result) => {
- if (active) setCapture(result ?? undefined);
+ if (!active) return;
+ setCapture(result ?? undefined);
+ setNotices((current) => applyCaptureResult(current, { ok: true }));
})
.catch((e) => {
- if (active) setError(e.message);
+ if (!active) return;
+ setNotices((current) =>
+ applyCaptureResult(
+ current,
+ describeFailure(e, 'Could not connect to the server.'),
+ ),
+ );
});
load();
const timer = setInterval(load, 3000);
@@ -528,7 +563,7 @@ export function App() {
<button
className="icon-button"
aria-label="Dismiss error"
- onClick={() => setError('')}
+ onClick={() => setNotices((current) => dismissNotice(current))}
>
<X size={16} />
</button>
diff --git a/src/client/poll-notice.ts b/src/client/poll-notice.ts
new file mode 100644
index 0000000..c2400f0
--- /dev/null
+++ b/src/client/poll-notice.ts
@@ -0,0 +1,59 @@
+export interface Notices {
+ refresh: string;
+ capture: string;
+ action: string;
+}
+
+export interface PollFailure {
+ ok: false;
+ status?: number;
+ message: string;
+}
+
+const TRANSPORT_MESSAGES = new Set(['Server returned an unreadable response.']);
+
+// A failure that carries no HTTP status did not come from the API, so it is a
+// network failure (fetch throws a TypeError whose text differs by browser).
+function isTransportFailure(result: PollFailure): boolean {
+ return (
+ result.status === undefined ||
+ result.status === 502 ||
+ result.status === 504 ||
+ TRANSPORT_MESSAGES.has(result.message)
+ );
+}
+
+export function visibleNotice(notices: Notices): string {
+ return notices.action || notices.refresh || notices.capture;
+}
+
+export function dismissNotice(notices: Notices): Notices {
+ if (notices.action) return { ...notices, action: '' };
+ if (notices.refresh) return { ...notices, refresh: '' };
+ if (notices.capture) return { ...notices, capture: '' };
+ return notices;
+}
+
+export function applyRefreshResult(
+ current: Notices,
+ result: { ok: true } | PollFailure,
+): Notices {
+ if (result.ok) return current.refresh ? { ...current, refresh: '' } : current;
+ if (result.status === 401) return current;
+ if (current.refresh === result.message) return current;
+ return { ...current, refresh: result.message };
+}
+
+export function applyCaptureResult(
+ current: Notices,
+ result: { ok: true } | PollFailure,
+): Notices {
+ if (result.ok) return current.capture ? { ...current, capture: '' } : current;
+ if (result.status === 401) return current;
+ if (isTransportFailure(result)) {
+ if (current.capture === result.message) return current;
+ return { ...current, capture: result.message };
+ }
+ if (current.action === result.message) return current;
+ return { ...current, action: result.message };
+}
diff --git a/tests/poll-notice.test.ts b/tests/poll-notice.test.ts
new file mode 100644
index 0000000..a87e867
--- /dev/null
+++ b/tests/poll-notice.test.ts
@@ -0,0 +1,110 @@
+import { expect, it } from 'vitest';
+import {
+ applyCaptureResult,
+ applyRefreshResult,
+ dismissNotice,
+ visibleNotice,
+ type Notices,
+} from '../src/client/poll-notice';
+
+const none: Notices = { refresh: '', capture: '', action: '' };
+const saved: Notices = { ...none, action: 'Could not save.' };
+
+it('clears a recovered refresh error without wiping an action error', () => {
+ const lost = applyRefreshResult(saved, {
+ ok: false,
+ message: 'Server returned an unreadable response.',
+ status: 502,
+ });
+ expect(lost).toEqual({
+ refresh: 'Server returned an unreadable response.',
+ capture: '',
+ action: 'Could not save.',
+ });
+ expect(visibleNotice(lost)).toBe('Could not save.');
+ expect(applyRefreshResult(lost, { ok: true })).toEqual(saved);
+ expect(
+ visibleNotice(
+ applyRefreshResult({ ...none, refresh: 'Failed to fetch' }, { ok: true }),
+ ),
+ ).toBe('');
+});
+
+it('keeps an unauthorized poll from replacing the connection notice', () => {
+ const current: Notices = { ...none, refresh: 'Failed to fetch' };
+ expect(
+ applyRefreshResult(current, {
+ ok: false,
+ status: 401,
+ message: 'Enter your owner access token to unlock OpenDots.',
+ }),
+ ).toBe(current);
+});
+
+it('treats a capture transport failure as a capture notice and leaves save errors in place', () => {
+ const lost = applyCaptureResult(saved, {
+ ok: false,
+ message: 'Failed to fetch',
+ });
+ expect(lost.capture).toBe('Failed to fetch');
+ expect(lost.action).toBe('Could not save.');
+ expect(applyCaptureResult(lost, { ok: true })).toEqual(saved);
+
+ const specific = applyCaptureResult(
+ { ...none, capture: 'Failed to fetch', action: 'Could not save.' },
+ { ok: false, status: 500, message: 'Thread not found.' },
+ );
+ expect(specific.capture).toBe('Failed to fetch');
+ expect(specific.action).toBe('Thread not found.');
+ expect(applyRefreshResult(specific, { ok: true }).action).toBe(
+ 'Thread not found.',
+ );
+});
+
+it('treats any failure without an HTTP status as a transport failure', () => {
+ for (const message of [
+ 'Failed to fetch',
+ 'NetworkError when attempting to fetch resource.',
+ 'Load failed',
+ ]) {
+ const lost = applyCaptureResult(none, { ok: false, message });
+ expect(lost).toEqual({ ...none, capture: message });
+ expect(applyCaptureResult(lost, { ok: true })).toEqual(none);
+ }
+});
+
+it('keeps a failing refresh poll visible while capture polls succeed', () => {
+ const failing = applyRefreshResult(none, {
+ ok: false,
+ status: 503,
+ message: 'Service unavailable.',
+ });
+ const afterCapture = applyCaptureResult(failing, { ok: true });
+ expect(afterCapture.refresh).toBe('Service unavailable.');
+ expect(visibleNotice(afterCapture)).toBe('Service unavailable.');
+ expect(applyRefreshResult(afterCapture, { ok: true })).toEqual(none);
+});
+
+it('keeps a failing capture poll visible while refresh polls succeed', () => {
+ const failing = applyCaptureResult(none, {
+ ok: false,
+ status: 504,
+ message: 'Gateway timeout.',
+ });
+ const afterRefresh = applyRefreshResult(failing, { ok: true });
+ expect(afterRefresh.capture).toBe('Gateway timeout.');
+ expect(applyCaptureResult(afterRefresh, { ok: true })).toEqual(none);
+});
+
+it('dismisses the visible notice and leaves the others', () => {
+ const all: Notices = {
+ refresh: 'Service unavailable.',
+ capture: 'Failed to fetch',
+ action: 'Could not save.',
+ };
+ const first = dismissNotice(all);
+ expect(first).toEqual({ ...all, action: '' });
+ const second = dismissNotice(first);
+ expect(second).toEqual({ ...all, action: '', refresh: '' });
+ expect(dismissNotice(second)).toEqual(none);
+});