@@ -104,6 +104,48 @@ function resolveBaseStorage(): { storage: StateStorage; durable: boolean } {
104104
105105const { storage : baseStashStorage , durable : storageIsDurable } = resolveBaseStorage ( ) ;
106106
107+ interface StashEntryImages {
108+ attachments : ReadonlyArray < PersistedComposerImageAttachment > ;
109+ droppedImageNames : ReadonlyArray < string > ;
110+ unreadableImageNames : ReadonlyArray < string > ;
111+ }
112+
113+ /**
114+ * Finalized images storage refused (quota), by entry id. Storage gets the
115+ * entry with those images listed as dropped, so a reload or another tab
116+ * reports them missing; this tab keeps them until it reloads, as the stash
117+ * toast promises.
118+ */
119+ const unsavedImagesByEntryId = new Map < string , StashEntryImages > ( ) ;
120+
121+ function withUnsavedImages ( entry : PromptStashEntry ) : PromptStashEntry {
122+ const images = unsavedImagesByEntryId . get ( entry . id ) ;
123+ return images ? { ...entry , ...images , pendingImageCount : 0 } : entry ;
124+ }
125+
126+ function withoutUnsavedImages ( entry : PromptStashEntry ) : PromptStashEntry {
127+ const images = unsavedImagesByEntryId . get ( entry . id ) ;
128+ if ( ! images ) return entry ;
129+ return {
130+ ...entry ,
131+ attachments : [ ] ,
132+ droppedImageNames : [
133+ ...images . droppedImageNames ,
134+ ...images . attachments . map ( ( attachment ) => attachment . name ) ,
135+ ] ,
136+ unreadableImageNames : images . unreadableImageNames ,
137+ pendingImageCount : 0 ,
138+ } ;
139+ }
140+
141+ /** Storage's entries as this tab shows them, forgetting images of entries now gone. */
142+ function visibleEntries ( entries : ReadonlyArray < PromptStashEntry > ) : ReadonlyArray < PromptStashEntry > {
143+ for ( const entryId of unsavedImagesByEntryId . keys ( ) ) {
144+ if ( ! entries . some ( ( entry ) => entry . id === entryId ) ) unsavedImagesByEntryId . delete ( entryId ) ;
145+ }
146+ return unsavedImagesByEntryId . size === 0 ? entries : entries . map ( withUnsavedImages ) ;
147+ }
148+
107149/**
108150 * Persists the queue, immediately rather than debounced. Stashing is a
109151 * deliberate, infrequent keystroke — not a per-character autosave — so there
@@ -119,7 +161,9 @@ function persistEntries(entries: ReadonlyArray<PromptStashEntry>): {
119161 PROMPT_STASH_STORAGE_KEY ,
120162 JSON . stringify ( {
121163 version : PROMPT_STASH_STORAGE_VERSION ,
122- state : { entries } ,
164+ state : {
165+ entries : unsavedImagesByEntryId . size === 0 ? entries : entries . map ( withoutUnsavedImages ) ,
166+ } ,
123167 } ) ,
124168 ) ;
125169 return { written : true , durable : storageIsDurable } ;
@@ -184,11 +228,7 @@ interface PromptStashStoreState {
184228 */
185229 finalizeEntryImages : (
186230 entryId : string ,
187- images : {
188- attachments : ReadonlyArray < PersistedComposerImageAttachment > ;
189- droppedImageNames : ReadonlyArray < string > ;
190- unreadableImageNames : ReadonlyArray < string > ;
191- } ,
231+ images : StashEntryImages ,
192232 ) => { attached : boolean ; durable : boolean } ;
193233}
194234
@@ -204,16 +244,17 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
204244 if ( ! written ) {
205245 return { evicted : null , written : false , durable : false } ;
206246 }
207- set ( ( ) => ( { entries : nextEntries } ) ) ;
247+ set ( ( ) => ( { entries : visibleEntries ( nextEntries ) } ) ) ;
208248 return { evicted, written : true , durable } ;
209249 } ,
210250 takeEntry : ( entryId ) => {
211251 const entries = latestEntries ( get ( ) . entries ) ;
212- const entry = entries . find ( ( candidate ) => candidate . id === entryId ) ?? null ;
213- if ( ! entry ) return { entry : null , durable : true } ;
252+ const stored = entries . find ( ( candidate ) => candidate . id === entryId ) ;
253+ if ( ! stored ) return { entry : null , durable : true } ;
254+ const entry = withUnsavedImages ( stored ) ;
214255 const nextEntries = entries . filter ( ( candidate ) => candidate . id !== entryId ) ;
215256 const { durable } = persistEntries ( nextEntries ) ;
216- set ( ( ) => ( { entries : nextEntries } ) ) ;
257+ set ( ( ) => ( { entries : visibleEntries ( nextEntries ) } ) ) ;
217258 return { entry, durable } ;
218259 } ,
219260 finalizeEntryImages : ( entryId , images ) => {
@@ -229,8 +270,18 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
229270 unreadableImageNames : images . unreadableImageNames ,
230271 pendingImageCount : 0 ,
231272 } ;
232- const { durable } = persistEntries ( nextEntries ) ;
233- set ( ( ) => ( { entries : nextEntries } ) ) ;
273+ const { written, durable } = persistEntries ( nextEntries ) ;
274+ if ( ! written ) {
275+ // The smaller write without the images settles the stored entry, so it
276+ // never shows "saving…" again with no encoder left to finish it.
277+ unsavedImagesByEntryId . set ( entryId , {
278+ attachments : images . attachments ,
279+ droppedImageNames : images . droppedImageNames ,
280+ unreadableImageNames : images . unreadableImageNames ,
281+ } ) ;
282+ persistEntries ( nextEntries ) ;
283+ }
284+ set ( ( ) => ( { entries : visibleEntries ( nextEntries ) } ) ) ;
234285 return { attached : true , durable } ;
235286 } ,
236287} ) ) ;
@@ -247,7 +298,7 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
247298 if ( storageIsDurable && typeof window !== "undefined" ) {
248299 window . addEventListener ( "storage" , ( event ) => {
249300 if ( event . key !== PROMPT_STASH_STORAGE_KEY && event . key !== null ) return ;
250- usePromptStashStore . setState ( { entries : readPersistedEntries ( ) ?? [ ] } ) ;
301+ usePromptStashStore . setState ( { entries : visibleEntries ( readPersistedEntries ( ) ?? [ ] ) } ) ;
251302 } ) ;
252303 }
253304}
0 commit comments