Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e60283d
feat(drive-integration): async import runs rearchitecture [INTEG-4526]
david-shibley-contentful Jul 16, 2026
8fac7a8
fix(drive-integration): lift useRunStorage to Page to fix stale runs …
david-shibley-contentful Jul 16, 2026
2d23ce1
chore(drive-integration): fix prettier formatting violations
david-shibley-contentful Jul 16, 2026
cd4c650
fix(drive-integration): remove unused imports and variables flagged b…
david-shibley-contentful Jul 16, 2026
66a8b7d
chore(drive-integration): remove speckit planning artifacts and dev f…
david-shibley-contentful Jul 17, 2026
db5e939
fix(drive-integration): remove dangling FixtureHarness import and VIT…
david-shibley-contentful Jul 17, 2026
82d7a99
feat(drive-integration): runs page UI polish and retry feature
david-shibley-contentful Jul 20, 2026
de91278
fix(drive-integration): address PR review feedback from JuliRossi
david-shibley-contentful Jul 20, 2026
0c40a28
chore(drive-integration): fix prettier formatting violations
david-shibley-contentful Jul 20, 2026
51e55db
fix(drive-integration): remove status grouping, sort runs by date only
david-shibley-contentful Jul 20, 2026
d728407
feat(drive-integration): show entry count in completed status badge
david-shibley-contentful Jul 20, 2026
9b2fe4c
feat(drive-integration): View button opens entries created modal with…
david-shibley-contentful Jul 20, 2026
9719b01
refactor(drive-integration): use f36 EntryCard in entries created mod…
david-shibley-contentful Jul 20, 2026
32e8e76
fix(drive-integration): remove unused removeRun destructure to fix li…
david-shibley-contentful Jul 20, 2026
ab88836
fix(drive-integration): update stale tests and add missing hook state
david-shibley-contentful Jul 20, 2026
c6fdfc7
chore(drive-integration): merge master, resolve useWorkflowAgent conf…
david-shibley-contentful Jul 20, 2026
06780ed
chore(drive-integration): fix prettier formatting in useWorkflowAgent
david-shibley-contentful Jul 20, 2026
3bf3a10
fix(drive-integration): drain react-modal timer before jsdom teardown…
david-shibley-contentful Jul 20, 2026
d46abe4
fix(drive-integration): address review feedback round 2 — transient-n…
david-shibley-contentful Jul 21, 2026
6b49900
fix(drive-integration): remove unused afterEach import from useRunsPo…
david-shibley-contentful Jul 21, 2026
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
101 changes: 101 additions & 0 deletions apps/drive-integration/src/hooks/useRunStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { useState, useCallback } from 'react';
import type { RunRecord } from '../types/runs';

const MAX_RUNS = 50;

function makeKey(spaceId: string, environmentId: string): string {
return `gdrive-import-runs::${spaceId}::${environmentId}`;
}

function readFromStorage(key: string): RunRecord[] {
try {
const raw = localStorage.getItem(key);
if (!raw) return [];
return JSON.parse(raw) as RunRecord[];
} catch {
return [];
}
}

function writeToStorage(key: string, records: RunRecord[]): void {
localStorage.setItem(key, JSON.stringify(records));
}

export interface UseRunStorage {
runs: RunRecord[];
addRun(record: RunRecord): void;
removeRun(runId: string): void;
markCompleted(runId: string, entryIds: string[]): void;
storageError: string | null;
}

export function useRunStorage(spaceId: string, environmentId: string): UseRunStorage {
const key = makeKey(spaceId, environmentId);
const [runs, setRuns] = useState<RunRecord[]>(() => readFromStorage(key));
const [storageError, setStorageError] = useState<string | null>(null);

const persist = useCallback(
(next: RunRecord[]) => {
try {
writeToStorage(key, next);
setStorageError(null);
} catch (err) {
const msg =
err instanceof Error
? err.message
: 'Unable to save import history. Storage may be full.';
setStorageError(msg);
}
setRuns(next);
},
[key]
);

const addRun = useCallback(
(record: RunRecord) => {
setRuns((current) => {
if (current.some((r) => r.runId === record.runId)) return current;
const next = [record, ...current];
if (next.length > MAX_RUNS) next.splice(MAX_RUNS);
try {
writeToStorage(key, next);
setStorageError(null);
} catch (err) {
const msg =
err instanceof Error
? err.message
: 'Unable to save import history. Storage may be full.';
setStorageError(msg);
}
return next;
Comment thread
JuliRossi marked this conversation as resolved.
Outdated
});
},
[key]
);

const removeRun = useCallback(
(runId: string) => {
setRuns((current) => {
const next = current.filter((r) => r.runId !== runId);
persist(next);
return next;
});
},
[persist]
);

const markCompleted = useCallback(
(runId: string, entryIds: string[]) => {
setRuns((current) => {
const next = current.map((r) =>
r.runId === runId ? { ...r, createdEntryIds: entryIds } : r
);
persist(next);
return next;
});
},
[persist]
);

return { runs, addRun, removeRun, markCompleted, storageError };
}
109 changes: 109 additions & 0 deletions apps/drive-integration/src/hooks/useRunsPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { useState, useEffect, useRef, useCallback } from 'react';
import { PageAppSDK } from '@contentful/app-sdk';
import { RunStatus } from '@types';
import { getWorkflowRun, AgentRunData } from '../services/agents-api';
import type { DisplayStatus, RunRecord } from '../types/runs';

const POLL_INTERVAL_MS = 10_000;

function toDisplayStatus(runData: AgentRunData | null): DisplayStatus {
if (!runData) return 'expired';
const status = runData.sys?.status ?? runData.metadata?.status;
switch (status) {
case RunStatus.IN_PROGRESS:
case RunStatus.DRAFT:
return 'running';
case RunStatus.PENDING_REVIEW:
return 'needs-review';
case RunStatus.COMPLETED:
return 'completed';
case RunStatus.FAILED:
return 'failed';
default:
return 'expired';
}
}
Comment thread
JuliRossi marked this conversation as resolved.

function extractErrorMessage(runData: AgentRunData | null): string | undefined {
return runData?.metadata?.workflowFailure?.message ?? undefined;
Comment thread
JuliRossi marked this conversation as resolved.
Outdated
}

export interface UseRunsPollingResult {
statusMap: Map<string, DisplayStatus>;
errorMap: Map<string, string>;
}

export function useRunsPolling(runs: RunRecord[], sdk: PageAppSDK): UseRunsPollingResult {
const [statusMap, setStatusMap] = useState<Map<string, DisplayStatus>>(
() => new Map(runs.map((r) => [r.runId, 'loading' as DisplayStatus]))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I think we can avoid seeding loading here, because in RunsPage where runsWithStatus is built:
displayStatus: statusMap[r.runId] ?? 'loading',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — useState is now initialized with new Map() (no seeding). The loading fallback lives only in RunsPage as statusMap.get(r.runId) ?? DisplayStatus.LOADING.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still doing the same in the current version

);
const [errorMap, setErrorMap] = useState<Map<string, string>>(new Map());
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);

const fetchAllStatuses = useCallback(async () => {
if (runs.length === 0) return;

const spaceId = sdk.ids.space;
const environmentId = sdk.ids.environmentAlias ?? sdk.ids.environment;

const results = await Promise.all(
runs.map((r) => getWorkflowRun(sdk, spaceId, environmentId, r.runId))
);

const nextStatus = new Map<string, DisplayStatus>();
const nextErrors = new Map<string, string>();

for (let i = 0; i < runs.length; i++) {
const runId = runs[i].runId;
const data = results[i];
nextStatus.set(runId, toDisplayStatus(data));
const errMsg = extractErrorMessage(data);
if (errMsg) nextErrors.set(runId, errMsg);
}

setStatusMap(nextStatus);
setErrorMap(nextErrors);
return nextStatus;
}, [runs, sdk]);

// Initial fetch + reactive refetch when run list changes
useEffect(() => {
// Reset new runs to 'loading'
setStatusMap((prev) => {
const next = new Map(prev);
for (const r of runs) {
if (!next.has(r.runId)) next.set(r.runId, 'loading');
}
return next;
});
Comment thread
JuliRossi marked this conversation as resolved.
Outdated

void fetchAllStatuses().then((nextStatus) => {
if (!nextStatus) return;
const hasRunning = [...nextStatus.values()].some((s) => s === 'running');

if (intervalRef.current) clearInterval(intervalRef.current);

if (hasRunning) {
intervalRef.current = setInterval(() => {
void fetchAllStatuses().then((updated) => {
if (!updated) return;
const stillRunning = [...updated.values()].some((s) => s === 'running');
if (!stillRunning && intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
});
}, POLL_INTERVAL_MS);
}
});

return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, [fetchAllStatuses]);

return { statusMap, errorMap };
}
Loading
Loading