diff --git a/frontend/libs/api/src/lib/MediaUploadApi.ts b/frontend/libs/api/src/lib/MediaUploadApi.ts index 5a3edd3499..3685e6e000 100644 --- a/frontend/libs/api/src/lib/MediaUploadApi.ts +++ b/frontend/libs/api/src/lib/MediaUploadApi.ts @@ -249,6 +249,7 @@ export class MediaUploadApi extends ApiManager { maybe_visibility = Visibility.Public, maybe_style_name, maybe_scene_source_media_file_token, + is_intermediate_system_file = EIntermediateFile.true, }: { blob: Blob; fileName: string; @@ -257,10 +258,11 @@ export class MediaUploadApi extends ApiManager { maybe_visibility?: Visibility; maybe_style_name?: string; maybe_scene_source_media_file_token?: string; + is_intermediate_system_file?: EIntermediateFile; }): Promise> { const endpoint = `${this.getApiSchemeAndHost()}/v1/media_files/upload/new_video`; const options: Record = { - is_intermediate_system_file: "true", + is_intermediate_system_file: EIntermediateFile[is_intermediate_system_file], maybe_title, maybe_visibility: maybe_visibility?.toString(), maybe_style_name, @@ -364,6 +366,7 @@ export const UploadImageMedia: UploadMediaFn = async ({ blob: assetFile, fileName: getFileName(assetFile), maybe_title: "char_frame_" + title, + is_intermediate_system_file: EIntermediateFile.false, }); if (!res?.success || !res.data) { @@ -390,6 +393,7 @@ export const UploadVideoMedia: UploadMediaFn = async ({ blob: assetFile, fileName: getFileName(assetFile), maybe_title: "ref_video_" + title, + is_intermediate_system_file: EIntermediateFile.false, }); if (!res?.success || !res.data) { diff --git a/frontend/libs/components/gallery-modal/src/lib/GalleryDraggableItem.tsx b/frontend/libs/components/gallery-modal/src/lib/GalleryDraggableItem.tsx index 97c9f3a94a..f393d0f20a 100644 --- a/frontend/libs/components/gallery-modal/src/lib/GalleryDraggableItem.tsx +++ b/frontend/libs/components/gallery-modal/src/lib/GalleryDraggableItem.tsx @@ -5,6 +5,7 @@ import { faEllipsis, faPencil, faTrashCan, + faUpload, } from "@fortawesome/pro-solid-svg-icons"; import { twMerge } from "tailwind-merge"; import galleryDnd from "./galleryDnd"; @@ -16,6 +17,7 @@ import { isActionReminderOpen, } from "@storyteller/ui-action-reminder-modal"; import { MediaFileDelete } from "@storyteller/tauri-api"; +import { toast } from "@storyteller/ui-toaster"; type ModalMode = "select" | "view"; @@ -70,8 +72,22 @@ export const GalleryDraggableItem: React.FC = ({ primaryActionBtnClassName: "bg-red text-white hover:bg-red/90", onPrimaryAction: async () => { try { - await MediaFileDelete(item.id); + const result = await MediaFileDelete(item.id); + console.log("MediaFileDelete result:", result); + if (result && "status" in result && result.status !== "success") { + const msg = + (result as any).error_message || "Failed to delete media."; + console.error("MediaFileDelete failed:", result); + toast.error(msg); + return; + } onDeleted?.(item.id); + toast.success("Deleted."); + } catch (err) { + console.error("MediaFileDelete threw:", err); + toast.error( + `Failed to delete media: ${err instanceof Error ? err.message : String(err)}`, + ); } finally { isActionReminderOpen.value = false; } @@ -136,8 +152,7 @@ export const GalleryDraggableItem: React.FC = ({ type="button" tabIndex={-1} className={twMerge( - "w-full group relative overflow-visible rounded-md border-[3px] transition-all focus:outline-none", - item.mediaClass === "video" ? "aspect-video" : "aspect-square", + "w-full group relative overflow-visible rounded-md border-[3px] transition-all focus:outline-none aspect-square", selected || bulkSelected ? "border-primary" : disableTooltipAndBadge @@ -171,7 +186,7 @@ export const GalleryDraggableItem: React.FC = ({ alt={item.label} className={twMerge( "h-full w-full bg-black/30", - item.mediaClass === "video" || imageFit === "contain" ? "object-contain" : "object-cover", + imageFit === "contain" ? "object-contain" : "object-cover", )} draggable={false} onError={onImageError} @@ -187,7 +202,7 @@ export const GalleryDraggableItem: React.FC = ({ ); return ( -
+
{/* dropdown menu */} {mode !== "select" && (
= ({ {item.mediaClass === "dimensional" ? "3D" : item.mediaClass}
)} + {/* Upload badge — bottom-right (always visible, even in select mode) */} + {item.isUpload && ( +
+ +
+ )} {/* Conditionally wrap with Tooltip — hidden when selecting or in bulk mode */} {showTooltip ? ( { const PAGE_SIZE = 100; +const SHOW_UPLOADS_STORAGE_KEY = "gallery-modal-show-uploads"; + /** Small thumbnail used in the bulk-selection footer bar. */ const BulkThumb = ({ thumbnail, @@ -394,6 +397,21 @@ export const GalleryModal = React.memo( ); const gridColumns = maxColumns - (sliderValue - minColumns); const [imageFit, setImageFit] = useState<"cover" | "contain">("contain"); + const [showUploads, setShowUploads] = useState(() => { + try { + const stored = localStorage.getItem(SHOW_UPLOADS_STORAGE_KEY); + return stored === null ? false : stored === "true"; + } catch { + return false; + } + }); + useEffect(() => { + try { + localStorage.setItem(SHOW_UPLOADS_STORAGE_KEY, String(showUploads)); + } catch { + // ignore + } + }, [showUploads]); const [allItems, setAllItems] = useState([]); const [pageIndex, setPageIndex] = useState(0); const [hasMore, setHasMore] = useState(true); @@ -507,6 +525,7 @@ export const GalleryModal = React.memo( mediaClass: item.media_class || (item.filter_media_classes ? item.filter_media_classes[0] : "image"), + isUpload: item.origin_category === "upload", assetType: item.media_class === "dimensional" ? item.maybe_animation_type || @@ -557,19 +576,20 @@ export const GalleryModal = React.memo( if (reset) { // Smart merge: update existing items in-place (preserves - // scroll position), then prepend any genuinely new items. + // scroll position), prepend brand-new items, and DROP items + // the server no longer returns (e.g. just-deleted). setAllItems((prev) => { if (prev.length === 0) return newItems; const existingIds = new Set(prev.map((it) => it.id)); const brandNew = newItems.filter( (it: GalleryItem) => !existingIds.has(it.id), ); - // Build lookup for fresh data so we can update in-place const freshMap = new Map( - newItems.map((it: GalleryItem) => [it.id, it]), + newItems.map((it: GalleryItem) => [it.id, it] as const), ); - // Update existing items in their current positions - const updated = prev.map((it) => freshMap.get(it.id) || it); + const updated = prev + .map((it) => freshMap.get(it.id)) + .filter((it): it is GalleryItem => it !== undefined); return [...brandNew, ...updated]; }); } else { @@ -675,6 +695,33 @@ export const GalleryModal = React.memo( }; }, [refreshGallery, username]); + // Drop any item deleted anywhere in the app (lightbox, single, bulk) + // so it disappears from the gallery immediately. + useEffect(() => { + const unlistenPromise = listen("media_file_deleted_event", (event) => { + const token: string | undefined = + event?.payload?.data?.media_file_token ?? + event?.payload?.media_file_token; + if (!token) return; + setAllItems((prev) => prev.filter((it) => it.id !== token)); + setBulkSelectedIds((prev) => { + if (!prev.has(token)) return prev; + const next = new Set(prev); + next.delete(token); + return next; + }); + for (const [key, entry] of galleryCacheMap.entries()) { + galleryCacheMap.set(key, { + ...entry, + items: entry.items.filter((it) => it.id !== token), + }); + } + }); + return () => { + unlistenPromise.then((fn) => fn()); + }; + }, []); + const toggleBulkSelect = useCallback((id: string) => { setBulkSelectedIds((prev) => { const next = new Set(prev); @@ -727,26 +774,23 @@ export const GalleryModal = React.memo( const handleItemDeleted = useCallback( (id: string) => { - setAllItems((prev) => { - const updated = prev.filter((it) => it.id !== id); - // Remove from cache too - const cacheKey = getCacheKey(); - const cached = galleryCacheMap.get(cacheKey); - if (cached) { - galleryCacheMap.set(cacheKey, { - ...cached, - items: cached.items.filter((it) => it.id !== id), - }); - } - return updated; - }); + // Drop from local state (functional setter — must stay pure). + setAllItems((prev) => prev.filter((it) => it.id !== id)); setBulkSelectedIds((prev) => { const next = new Set(prev); next.delete(id); return next; }); + // Drop from the module-level cache for every filter so reopening + // the modal under any filter doesn't resurrect the item. + for (const [key, entry] of galleryCacheMap.entries()) { + galleryCacheMap.set(key, { + ...entry, + items: entry.items.filter((it) => it.id !== id), + }); + } }, - [getCacheKey], + [], ); const clearBulkSelection = useCallback(() => { @@ -776,28 +820,21 @@ export const GalleryModal = React.memo( try { const ids = Array.from(bulkSelectedIds); await Promise.allSettled(ids.map((id) => MediaFileDelete(id))); - setAllItems((prev) => { - const updated = prev.filter((it) => !bulkSelectedIds.has(it.id)); - // Evict from cache too - const cacheKey = getCacheKey(); - const cached = galleryCacheMap.get(cacheKey); - if (cached) { - galleryCacheMap.set(cacheKey, { - ...cached, - items: cached.items.filter( - (it) => !bulkSelectedIds.has(it.id), - ), - }); - } - return updated; - }); + const idSet = new Set(ids); + setAllItems((prev) => prev.filter((it) => !idSet.has(it.id))); + for (const [key, entry] of galleryCacheMap.entries()) { + galleryCacheMap.set(key, { + ...entry, + items: entry.items.filter((it) => !idSet.has(it.id)), + }); + } clearBulkSelection(); } finally { isActionReminderOpen.value = false; } }, }); - }, [bulkSelectedIds, clearBulkSelection, getCacheKey]); + }, [bulkSelectedIds, clearBulkSelection]); useSignals(); @@ -822,6 +859,8 @@ export const GalleryModal = React.memo( const filterItem = useCallback( (item: GalleryItem) => { if ((item as any).mediaType === "scene_json") return false; + if (!showUploads && activeFilter !== "uploaded" && item.isUpload) + return false; if (activeFilter === "3d") return item.mediaClass === "dimensional"; if (activeFilter === "image") return item.mediaClass === "image"; if (activeFilter === "video") return item.mediaClass === "video"; @@ -833,7 +872,7 @@ export const GalleryModal = React.memo( } return true; }, - [activeFilter], + [activeFilter, showUploads], ); const flatFilteredItems = useMemo(() => { @@ -1011,6 +1050,23 @@ export const GalleryModal = React.memo(
)} + {activeFilter !== "uploaded" && ( +
+ setShowUploads(e.target.checked)} + className="h-4 w-4 cursor-pointer rounded-lg border-gray-300 bg-gray-700 text-primary focus:ring-primary" + /> + +
+ )}
{/* Refresh button */} @@ -1055,7 +1111,7 @@ export const GalleryModal = React.memo( {/* Slider */} -
+