@@ -2,10 +2,13 @@ import { filePreviewDelimiter } from "@t3tools/shared/delimitedPreview";
22import type { EnvironmentId , ScopedThreadRef } from "@t3tools/contracts" ;
33import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments" ;
44import { readFilePreviewResponse } from "@t3tools/client-runtime/file-preview" ;
5+ import { settlePromise } from "@t3tools/client-runtime/state/runtime" ;
6+ import * as AsyncResult from "effect/reactivity/AsyncResult" ;
7+ import * as Cause from "effect/Cause" ;
58import { filePreviewKind , FILE_TEXT_PREVIEW_MAX_BYTES } from "@t3tools/shared/filePreview" ;
69import { ChevronRightIcon , DownloadIcon , Trash2Icon , WrapTextIcon , XIcon } from "lucide-react" ;
710import { Check , Code2 , Copy , Eye , Table2 } from "lucide" ;
8- import { lazy , Suspense , useEffect , useMemo , useRef , useState } from "react" ;
11+ import { lazy , Suspense , useEffect , useRef , useState } from "react" ;
912
1013import { useAssetUrlRefresh } from "~/assets/assetUrls" ;
1114import ChatMarkdown from "~/components/ChatMarkdown" ;
@@ -45,6 +48,26 @@ function renderedToggleLabel(mode: "markdown" | "html" | "table", rendered: bool
4548 return rendered ? "Show HTML source" : "Show rendered page" ;
4649}
4750
51+ // useAssetUrlRefresh keys its callback on these objects, so they must stay cached.
52+ function useAttachmentResources (
53+ attachmentId : string | undefined ,
54+ fileName : string ,
55+ mimeType : string ,
56+ ) {
57+ "use memo" ;
58+ const resource = attachmentId
59+ ? {
60+ _tag : "attachment" as const ,
61+ attachmentId,
62+ fileName,
63+ mimeType,
64+ disposition : "inline" as const ,
65+ }
66+ : null ;
67+ const downloadResource = resource ? { ...resource , disposition : "attachment" as const } : null ;
68+ return { resource, downloadResource } ;
69+ }
70+
4871export function AttachmentFilePreview ( props : {
4972 name : string ;
5073 mimeType : string ;
@@ -63,27 +86,15 @@ export function AttachmentFilePreview(props: {
6386 kind === "markdown" ? "markdown" : kind === "html" ? "html" : delimiter ? "table" : null ;
6487 const attachmentId = props . asset ?. attachmentId ;
6588 const { copyToClipboard, isCopied } = useCopyToClipboard ( { target : props . name } ) ;
66- const resource = useMemo (
67- ( ) =>
68- attachmentId
69- ? {
70- _tag : "attachment" as const ,
71- attachmentId,
72- fileName : props . name ,
73- mimeType : props . mimeType ,
74- disposition : "inline" as const ,
75- }
76- : null ,
77- [ attachmentId , props . name , props . mimeType ] ,
89+ const { resource, downloadResource } = useAttachmentResources (
90+ attachmentId ,
91+ props . name ,
92+ props . mimeType ,
7893 ) ;
7994 const refresh = useAssetUrlRefresh (
8095 props . file ? null : ( props . asset ?. environmentId ?? null ) ,
8196 props . file ? null : resource ,
8297 ) ;
83- const downloadResource = useMemo (
84- ( ) => ( resource ? { ...resource , disposition : "attachment" as const } : null ) ,
85- [ resource ] ,
86- ) ;
8798 const prepareDownload = useAssetUrlRefresh ( props . asset ?. environmentId ?? null , downloadResource ) ;
8899 const [ saving , setSaving ] = useState ( false ) ;
89100 const [ localUrl , setLocalUrl ] = useState < string | null > ( null ) ;
@@ -172,37 +183,35 @@ export function AttachmentFilePreview(props: {
172183
173184 const save = ( ) => {
174185 setSaving ( true ) ;
175- void ( async ( ) => {
176- try {
177- let file = props . file ;
178- if ( ! file ) {
179- const target = await prepareDownload ( ) ;
180- if ( ! target ) throw new Error ( "Reconnect to the environment and try again." ) ;
181- const response = await fetch ( target ) ;
182- if ( ! response . ok ) throw new Error ( "The file could not be loaded. Try again." ) ;
183- file = await response . blob ( ) ;
184- }
185- // A Blob keeps cross-origin downloads inside the desktop client instead of
186- // navigating its custom app scheme to an external browser.
187- const downloadUrl = URL . createObjectURL ( file ) ;
188- try {
189- const anchor = document . createElement ( "a" ) ;
190- anchor . href = downloadUrl ;
191- anchor . download = props . name ;
192- anchor . click ( ) ;
193- } finally {
194- setTimeout ( ( ) => URL . revokeObjectURL ( downloadUrl ) , 30_000 ) ;
195- }
196- } catch ( cause ) {
186+ void settlePromise ( async ( ) => {
187+ let file = props . file ;
188+ if ( ! file ) {
189+ const target = await prepareDownload ( ) ;
190+ if ( ! target ) throw new Error ( "Reconnect to the environment and try again." ) ;
191+ const response = await fetch ( target ) ;
192+ if ( ! response . ok ) throw new Error ( "The file could not be loaded. Try again." ) ;
193+ file = await response . blob ( ) ;
194+ }
195+ // A Blob keeps cross-origin downloads inside the desktop client instead of
196+ // navigating its custom app scheme to an external browser.
197+ const downloadUrl = URL . createObjectURL ( file ) ;
198+ // Scheduled before the click so the URL is revoked even if the click throws.
199+ setTimeout ( ( ) => URL . revokeObjectURL ( downloadUrl ) , 30_000 ) ;
200+ const anchor = document . createElement ( "a" ) ;
201+ anchor . href = downloadUrl ;
202+ anchor . download = props . name ;
203+ anchor . click ( ) ;
204+ } ) . then ( ( result ) => {
205+ if ( ! AsyncResult . isSuccess ( result ) ) {
206+ const cause = Cause . squash ( result . cause ) ;
197207 toastManager . add ( {
198208 type : "error" ,
199209 title : "Could not save file" ,
200210 description : cause instanceof Error ? cause . message : "Please try again." ,
201211 } ) ;
202- } finally {
203- setSaving ( false ) ;
204212 }
205- } ) ( ) ;
213+ setSaving ( false ) ;
214+ } ) ;
206215 } ;
207216
208217 const body = failure ? (
0 commit comments