-
Notifications
You must be signed in to change notification settings - Fork 170
feat(drive-integration): async import runs rearchitecture [INTEG-4526] #11092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
david-shibley-contentful
wants to merge
18
commits into
master
Choose a base branch
from
feat/drive-integration-async-runs-INTEG-4526
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 8fac7a8
fix(drive-integration): lift useRunStorage to Page to fix stale runs …
david-shibley-contentful 2d23ce1
chore(drive-integration): fix prettier formatting violations
david-shibley-contentful cd4c650
fix(drive-integration): remove unused imports and variables flagged b…
david-shibley-contentful 66a8b7d
chore(drive-integration): remove speckit planning artifacts and dev f…
david-shibley-contentful db5e939
fix(drive-integration): remove dangling FixtureHarness import and VIT…
david-shibley-contentful 82d7a99
feat(drive-integration): runs page UI polish and retry feature
david-shibley-contentful de91278
fix(drive-integration): address PR review feedback from JuliRossi
david-shibley-contentful 0c40a28
chore(drive-integration): fix prettier formatting violations
david-shibley-contentful 51e55db
fix(drive-integration): remove status grouping, sort runs by date only
david-shibley-contentful d728407
feat(drive-integration): show entry count in completed status badge
david-shibley-contentful 9b2fe4c
feat(drive-integration): View button opens entries created modal with…
david-shibley-contentful 9719b01
refactor(drive-integration): use f36 EntryCard in entries created mod…
david-shibley-contentful 32e8e76
fix(drive-integration): remove unused removeRun destructure to fix li…
david-shibley-contentful ab88836
fix(drive-integration): update stale tests and add missing hook state
david-shibley-contentful c6fdfc7
chore(drive-integration): merge master, resolve useWorkflowAgent conf…
david-shibley-contentful 06780ed
chore(drive-integration): fix prettier formatting in useWorkflowAgent
david-shibley-contentful 3bf3a10
fix(drive-integration): drain react-modal timer before jsdom teardown…
david-shibley-contentful File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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 }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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_DISPLAYrecord map and atoDisplayStatushelper that looks up the map and falls back toDisplayStatus.EXPIRED.