@@ -2,8 +2,9 @@ import * as Dialog from "./ui/Dialog";
22import { ComposerContextId } from "@t3tools/contracts" ;
33import { useAtomValue } from "@effect/atom-react" ;
44import { AsyncResult } from "effect/reactivity" ;
5+ import { settlePromise , squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
56import { useNavigation } from "@react-navigation/native" ;
6- import { useEffect , useMemo , useRef , useState } from "react" ;
7+ import { useEffect , useRef , useState } from "react" ;
78import type { EnvironmentId } from "@t3tools/contracts" ;
89import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard" ;
910import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences" ;
@@ -54,7 +55,7 @@ export function ComposerEditor({
5455 const preferredEnterBehavior = AsyncResult . isSuccess ( preferencesResult )
5556 ? preferencesResult . value . composerEnterBehavior
5657 : undefined ;
57- const contextHistory = useMemo ( ( ) => createComposerDraftContextHistory ( ) , [ draftKey ] ) ;
58+ const contextHistory = createComposerDraftContextHistory ( ) ;
5859 useEffect ( ( ) => ( ) => contextHistory . dispose ( ) , [ contextHistory ] ) ;
5960 const changeText = ( text : string ) => {
6061 const restored = contextHistory . restore (
@@ -87,7 +88,7 @@ export function ComposerEditor({
8788 importRef . current = controller ;
8889 setImporting ( true ) ;
8990 setComposerContextImporting ( draftKey , true ) ;
90- try {
91+ const settled = await settlePromise ( async ( ) => {
9192 const retained = getComposerDraftAfterSelection ( draftKey , insertion ) ;
9293 const result = await importComposerContextClipboard (
9394 clipboard ,
@@ -111,41 +112,36 @@ export function ComposerEditor({
111112 "Some attachments could not be copied" ,
112113 "Reconnect to the source environment and copy them again. References without their files are marked unavailable." ,
113114 ) ;
114- } catch ( error ) {
115- if ( ! controller . signal . aborted )
116- Dialog . alert (
117- "Could not paste context" ,
118- error instanceof Error ? error . message : "Try copying again." ,
119- ) ;
120- } finally {
121- setComposerContextImporting ( draftKey , false ) ;
122- importRef . current = null ;
123- setImporting ( false ) ;
115+ } ) ;
116+ setComposerContextImporting ( draftKey , false ) ;
117+ importRef . current = null ;
118+ setImporting ( false ) ;
119+ if ( AsyncResult . isFailure ( settled ) && ! controller . signal . aborted ) {
120+ const error = squashAtomCommandFailure ( settled ) ;
121+ Dialog . alert (
122+ "Could not paste context" ,
123+ error instanceof Error ? error . message : "Try copying again." ,
124+ ) ;
124125 }
125126 } ;
126- const clipboardFragment = useMemo (
127- ( ) =>
128- environmentId && draft . context
129- ? encodeComposerContextFragment ( {
130- version : 1 ,
131- source : { environmentId } ,
132- records : draft . context . records . map ( ( record ) => {
133- if ( ! ( "attachmentId" in record ) ) return record ;
134- const attachment = draft . attachments . find (
135- ( entry ) => entry . id === record . attachmentId ,
136- ) ;
137- return {
138- ...record ,
139- attachmentId :
140- attachment ?. uploadEnvironmentId === environmentId
141- ? ( attachment . uploadedAttachmentId ?? record . attachmentId )
142- : record . attachmentId ,
143- } ;
144- } ) ,
145- } )
146- : "" ,
147- [ environmentId , draft . context , draft . attachments ] ,
148- ) ;
127+ const clipboardFragment =
128+ environmentId && draft . context
129+ ? encodeComposerContextFragment ( {
130+ version : 1 ,
131+ source : { environmentId } ,
132+ records : draft . context . records . map ( ( record ) => {
133+ if ( ! ( "attachmentId" in record ) ) return record ;
134+ const attachment = draft . attachments . find ( ( entry ) => entry . id === record . attachmentId ) ;
135+ return {
136+ ...record ,
137+ attachmentId :
138+ attachment ?. uploadEnvironmentId === environmentId
139+ ? ( attachment . uploadedAttachmentId ?? record . attachmentId )
140+ : record . attachmentId ,
141+ } ;
142+ } ) ,
143+ } )
144+ : "" ;
149145 const navigation = useNavigation ( ) ;
150146 const selectedReference = selected
151147 ? collectComposerContextReferences ( selected . source ) [ 0 ]
0 commit comments