@@ -4,19 +4,18 @@ import {
44 type MediaReference ,
55} from "@t3tools/client-runtime/media-reference" ;
66import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets" ;
7- import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
7+ import { settlePromise , squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
88import {
99 AuthFilesystemReadScope ,
1010 type AssetResource ,
11- type AuthSessionState ,
1211 type ContextMenuItem ,
1312 type EnvironmentId ,
1413 sessionGrantsScope ,
1514 type SessionGrantInput ,
1615} from "@t3tools/contracts" ;
1716import * as Option from "effect/Option" ;
1817import { AsyncResult } from "effect/reactivity" ;
19- import { useCallback , useRef , useState , type ReactElement } from "react" ;
18+ import { useRef , useState , type ReactElement } from "react" ;
2019
2120import { writeTextToClipboard } from "../../hooks/useCopyToClipboard" ;
2221import { readLocalApi } from "../../localApi" ;
@@ -66,16 +65,16 @@ export function useMediaActions(source: MediaActionSource) {
6665 const canReadMedia =
6766 hostEnvironmentId === null ||
6867 ( fileSession . error === null && fileSession . data !== null && allowsHostMedia ( fileSession . data ) ) ;
69- const assertCanReadMedia = useCallback ( ( ) => {
68+ const assertCanReadMedia = ( ) => {
7069 if ( ! canReadHostMedia ( hostEnvironmentId ) ) {
7170 throw new Error ( "This connection cannot read host files." ) ;
7271 }
73- } , [ hostEnvironmentId ] ) ;
72+ } ;
7473 const createAssetUrl = useAtomQueryRunner ( assetEnvironment . createUrl , {
7574 reportFailure : false ,
7675 refresh : true ,
7776 } ) ;
78- const actionUrl = useCallback ( async ( ) => {
77+ const actionUrl = async ( ) => {
7978 assertCanReadMedia ( ) ;
8079 if ( ! source . asset ) {
8180 if ( ! source . src ) throw new Error ( "This media is unavailable. Try reopening the preview." ) ;
@@ -90,11 +89,11 @@ export function useMediaActions(source: MediaActionSource) {
9089 const url = resolveAssetUrl ( connection . httpBaseUrl , result . value . relativeUrl ) ;
9190 if ( ! url ) throw new Error ( "The environment returned an invalid media URL." ) ;
9291 return url ;
93- } , [ source , createAssetUrl , assertCanReadMedia ] ) ;
94- const save = useCallback ( async ( ) => {
92+ } ;
93+ const save = async ( ) => {
9594 await downloadMedia ( await actionUrl ( ) , mediaFileName ( source ) ) ;
96- } , [ actionUrl , source ] ) ;
97- const copyImage = useCallback ( async ( ) => {
95+ } ;
96+ const copyImage = async ( ) => {
9897 assertCanReadMedia ( ) ;
9998 if ( ! navigator . clipboard ?. write || typeof ClipboardItem === "undefined" ) {
10099 throw new Error (
@@ -105,7 +104,7 @@ export function useMediaActions(source: MediaActionSource) {
105104 await navigator . clipboard . write ( [
106105 new ClipboardItem ( { "image/png" : actionUrl ( ) . then ( readMediaPng ) } ) ,
107106 ] ) ;
108- } , [ actionUrl , assertCanReadMedia ] ) ;
107+ } ;
109108 return { save, copyImage, canReadMedia, assertCanReadMedia } ;
110109}
111110
@@ -129,7 +128,7 @@ export function MediaActions({
129128 setTooltipOpen ( false ) ;
130129 let failureTitle = "Could not open media menu" ;
131130 let progressToast : ReturnType < typeof toastManager . add > | undefined ;
132- try {
131+ const result = await settlePromise ( async ( ) => {
133132 const noun = source . kind === "image" ? "image" : "video" ;
134133 const unavailable = ! canReadMedia || ( source . src === null && source . asset === undefined ) ;
135134 const canCopyImage =
@@ -186,17 +185,18 @@ export function MediaActions({
186185 title : action === "save" ? "Download started" : "Image copied" ,
187186 } ) ;
188187 }
189- } catch ( error ) {
188+ } ) ;
189+ if ( AsyncResult . isFailure ( result ) ) {
190+ const error = squashAtomCommandFailure ( result ) ;
190191 const toast = stackedThreadToast ( {
191192 type : "error" ,
192193 title : failureTitle ,
193194 description : error instanceof Error ? error . message : "The media action failed." ,
194195 } ) ;
195196 if ( progressToast ) toastManager . update ( progressToast , toast ) ;
196197 else toastManager . add ( toast ) ;
197- } finally {
198- menuOpen . current = false ;
199198 }
199+ menuOpen . current = false ;
200200 } ;
201201
202202 return (
0 commit comments