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.

refactor(webv2): share the gallery upload picker input

joshistoast9486c278b833
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
index 58bc18fddc..8db1ca9968 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
@@ -12,7 +12,6 @@ import {
useMemo,
useRef,
useState,
- type ChangeEvent,
type DragEvent,
type KeyboardEvent,
} from 'react';
@@ -31,10 +30,10 @@ import {
import { GalleryQueuePlaceholderCell } from './GalleryQueuePlaceholderCell';
import { GalleryThumbnailCell } from './GalleryThumbnail';
import { useGalleryUi } from './GalleryUiContext';
-import { ACCEPTED_UPLOAD_EXTENSIONS, UPLOAD_INPUT_STYLE } from './GalleryUploadButton';
import { useGalleryWidget } from './GalleryWidgetContext';
import { useGalleryGridHotkeys } from './useGalleryGridHotkeys';
import { useGalleryGridSelection } from './useGalleryGridSelection';
+import { useGalleryUploadInput } from './useGalleryUploadInput';
/**
* Seeds the measured width per region so remounting the gallery in a placement
@@ -128,7 +127,6 @@ export const GalleryImageGrid = () => {
const [viewportWidth, setViewportWidth] = useState(() => viewportWidthCache.get(region) ?? 0);
const dragDepthRef = useRef(0);
const viewportRef = useRef<HTMLDivElement | null>(null);
- const uploadInputRef = useRef<HTMLInputElement | null>(null);
const { imageDensityPercent, imageOrderDir, paginationMode, showImageDimensions, thumbnailFit } = gallery.settings;
const {
@@ -302,20 +300,7 @@ export const GalleryImageGrid = () => {
[actions]
);
- const handleUploadFileChange = useCallback(
- (event: ChangeEvent<HTMLInputElement>) => {
- const files = Array.from(event.currentTarget.files ?? []);
-
- event.currentTarget.value = '';
-
- if (files.length > 0) {
- void actions.uploadFiles(files);
- }
- },
- [actions]
- );
-
- const openUploadPicker = useCallback(() => uploadInputRef.current?.click(), []);
+ const { inputProps: uploadInputProps, openPicker: openUploadPicker } = useGalleryUploadInput(actions.uploadFiles);
const handleUploadKeyDown = useCallback(
(event: KeyboardEvent) => {
@@ -364,14 +349,7 @@ export const GalleryImageGrid = () => {
</Flex>
) : (
<Flex align="stretch" h="full" minH="8rem" p="2">
- <input
- accept={ACCEPTED_UPLOAD_EXTENSIONS}
- multiple
- ref={uploadInputRef}
- style={UPLOAD_INPUT_STYLE}
- type="file"
- onChange={handleUploadFileChange}
- />
+ <input {...uploadInputProps} />
<DropZone
alignItems="center"
cursor="pointer"
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryUploadButton.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryUploadButton.tsx
index 7fdbfde981..3fb4308807 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryUploadButton.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryUploadButton.tsx
@@ -6,11 +6,9 @@ import { isDateBoardId } from '@features/gallery/data/backend';
import { IconButton } from '@platform/ui/Button';
import { Tooltip } from '@platform/ui/Tooltip';
import { UploadIcon } from 'lucide-react';
-import { useCallback, useRef } from 'react';
import { useTranslation } from 'react-i18next';
-export const ACCEPTED_UPLOAD_EXTENSIONS = 'image/png,image/jpeg,image/webp,video/mp4,.png,.jpg,.jpeg,.webp,.mp4';
-export const UPLOAD_INPUT_STYLE = { display: 'none' } as const;
+import { useGalleryUploadInput } from './useGalleryUploadInput';
export const GalleryUploadButton = ({
boards,
@@ -22,7 +20,6 @@ export const GalleryUploadButton = ({
onUploadFiles: (files: File[]) => Promise<void>;
}) => {
const { t } = useTranslation();
- const fileInputRef = useRef<HTMLInputElement>(null);
const selectedBoard = boards.find((board) => board.id === selectedBoardId);
const isVirtualTarget = isDateBoardId(selectedBoardId);
const label = isVirtualTarget
@@ -31,31 +28,11 @@ export const GalleryUploadButton = ({
name: selectedBoard ? getGalleryBoardLabel(selectedBoard, t) : t('widgets.gallery.selectedBoardFallback'),
});
- const handleFileChange = useCallback(
- (event: React.ChangeEvent<HTMLInputElement>) => {
- const files = Array.from(event.currentTarget.files ?? []);
-
- event.currentTarget.value = '';
-
- if (files.length > 0) {
- void onUploadFiles(files);
- }
- },
- [onUploadFiles]
- );
-
- const handleUploadClick = useCallback(() => fileInputRef.current?.click(), []);
+ const { inputProps, openPicker } = useGalleryUploadInput(onUploadFiles);
return (
<>
- <input
- accept={ACCEPTED_UPLOAD_EXTENSIONS}
- multiple
- ref={fileInputRef}
- style={UPLOAD_INPUT_STYLE}
- type="file"
- onChange={handleFileChange}
- />
+ <input {...inputProps} />
<Tooltip content={label}>
<IconButton
aria-label={label}
@@ -63,7 +40,7 @@ export const GalleryUploadButton = ({
disabled={isVirtualTarget}
size="2xs"
variant="ghost"
- onClick={handleUploadClick}
+ onClick={openPicker}
>
<Icon as={UploadIcon} boxSize="3.5" />
</IconButton>
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryUploadInput.ts b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryUploadInput.ts
new file mode 100644
index 0000000000..e04a41990a
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/useGalleryUploadInput.ts
@@ -0,0 +1,36 @@
+import { useCallback, useRef, type ChangeEvent } from 'react';
+
+export const ACCEPTED_UPLOAD_EXTENSIONS = 'image/png,image/jpeg,image/webp,video/mp4,.png,.jpg,.jpeg,.webp,.mp4';
+export const UPLOAD_INPUT_STYLE = { display: 'none' } as const;
+
+/** The hidden-input picker every gallery upload trigger shares: change extracts files, resets the input (so re-picking the same file fires), and forwards. */
+export const useGalleryUploadInput = (onUploadFiles: (files: File[]) => Promise<void> | void) => {
+ const inputRef = useRef<HTMLInputElement | null>(null);
+
+ const onChange = useCallback(
+ (event: ChangeEvent<HTMLInputElement>) => {
+ const files = Array.from(event.currentTarget.files ?? []);
+
+ event.currentTarget.value = '';
+
+ if (files.length > 0) {
+ void onUploadFiles(files);
+ }
+ },
+ [onUploadFiles]
+ );
+
+ const openPicker = useCallback(() => inputRef.current?.click(), []);
+
+ return {
+ inputProps: {
+ accept: ACCEPTED_UPLOAD_EXTENSIONS,
+ multiple: true as const,
+ onChange,
+ ref: inputRef,
+ style: UPLOAD_INPUT_STYLE,
+ type: 'file' as const,
+ },
+ openPicker,
+ };
+};