Skip to content
This repository was archived by the owner on Nov 29, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 100 additions & 50 deletions src/components/ExtensionConnectClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Check, Copy, ExternalLink, KeyRound, Loader2, Trash2 } from 'lucide-react';
import { Link } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';

import { signIn } from '@/lib/auth-client';

Expand Down Expand Up @@ -39,24 +39,50 @@ function formatDate(value: ApiKeySummary['createdAt']): string {

export function ExtensionConnectClient() {
const { user, loading } = useAuth();
const userId = user?.id;
return (
<ExtensionConnections
key={user?.id ?? 'guest'}
userId={user?.id ?? null}
signedIn={Boolean(user)}
loading={loading}
/>
);
}

function ExtensionConnections({
userId,
signedIn,
loading,
}: {
userId: string | null;
signedIn: boolean;
loading: boolean;
}) {
const [keys, setKeys] = useState<ApiKeySummary[]>([]);
const [createdKey, setCreatedKey] = useState<CreatedKey | null>(null);
const [isLoadingKeys, setIsLoadingKeys] = useState(false);
const [isLoadingKeys, setIsLoadingKeys] = useState(Boolean(userId));
const [loadError, setLoadError] = useState<string | null>(null);
const keyList = useRef<HTMLDivElement>(null);
const keyRead = useRef<AbortController | null>(null);
const [isCreating, setIsCreating] = useState(false);
const [revokingId, setRevokingId] = useState<string | null>(null);
const [copied, setCopied] = useState(false);
const [error, setError] = useState<string | null>(null);
const activeKeys = keys.filter((key) => !key.revokedAt);

useEffect(() => {
if (!userId) return;

let cancelled = false;
// eslint-disable-next-line react-hooks/set-state-in-effect -- fetch-on-mount
const loadKeys = useCallback(() => {
keyRead.current?.abort();
const controller = new AbortController();
keyRead.current = controller;
setIsLoadingKeys(true);

setError(null);
fetch('/api/keys', { cache: 'no-store' })
setLoadError(null);
const deadline = window.setTimeout(() => {
if (controller.signal.aborted) return;
controller.abort();
setLoadError('Loading extension keys timed out.');
setIsLoadingKeys(false);
}, 12_000);
fetch('/api/keys', { cache: 'no-store', signal: controller.signal })
.then(async (response) => {
if (!response.ok) {
const payload = (await response.json().catch(() => ({}))) as { error?: string };
Expand All @@ -65,19 +91,26 @@ export function ExtensionConnectClient() {
return response.json() as Promise<ApiKeySummary[]>;
})
.then((rows) => {
if (!cancelled) setKeys(rows);
if (!controller.signal.aborted) setKeys(rows);
})
.catch((err) => {
if (!cancelled) setError(err instanceof Error ? err.message : 'Failed to load keys');
if (!controller.signal.aborted) {
setLoadError(err instanceof Error ? err.message : 'Failed to load keys');
}
})
.finally(() => {
if (!cancelled) setIsLoadingKeys(false);
window.clearTimeout(deadline);
if (!controller.signal.aborted) setIsLoadingKeys(false);
});
}, []);

useEffect(() => {
if (!userId) return;
loadKeys();
return () => {
cancelled = true;
keyRead.current?.abort();
};
}, [userId]);
}, [loadKeys, userId]);

const createKey = async () => {
setError(null);
Expand Down Expand Up @@ -165,7 +198,7 @@ export function ExtensionConnectClient() {
</div>
</section>

{!user && (
{!signedIn && (
<section className="rounded-xl border border-gray-800 bg-gray-900/70 p-5">
<h2 className="text-base font-semibold text-white">Install the Chrome extension</h2>
<ol className="mt-3 list-decimal space-y-2 pl-5 text-sm text-gray-400">
Expand Down Expand Up @@ -195,7 +228,7 @@ export function ExtensionConnectClient() {
</section>
)}

{!user ? (
{!signedIn ? (
<section className="rounded-xl border border-gray-800 bg-gray-900/70 p-5">
<h2 className="text-base font-semibold text-white">Sign in first</h2>
<p className="mt-2 text-sm text-gray-400">
Expand All @@ -218,7 +251,7 @@ export function ExtensionConnectClient() {
The token is shown once. If you lose it, revoke it and create another.
</p>
</div>
<Button onClick={() => void createKey()} disabled={isCreating}>
<Button onClick={() => void createKey()} disabled={isCreating || isLoadingKeys}>
{isCreating ? (
<span className="inline-flex items-center gap-2">
<Loader2 className="h-4 w-4 animate-spin" />
Expand Down Expand Up @@ -298,39 +331,56 @@ export function ExtensionConnectClient() {
</Link>
</div>

<div className="mt-4 divide-y divide-gray-800">
<div ref={keyList} tabIndex={-1} className="mt-4 divide-y divide-gray-800">
{isLoadingKeys ? (
<div className="py-4 text-sm text-gray-400">Loading keys...</div>
) : keys.filter((key) => !key.revokedAt).length === 0 ? (
<div className="py-4 text-sm text-gray-400" role="status">
Loading keys...
</div>
) : loadError ? (
<div className="space-y-3 py-4">
<p className="text-sm text-red-300" role="alert">
{loadError}
</p>
<Button
type="button"
variant="secondary"
size="sm"
disabled={isCreating || revokingId !== null}
onClick={() => {
keyList.current?.focus();
loadKeys();
}}
>
Try again
</Button>
</div>
) : activeKeys.length === 0 ? (
<div className="py-4 text-sm text-gray-500">No active extension keys.</div>
) : (
keys
.filter((key) => !key.revokedAt)
.map((key) => (
<div key={key.id} className="flex items-center justify-between gap-3 py-3">
<div className="min-w-0">
<p className="truncate text-sm font-medium text-gray-100">{key.name}</p>
<p className="text-xs text-gray-500">
{key.prefix}... created {formatDate(key.createdAt)}
</p>
</div>
<Button
type="button"
variant="ghost"
size="icon"
disabled={revokingId === key.id}
onClick={() => void revokeKey(key.id)}
title="Revoke key"
>
{revokingId === key.id ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Trash2 className="h-4 w-4 text-red-300" />
)}
</Button>
</div>
))
)}
) : null}
{activeKeys.map((key) => (
<div key={key.id} className="flex items-center justify-between gap-3 py-3">
<div className="min-w-0">
<p className="truncate text-sm font-medium text-gray-100">{key.name}</p>
<p className="text-xs text-gray-500">
{key.prefix}... created {formatDate(key.createdAt)}
</p>
</div>
<Button
type="button"
variant="ghost"
size="icon"
disabled={revokingId === key.id || isLoadingKeys}
onClick={() => void revokeKey(key.id)}
title="Revoke key"
>
{revokingId === key.id ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<Trash2 className="h-4 w-4 text-red-300" />
)}
</Button>
</div>
))}
</div>
</section>
</>
Expand Down
63 changes: 63 additions & 0 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,66 @@ test('signed-in connections retain key management without creating a key on entr
await expect(page.getByRole('heading', { name: 'Sign in first' })).toHaveCount(0);
expect(methods).toEqual(['GET']);
});

test('a failed extension key read has its own retry and shows empty only after success', async ({
page,
}) => {
await page.clock.install();
await page.route('**/api/auth/get-session', (route) => route.fulfill({ json: syntheticSession }));
let reads = 0;
await page.route('**/api/keys', async (route) => {
expect(route.request().method()).toBe('GET');
reads++;
if (reads === 1) {
await route.fulfill({ status: 503, json: { error: 'Synthetic key read failed' } });
return;
}
if (reads === 2) return;
await route.fulfill({ json: [] });
});

await page.goto('/extension');
await expect(page.getByRole('alert')).toHaveText('Synthetic key read failed');
await expect(page.getByText('No active extension keys.')).toHaveCount(0);
await render(page, 'failed-extension-keys');
await page.getByRole('button', { name: 'Try again' }).click();
await expect(page.getByRole('status')).toHaveText('Loading keys...');
await expect(page.getByRole('status').locator('..')).toBeFocused();
await expect(page.getByRole('alert')).toHaveCount(0);
await expect.poll(() => reads).toBe(2);
await page.clock.fastForward(13_000);
await expect(page.getByRole('alert')).toHaveText('Loading extension keys timed out.');
await expect(page.getByText('No active extension keys.')).toHaveCount(0);
await page.getByRole('button', { name: 'Try again' }).click();
await expect(page.getByText('No active extension keys.')).toBeVisible();
expect(reads).toBe(3);
await render(page, 'recovered-extension-keys');
});

test('a newly created fixture key remains visible while the list read is unavailable', async ({
page,
}) => {
await page.route('**/api/auth/get-session', (route) => route.fulfill({ json: syntheticSession }));
await page.route('**/api/keys', (route) => {
if (route.request().method() === 'GET') {
return route.fulfill({ status: 503, json: { error: 'Synthetic key read failed' } });
}
expect(route.request().method()).toBe('POST');
return route.fulfill({
status: 201,
json: {
id: 'fixture-key-id',
name: 'Fixture extension',
prefix: 'fixture',
token: 'fixture-value',
createdAt: 1763942400,
},
});
});
await page.goto('/extension');
await expect(page.getByRole('alert')).toHaveText('Synthetic key read failed');
await page.getByRole('button', { name: 'Create extension key' }).click();
await expect(page.getByLabel('New extension key')).toHaveValue('fixture-value');
await expect(page.getByText('Fixture extension', { exact: true })).toBeVisible();
await expect(page.getByText('No active extension keys.')).toHaveCount(0);
});
Loading