@@ -2,7 +2,6 @@ import * as Haptics from "expo-haptics";
22import { KeyboardAwareLegendList } from "@legendapp/list/keyboard" ;
33import { useViewabilityAmount , type LegendListRef } from "@legendapp/list/react-native" ;
44import type {
5- AssetResource ,
65 ChatAttachment ,
76 ChatFileAttachment ,
87 ChatImageAttachment ,
@@ -68,7 +67,6 @@ import {
6867 type ColorValue ,
6968 useWindowDimensions ,
7069 View ,
71- type ViewStyle ,
7270} from "react-native" ;
7371import { FilePreviewModal , type FilePreviewSource } from "../../components/FilePreviewModal" ;
7472import { isPdfFile } from "../../lib/filePreview" ;
@@ -102,8 +100,6 @@ import { VideoPreviewModal, type VideoPreviewSource } from "../../components/Vid
102100import { VideoAttachmentTile } from "../../components/VideoAttachmentTile" ;
103101import { MediaVideoPlayer } from "../../components/MediaVideoPlayer" ;
104102import { resolveMarkdownMediaPreview } from "../../lib/markdownMedia" ;
105- import { useMediaActions , type MediaActionsSource } from "../../lib/mediaActions" ;
106- import { MediaActionsMenu } from "../../components/MediaActionsMenu" ;
107103import {
108104 attachmentVideoPreviewSource ,
109105 mediaVideoPreviewUri ,
@@ -178,8 +174,12 @@ import {
178174 isAbsolutePath ,
179175 resolveWorkspaceRelativeFilePath ,
180176} from "../files/filePath" ;
181- import { MARKDOWN_IMAGE_MAX_WIDTH , resolveMarkdownImageDisplaySize } from "./markdownImageSize" ;
182177import { fileChipMenu , resolveFileChipTarget , type FileChipAction } from "./fileChipMenu" ;
178+ import {
179+ ThreadMarkdownImage ,
180+ ThreadMarkdownImageUnavailable ,
181+ ThreadMarkdownImageView ,
182+ } from "./ThreadMarkdownImage" ;
183183
184184const WIDE_MARKDOWN_BLOCK_OPTIONS = {
185185 // Native iOS blockquotes and adjacent selectable text are separate layout
@@ -505,171 +505,6 @@ function MessageAttachmentUnknown(props: { readonly name: string }) {
505505 ) ;
506506}
507507
508- function ThreadMarkdownImageView ( props : {
509- readonly uri : string | null ;
510- readonly sourceKey : string ;
511- readonly unavailable : boolean ;
512- readonly alt : string | null ;
513- readonly actionsSource ?: MediaActionsSource ;
514- readonly onPressPreview : ( source : FilePreviewSource ) => void ;
515- } ) {
516- const sourceIdentifier = useId ( ) ;
517- const mediaActions = useMediaActions ( props . actionsSource ) ;
518- const [ availableWidth , setAvailableWidth ] = useState ( 0 ) ;
519- const [ sourceSize , setSourceSize ] = useState < { width : number ; height : number } | null > ( null ) ;
520- const [ failedUri , setFailedUri ] = useState < string | null > ( null ) ;
521-
522- useEffect ( ( ) => {
523- setSourceSize ( null ) ;
524- } , [ props . sourceKey ] ) ;
525-
526- useEffect ( ( ) => {
527- setFailedUri ( null ) ;
528- } , [ props . uri ] ) ;
529-
530- const displaySize =
531- sourceSize === null
532- ? null
533- : resolveMarkdownImageDisplaySize ( {
534- sourceWidth : sourceSize . width ,
535- sourceHeight : sourceSize . height ,
536- availableWidth,
537- } ) ;
538- const failed = props . unavailable || ( props . uri !== null && failedUri === props . uri ) ;
539- const placeholderWidth : ViewStyle [ "width" ] =
540- availableWidth > 0 ? Math . min ( availableWidth , MARKDOWN_IMAGE_MAX_WIDTH ) : "100%" ;
541- const frameStyle : ViewStyle = displaySize ?? { width : placeholderWidth , aspectRatio : 16 / 9 } ;
542-
543- return (
544- < View
545- onLayout = { ( event ) => setAvailableWidth ( event . nativeEvent . layout . width ) }
546- style = { { alignSelf : "stretch" , gap : 6 } }
547- >
548- { props . uri === null || failed ? (
549- < MediaActionsMenu media = { mediaActions } >
550- < Pressable
551- accessibilityRole = "imagebutton"
552- accessibilityLabel = { props . alt ?? "Markdown image" }
553- accessibilityHint = {
554- mediaActions . actions . length > 0 ? "Touch and hold for media actions" : undefined
555- }
556- className = "items-center justify-center rounded-[10px] bg-md-code-bg"
557- style = { frameStyle }
558- >
559- { failed ? (
560- < Text className = "text-xs text-foreground-muted" > Image unavailable</ Text >
561- ) : (
562- < ActivityIndicator />
563- ) }
564- </ Pressable >
565- </ MediaActionsMenu >
566- ) : (
567- < PresentationSource identifier = { sourceIdentifier } style = { { alignSelf : "flex-start" } } >
568- < MediaActionsMenu media = { mediaActions } >
569- < Pressable
570- accessibilityRole = "imagebutton"
571- accessibilityLabel = { props . alt ?? "Markdown image" }
572- accessibilityHint = {
573- mediaActions . actions . length > 0 ? "Touch and hold for media actions" : undefined
574- }
575- onPress = { ( ) =>
576- // Quick Look picks the viewer from the name's extension, so it needs the
577- // file name rather than the alt text.
578- props . onPressPreview ( {
579- kind : "image" ,
580- uri : props . uri ! ,
581- name : props . actionsSource ?. name ?? props . alt ?? "Image" ,
582- sourceIdentifier,
583- actionsSource : props . actionsSource ,
584- } )
585- }
586- style = { { alignSelf : "flex-start" } }
587- >
588- < View
589- className = "items-center justify-center overflow-hidden rounded-[10px] bg-md-code-bg"
590- style = { {
591- ...frameStyle ,
592- } }
593- >
594- < ThreadMarkdownImageRequest
595- key = { props . uri }
596- uri = { props . uri }
597- onLoad = { setSourceSize }
598- onError = { ( ) => setFailedUri ( props . uri ) }
599- />
600- </ View >
601- </ Pressable >
602- </ MediaActionsMenu >
603- </ PresentationSource >
604- ) }
605- { props . alt ? (
606- < Text selectable className = "text-xs text-foreground-muted" >
607- { props . alt }
608- </ Text >
609- ) : null }
610- </ View >
611- ) ;
612- }
613-
614- function ThreadMarkdownImageRequest ( props : {
615- readonly uri : string ;
616- readonly onLoad : ( sourceSize : { width : number ; height : number } ) => void ;
617- readonly onError : ( ) => void ;
618- } ) {
619- const [ loaded , setLoaded ] = useState ( false ) ;
620-
621- return (
622- < >
623- < Image
624- source = { { uri : props . uri } }
625- resizeMode = "contain"
626- accessible = { false }
627- onLoad = { ( event ) => {
628- setLoaded ( true ) ;
629- props . onLoad ( event . nativeEvent . source ) ;
630- } }
631- onError = { props . onError }
632- style = { { width : "100%" , height : "100%" , opacity : loaded ? 1 : 0 } }
633- />
634- { loaded ? null : (
635- < View
636- pointerEvents = "none"
637- style = { [ StyleSheet . absoluteFill , { alignItems : "center" , justifyContent : "center" } ] }
638- >
639- < Text className = "text-xs text-foreground-muted" > Loading image…</ Text >
640- </ View >
641- ) }
642- </ >
643- ) ;
644- }
645-
646- /** Environment-hosted image that loads through a signed asset URL. */
647- function ThreadMarkdownImage ( props : {
648- readonly environmentId : EnvironmentId ;
649- readonly resource : Extract < AssetResource , { readonly _tag : "attachment" | "media-file" } > ;
650- readonly alt : string | null ;
651- readonly srcFragment ?: string ;
652- readonly actionsSource ?: MediaActionsSource ;
653- readonly onPressPreview : ( source : FilePreviewSource ) => void ;
654- } ) {
655- const assetUrl = useAssetUrlState ( props . environmentId , props . resource ) ;
656-
657- return (
658- < ThreadMarkdownImageView
659- uri = { assetUrl . _tag === "Success" ? assetUrl . url + ( props . srcFragment ?? "" ) : null }
660- sourceKey = {
661- props . resource . _tag === "attachment"
662- ? `attachment:${ props . resource . attachmentId } `
663- : `workspace:${ props . resource . path } `
664- }
665- unavailable = { assetUrl . _tag === "Failure" }
666- alt = { props . alt }
667- actionsSource = { props . actionsSource }
668- onPressPreview = { props . onPressPreview }
669- />
670- ) ;
671- }
672-
673508const ThreadMediaVisibleContext = createContext ( false ) ;
674509// LegendList only computes hook visibility when the list has a viewability config.
675510const THREAD_MEDIA_VIEWABILITY_CONFIG = { itemVisiblePercentThreshold : 0 } ;
@@ -713,18 +548,6 @@ function ThreadMarkdownVideo(props: { readonly source: MediaVideoPreviewSource }
713548 ) ;
714549}
715550
716- function ThreadMarkdownImageUnavailable ( props : { readonly alt : string | null } ) {
717- return (
718- < ThreadMarkdownImageView
719- uri = { null }
720- sourceKey = "unavailable"
721- unavailable
722- alt = { props . alt }
723- onPressPreview = { ( ) => undefined }
724- />
725- ) ;
726- }
727-
728551const MARKDOWN_MONO_FONT = Platform . select ( {
729552 ios : "ui-monospace" ,
730553 android : "monospace" ,
0 commit comments