@@ -64,6 +64,7 @@ import { ModelFeedback } from "./model-feedback.js";
6464const modelFeedback = new ModelFeedback ( ( ) => later ( ) ) ;
6565import { BodyWindows } from "./body-windows.js" ;
6666import { ThinContinuations } from "./thin-continuations.js" ;
67+ import { DraftAttachments } from "./draft-attachments.js" ;
6768import { ThinMailDraft , mailReplyField } from "./mail-drafts.js" ;
6869import { ThinViews , viewCapability } from "./thin.js" ;
6970import * as Wire from "./wire.js" ;
@@ -1109,7 +1110,15 @@ function dispatch(action, value, timer) {
11091110 if ( action === "hold-bottom" ) { holdBottom ( ) ; return true ; }
11101111 const layer = document . querySelector ( '[data-local-models="1"]' ) ;
11111112 const settled = layer ? modelFeedback . choice ( action , value , layer . style . display !== "none" ) : undefined ;
1112- return thin . request ( action , value , settled ) ;
1113+ const thread = currentSession ( ) . sel ;
1114+ const attachments = [ "detach" , "send" , "send-alt" ] . includes ( action ) ? draftAttachmentPaths ( currentSession ( ) , thread ) : [ ] ;
1115+ return thin . request ( action , value , accepted => {
1116+ settled ?. ( accepted ) ;
1117+ if ( accepted && attachments . length ) {
1118+ const kept = draftAttachmentPaths ( currentSession ( ) , thread ) ;
1119+ void draftAttachments . forget ( thread , attachments . filter ( path => ! kept . includes ( path ) ) ) . catch ( ( ) => { } ) ;
1120+ }
1121+ } ) ;
11131122 }
11141123 if ( ! timer ) showViewStatus ( "Saved conversation is read-only until connected." ) ;
11151124 return true ;
@@ -1219,6 +1228,17 @@ function inputValue(el) {
12191228// Files: each goes whole as the body of one POST /attach to the hub (the
12201229// pairing cookie carries the token); "attach-up" and "attach-end" tell
12211230// app.bend it started and what the hub answered, and it decides the rest
1231+ const draftAttachments = new DraftAttachments ( ) ;
1232+ function draftAttachmentPaths ( session , thread ) {
1233+ return ( session . drafts ?. [ "@att:" + thread ] || "" ) . split ( "\n" ) . filter ( Boolean ) ;
1234+ }
1235+ function restoreDraftAttachments ( session ) {
1236+ const scope = uploadScope ( ) , thread = session . sel ;
1237+ if ( ! scope . controller ?. ready || scope . thread !== thread || session . drafts ?. [ "@att.up:" + thread ] ) return ;
1238+ void draftAttachments . sync ( thread , draftAttachmentPaths ( session , thread ) ,
1239+ ( file , previous ) => upload ( "attach" , file , scope , previous ) ,
1240+ ( ) => thin === scope . controller && thin . ready && currentSession ( ) . sel === thread ) . catch ( ( ) => { } ) ;
1241+ }
12221242function uploadScope ( ) {
12231243 const controller = viewMode === "compatibility" ? null : thin ;
12241244 return { mode : viewMode , controller,
@@ -1229,7 +1249,7 @@ function uploadScope() {
12291249async function uploadFiles ( action , files , scope = uploadScope ( ) ) {
12301250 for ( const file of files ) await upload ( action , file , scope ) ;
12311251}
1232- async function upload ( action , file , scope = uploadScope ( ) ) {
1252+ async function upload ( action , file , scope = uploadScope ( ) , previous = "" ) {
12331253 const key = Array . from ( crypto . getRandomValues ( new Uint8Array ( 4 ) ) , ( b ) => b . toString ( 16 ) . padStart ( 2 , "0" ) ) . join ( "" ) ;
12341254 const name = file . name || "pasted." + ( ( file . type || "" ) . split ( "/" ) [ 1 ] || "bin" ) ;
12351255 if ( file . size === 0 ) return ;
@@ -1256,6 +1276,13 @@ async function upload(action, file, scope = uploadScope()) {
12561276 answer = JSON . stringify ( { text : "the upload did not reach the hub" , thread, upload : true } ) ;
12571277 }
12581278 if ( ! current ( ) ) return ;
1279+ let result ; try { result = JSON . parse ( answer ) ; } catch { return ; }
1280+ if ( result . attached && result . thread === thread ) {
1281+ try { await draftAttachments . remember ( thread , result . attached , file , previous ) ; } catch {
1282+ showViewStatus ( "This attachment could not be kept for reload. Send it before leaving this page." ) ;
1283+ }
1284+ }
1285+ if ( ! current ( ) ) return ;
12591286 if ( viewMode === "compatibility" ) dispatch ( "attach-end" , answer ) ;
12601287 else if ( thin === uploadController ) {
12611288 // The HTTP body and completion stay bound to the original chosen thread,
@@ -1268,6 +1295,7 @@ async function upload(action, file, scope = uploadScope()) {
12681295 // of this completion. A retired view never retargets it into a new actor.
12691296 await uploadController . perform ( "attach-end" , answer ) ;
12701297 }
1298+ return result . attached && result . thread === thread ? result . attached : undefined ;
12711299}
12721300
12731301document . addEventListener ( "change" , ( e ) => {
@@ -2420,6 +2448,7 @@ async function startThin(again = false, reason = "initial") {
24202448 ui = App . unsync ( App . presentation_handoff ( ui , presentationJson ( session ) ) ) ;
24212449 document . documentElement . lang = App . lang ( ui ) ;
24222450 keepViewer ( ) ;
2451+ setTimeout ( ( ) => restoreDraftAttachments ( currentSession ( ) ) , 0 ) ;
24232452 // A hub action can open a thread too (Ctrl+N, Fork, the palette).
24242453 // Keep the accepted selection as well as explicit sidebar clicks.
24252454 try { localStorage . setItem ( "backplane-selected" , session . sel || "" ) ; } catch { }
0 commit comments