Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 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
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
48 changes: 48 additions & 0 deletions apps/drive-integration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/drive-integration/src/hooks/useGoogleDocPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function useGoogleDocsPicker(

const docs: PickerCallbackData[] = pickedItems.map((doc: any) => ({
id: doc.id,
name: doc.name,
name: doc.name ?? doc.title ?? 'Untitled Document',
mimeType: doc.mimeType,
url: doc.url,
}));
Expand Down
104 changes: 104 additions & 0 deletions apps/drive-integration/src/hooks/useRunStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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;
retryRun(oldRunId: string, newRecord: RunRecord): 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);
persist(next);
return next;
});
},
[persist]
);

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

const retryRun = useCallback(
(oldRunId: string, newRecord: RunRecord) => {
setRuns((current) => {
const next = current.map((r) => (r.runId === oldRunId ? newRecord : r));
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, retryRun, markCompleted, storageError };
}
104 changes: 104 additions & 0 deletions apps/drive-integration/src/hooks/useRunsPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 { DisplayStatus } from '../types/runs';
import type { RunRecord } from '../types/runs';

const POLL_INTERVAL_MS = 10_000;

const RUN_STATUS_TO_DISPLAY: Partial<Record<RunStatus, DisplayStatus>> = {
[RunStatus.IN_PROGRESS]: DisplayStatus.RUNNING,
[RunStatus.DRAFT]: DisplayStatus.RUNNING,
[RunStatus.PENDING_REVIEW]: DisplayStatus.NEEDS_REVIEW,
[RunStatus.COMPLETED]: DisplayStatus.COMPLETED,
[RunStatus.FAILED]: DisplayStatus.FAILED,
};

function toDisplayStatus(runData: AgentRunData | null): DisplayStatus {
if (!runData) return DisplayStatus.EXPIRED;
const status = runData.sys?.status ?? runData.metadata?.status;
return (status && RUN_STATUS_TO_DISPLAY[status]) ?? DisplayStatus.EXPIRED;
}

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 could be a map, instead of s switch. Similar to what we do for FAILURE_REASON_MESSAGES

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.

Done — replaced the switch with a RUN_STATUS_TO_DISPLAY record map and a toDisplayStatus helper that looks up the map and falls back to DisplayStatus.EXPIRED.


function extractErrorMessage(runData: AgentRunData | null): string | undefined {
return runData?.metadata?.workflowFailure?.message;
}

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

export function useRunsPolling(runs: RunRecord[], sdk: PageAppSDK): UseRunsPollingResult {
const [statusMap, setStatusMap] = useState<Map<string, DisplayStatus>>(() => {
const initial = new Map<string, DisplayStatus>();
for (const r of runs) initial.set(r.runId, DisplayStatus.LOADING);
return initial;
});
const [errorMap, setErrorMap] = useState<Map<string, string>>(new Map());
const [titleMap, setTitleMap] = 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).catch(() => null))
);

const nextStatus = new Map<string, DisplayStatus>();
const nextErrors = new Map<string, string>();
const nextTitles = 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);
const title = data?.metadata?.suspendPayload?.documentTitle;
if (title) nextTitles.set(runId, title);
}

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

useEffect(() => {
void fetchAllStatuses().then((nextStatus) => {
if (!nextStatus) return;
const hasRunning = [...nextStatus.values()].some((s) => s === DisplayStatus.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 === DisplayStatus.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, titleMap };
}
Loading
Loading