@@ -6,16 +6,23 @@ import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"
66import {
77 PullRequestMedia ,
88 PullRequestMediaContext ,
9- usePullRequestImageSource ,
109 usePullRequestMediaUrl ,
1110} from "./PullRequestMedia" ;
1211import { videoMimeType } from "@t3tools/shared/video" ;
13- import { createContext , useContext , useMemo , useState } from "react" ;
12+ import { createContext , useCallback , useContext , useMemo , useState } from "react" ;
1413import {
1514 changeRequestLinkResolver ,
1615 changeRequestReferenceAutolinker ,
1716} from "@t3tools/client-runtime/pull-request-markdown" ;
18- import type { ReferenceAutolink } from "@t3tools/client-runtime/source-control-clients" ;
17+ import {
18+ sourceControlClients ,
19+ type ReferenceAutolink ,
20+ } from "@t3tools/client-runtime/source-control-clients" ;
21+ import { SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts" ;
22+ import { assetUrlStateFromResult } from "@t3tools/client-runtime/state/assets" ;
23+ import { assetEnvironment } from "../../state/assets" ;
24+ import { usePreparedConnection } from "../../state/session" ;
25+ import { useAtomQueryRunner } from "../../state/use-atom-query-runner" ;
1926import { linkReferenceAutolinks } from "./pullRequestAutolinks" ;
2027
2128import { useOpenPullRequest } from "../../native/useOpenPullRequest" ;
@@ -30,7 +37,10 @@ import {
3037 type FilePreviewItem ,
3138} from "../../components/FilePreviewModal" ;
3239import { ThreadMarkdownImageView } from "../threads/ThreadMarkdownImage" ;
33- import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText" ;
40+ import type {
41+ MarkdownImageRenderer ,
42+ MarkdownImageSourceResolver ,
43+ } from "../../native/SelectableMarkdownText" ;
3444
3545const AutolinksContext = createContext < ( ( text : string ) => ReadonlyArray < ReferenceAutolink > ) | null > (
3646 null ,
@@ -66,6 +76,29 @@ export function PullRequestMarkdownRepository(props: {
6676 ) ;
6777}
6878
79+ /** Images Enriched draws natively, such as one inside a list item, load like the standalone ones. */
80+ function usePullRequestImageSource ( resolve : ( url : string ) => string ) : MarkdownImageSourceResolver {
81+ const context = useContext ( PullRequestMediaContext ) ;
82+ const connection = usePreparedConnection ( context ?. environmentId ?? null ) ;
83+ const httpBaseUrl = connection . _tag === "Some" ? connection . value . httpBaseUrl : null ;
84+ const createUrl = useAtomQueryRunner ( assetEnvironment . createUrl , { reportFailure : false } ) ;
85+ return useCallback (
86+ async ( image ) => {
87+ const url = resolve ( image . href ) ;
88+ const fetchUrl = sourceControlClients . mediaUrl ( url ) ?. fetchUrl ;
89+ if ( ! context || ! fetchUrl ) return / ^ h t t p s ? : \/ \/ / i. test ( url ) ? { uri : url } : null ;
90+ const resource = { _tag : SOURCE_CONTROL_MEDIA_ASSET , cwd : context . cwd , url : fetchUrl } ;
91+ const state = assetUrlStateFromResult (
92+ await createUrl ( { environmentId : context . environmentId , input : { resource } } ) ,
93+ httpBaseUrl ,
94+ ) ;
95+ if ( state . _tag !== "Success" ) throw new Error ( "Pull request image is unavailable" ) ;
96+ return { uri : state . url } ;
97+ } ,
98+ [ context , createUrl , httpBaseUrl , resolve ] ,
99+ ) ;
100+ }
101+
69102function MarkdownImage ( props : {
70103 readonly href : string ;
71104 readonly alt : string | null ;
0 commit comments