Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(lint): read the clock once per mount and throw TypeError after type guards

Josh Corbett0b901607ec31
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/GenerateConditioningRebalanceField.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/GenerateConditioningRebalanceField.browser.test.tsx
index 20379493d4..66b33a2f6f 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/GenerateConditioningRebalanceField.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/GenerateConditioningRebalanceField.browser.test.tsx
@@ -136,7 +136,7 @@ const weightsInput = (): HTMLInputElement => {
const input = byLabel('Per-tap weights');
if (!(input instanceof HTMLInputElement)) {
- throw new Error('weights input did not render');
+ throw new TypeError('weights input did not render');
}
return input;
@@ -274,7 +274,7 @@ describe('GenerateConditioningRebalanceField', () => {
const nameInput = document.querySelector('[role="dialog"] input');
if (!(nameInput instanceof HTMLInputElement)) {
- throw new Error('preset name dialog did not open');
+ throw new TypeError('preset name dialog did not open');
}
await interact(() => typeInto(nameInput, 'Punchy'));
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.browser.test.ts b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.browser.test.ts
index 3bf3c6020f..7919012797 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.browser.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/workflowImageExport.browser.test.ts
@@ -114,7 +114,7 @@ const captureDownloads = () => {
const downloadedBlob = () => {
const blob = createObjectURL.mock.calls.at(-1)?.[0];
if (!(blob instanceof Blob)) {
- throw new Error('Workflow export did not download a Blob.');
+ throw new TypeError('Workflow export did not download a Blob.');
}
return blob;
};
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/fields/WorkflowFieldInput.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/fields/WorkflowFieldInput.browser.test.tsx
index 2785ad7c79..2ee1b98a0d 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/fields/WorkflowFieldInput.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/fields/WorkflowFieldInput.browser.test.tsx
@@ -322,7 +322,7 @@ describe('WorkflowFieldInput boolean', () => {
const control = host.querySelector('[data-scope="switch"][data-part="control"]');
if (!(control instanceof HTMLElement)) {
- throw new Error('Switch control not rendered');
+ throw new TypeError('Switch control not rendered');
}
await act(async () => {
@@ -607,7 +607,7 @@ describe('WorkflowFieldInput saved workflows', () => {
);
if (!(sharedItem instanceof HTMLElement)) {
- throw new Error('Shared workflow option not rendered');
+ throw new TypeError('Shared workflow option not rendered');
}
await act(async () => {
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewSnapshot.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewSnapshot.tsx
index f24aadd227..d8a1329bd6 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewSnapshot.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/graph-preview/GraphPreviewSnapshot.tsx
@@ -84,7 +84,7 @@ export const GraphPreviewSnapshot = ({
const surface = stageRef.current?.firstElementChild;
if (!(surface instanceof HTMLElement)) {
- throw new Error('The graph preview unmounted before it could be captured.');
+ throw new TypeError('The graph preview unmounted before it could be captured.');
}
onCapture(await captureSurface(surface));
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/ProjectWorkflowsView.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/ProjectWorkflowsView.tsx
index a7f335a096..50e231127b 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/ProjectWorkflowsView.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/ProjectWorkflowsView.tsx
@@ -31,7 +31,7 @@ import {
isUpdatableSource,
toProjectWorkflowLibraryEntries,
} from './projectWorkflowEntries';
-import { formatRelativeTime } from './relativeTime';
+import { formatTimeSince } from './relativeTime';
import {
getWorkflowLibraryCardId,
getWorkflowLibraryCardMenuContentId,
@@ -239,6 +239,7 @@ const ProjectWorkflowDetailPanel = ({
const templatesSnapshot = useInvocationTemplatesSnapshot();
const openAddModels = useOpenAddModels();
const [failedThumbnailUrl, setFailedThumbnailUrl] = useState<string | null>(null);
+ const [openedAt] = useState(() => new Date());
const libraryEntry = useMemo(
() =>
@@ -320,7 +321,7 @@ const ProjectWorkflowDetailPanel = ({
const name = entryName;
const thumbnailUrl = libraryEntry.item.thumbnail_url;
const showThumbnail = Boolean(thumbnailUrl) && thumbnailUrl !== failedThumbnailUrl;
- const lastRun = entry.lastRun ? formatRelativeTime(entry.lastRun.completedAt, new Date()) : '';
+ const lastRun = entry.lastRun ? formatTimeSince(entry.lastRun.completedAt, openedAt) : '';
const source = entry.source;
const canUpdateSource = isUpdatableSource(source);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryThumbnail.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryThumbnail.tsx
index 05502faef6..e176b1245e 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryThumbnail.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryThumbnail.tsx
@@ -28,7 +28,7 @@ import { Suspense, useCallback, useRef, useState, type MouseEvent } from 'react'
import { useTranslation } from 'react-i18next';
import { buildLibraryGraphPreviewSource, DeferredGraphPreviewSnapshot, loadGraphPreview } from './libraryPreviewSource';
-import { formatRelativeTime } from './relativeTime';
+import { formatTimeSince } from './relativeTime';
import { WORKFLOW_THUMBNAIL_ASPECT_RATIO, WorkflowThumbnailImage } from './workflowThumbnail';
const IMAGE_ONLY = ['image'] as const;
@@ -56,6 +56,7 @@ export const WorkflowLibraryThumbnail = ({
const notify = useWorkflowNotifications();
// Keyed by URL: a replaced image arrives under a fresh URL and re-arms the thumbnail.
const [failedThumbnailUrl, setFailedThumbnailUrl] = useState<string | null>(null);
+ const [openedAt] = useState(() => new Date());
// Local rather than `useScopedAction`: the library ships in the editor boot graph, and a second importer of that
// models-only hook splits it into its own startup chunk.
const [isBusy, setIsBusy] = useState(false);
@@ -224,7 +225,7 @@ export const WorkflowLibraryThumbnail = ({
);
const showThumbnail = Boolean(item.thumbnail_url) && item.thumbnail_url !== failedThumbnailUrl;
- const lastRun = item.last_run_at ? formatRelativeTime(item.last_run_at, new Date()) : '';
+ const lastRun = item.last_run_at ? formatTimeSince(item.last_run_at, openedAt) : '';
// A user template's thumbnail is always one someone set; only bundled defaults ship a sample output.
const caption = lastRun
? t('workflowLibrary.lastRun', { when: lastRun })
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
index 731f4c339a..0d42658e67 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
@@ -30,7 +30,7 @@ import {
preloadGraphPreview,
} from './libraryPreviewSource';
import { isUpdatableSource } from './projectWorkflowEntries';
-import { formatRelativeTime } from './relativeTime';
+import { formatTimeSince } from './relativeTime';
import {
discardUnresolvedWorkflowPublication,
getUnresolvedWorkflowPublication,
@@ -584,6 +584,7 @@ const ReviewBody = ({
onPreview: (() => void) | undefined;
}) => {
const { t } = useTranslation();
+ const [openedAt] = useState(() => new Date());
if (error) {
return (
@@ -616,7 +617,7 @@ const ReviewBody = ({
<Text color="fg.subtle" fontSize="xs">
{t('workflowLibrary.reviewRevision', {
revision: record.revision,
- when: record.updated_at ? formatRelativeTime(record.updated_at, new Date()) : '',
+ when: record.updated_at ? formatTimeSince(record.updated_at, openedAt) : '',
})}
</Text>
</Stack>
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.test.ts b/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.test.ts
index c2d1c00e2f..da55d1d072 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { formatRelativeTime } from './relativeTime';
+import { formatRelativeTime, formatTimeSince } from './relativeTime';
const SECOND = 1000;
const MINUTE = 60 * SECOND;
@@ -55,3 +55,17 @@ describe('formatRelativeTime', () => {
expect(formatRelativeTime('', NOW)).toBe('');
});
});
+
+describe('formatTimeSince', () => {
+ it('measures events before the snapshot from the snapshot', () => {
+ expect(formatTimeSince(ago(5 * MINUTE), NOW)).toBe('5 minutes ago');
+ });
+
+ it('reads events newer than the snapshot as now, not the future', () => {
+ expect(formatTimeSince(ahead(3 * MINUTE), NOW)).toBe('now');
+ });
+
+ it('returns an empty string for values it cannot read', () => {
+ expect(formatTimeSince('not-a-timestamp', NOW)).toBe('');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.ts b/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.ts
index 6433fb6107..7a6d806615 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/relativeTime.ts
@@ -32,3 +32,12 @@ export const formatRelativeTime = (timestamp: string, now: Date): string => {
return '';
};
+
+/**
+ * Caption for a past event (last run, last update), read against a clock snapshot the component takes once per mount:
+ * the React Compiler memoizes render output, so reading the clock during render would be stale anyway. A timestamp
+ * newer than the snapshot is either server clock skew or an event since mount; both mean "just happened", so it reads
+ * as "now" rather than "in X". Use `formatRelativeTime` directly where future times are meaningful.
+ */
+export const formatTimeSince = (timestamp: string, snapshot: Date): string =>
+ formatRelativeTime(timestamp, new Date(Math.max(snapshot.getTime(), Date.parse(timestamp))));
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
index 0dc4db0f54..9b8f02a07b 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
@@ -250,7 +250,7 @@ describe('Form builder drag and drop (dnd-kit)', () => {
const content = chrome?.lastElementChild;
if (!(content instanceof HTMLElement)) {
- throw new Error(`card content not found for "${title}"`);
+ throw new TypeError(`card content not found for "${title}"`);
}
return content;
diff --git a/invokeai/frontend/webv2/src/workbench/components/WorkbenchSplashScreen.tsx b/invokeai/frontend/webv2/src/workbench/components/WorkbenchSplashScreen.tsx
index 65764d2624..5883d295aa 100644
--- a/invokeai/frontend/webv2/src/workbench/components/WorkbenchSplashScreen.tsx
+++ b/invokeai/frontend/webv2/src/workbench/components/WorkbenchSplashScreen.tsx
@@ -2,12 +2,14 @@ import splashImageUrl from '@assets/SplashImage.webp';
import { Box, Flex, Heading, HStack, Spinner, Text, VStack } from '@chakra-ui/react';
import { APP_VERSION } from '@platform/runtime/appMetadata';
import { InvokeMark } from '@platform/ui/InvokeMark';
+import { useState } from 'react';
import { useTranslation } from 'react-i18next';
const splashImageStyle = { backgroundPosition: 'center', backgroundSize: 'cover' } as const;
export const WorkbenchSplashScreen = ({ messageKey = 'splash.loadingWorkspace' }: { messageKey?: string }) => {
const { t } = useTranslation();
+ const [year] = useState(() => new Date().getFullYear());
return (
<Flex align="center" aria-busy="true" h="100vh" justify="center" layerStyle="dotGrid" role="status" w="100vw">
@@ -37,7 +39,7 @@ export const WorkbenchSplashScreen = ({ messageKey = 'splash.loadingWorkspace' }
</Text>
</Flex>
<Text fontSize="xs" mt="auto" fontFamily="mono">
- {t('splash.copyright', { year: new Date().getFullYear() })}
+ {t('splash.copyright', { year })}
</Text>
</VStack>
</HStack>
diff --git a/invokeai/frontend/webv2/src/workbench/extensions/README.md b/invokeai/frontend/webv2/src/workbench/extensions/README.md
index 7434982f39..b84aa9bb54 100644
--- a/invokeai/frontend/webv2/src/workbench/extensions/README.md
+++ b/invokeai/frontend/webv2/src/workbench/extensions/README.md
@@ -13,7 +13,9 @@ Widgets should provide component icons, not icon IDs or import strings:
```tsx
import type { WidgetManifest } from '@workbench/widgetContracts';
+
import { LayersIcon } from 'lucide-react';
+
import { layerSettingsContribution } from './settingsContribution';
export const manifest: WidgetManifest = {
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
index bdedd2496f..f7c18c40ed 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
@@ -144,7 +144,7 @@ const spinner = (): HTMLInputElement => {
const input = host?.querySelector('input[type="number"]');
if (!(input instanceof HTMLInputElement)) {
- throw new Error('the clustering-strength spinner is not in the document');
+ throw new TypeError('the clustering-strength spinner is not in the document');
}
return input;
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
index 67c5a949cf..d2b80e7336 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ImageMapWidgetFooter.browser.test.tsx
@@ -71,7 +71,7 @@ const readout = (): HTMLElement => {
);
if (!(found instanceof HTMLElement)) {
- throw new Error('the cluster readout is not in the document');
+ throw new TypeError('the cluster readout is not in the document');
}
return found;