@@ -2,29 +2,41 @@ import type { DevframeRpcClient } from 'devframe/client'
22import type { OgSnapshot } from '../connect'
33import { connectDevframe } from 'devframe/client'
44import { computed , readonly , shallowRef } from 'vue'
5+ import { formatAddress , resolveAddress } from '../utils/address'
6+ import { useHostPage } from './useHostPage'
57
68export function useOgViewer ( ) {
79 const rpc = shallowRef < DevframeRpcClient | null > ( null )
8- const params = new URLSearchParams ( location . search )
9- const target = shallowRef ( params . get ( 'url' ) ?? '' )
1010 const snapshot = shallowRef < OgSnapshot | null > ( null )
1111 const loading = shallowRef ( false )
1212 const error = shallowRef < string | null > ( null )
1313 const isStatic = computed ( ( ) => rpc . value ?. connectionMeta . backend === 'static' )
14+ let latestRequest = 0
15+
16+ const host = useHostPage ( )
17+ const target = shallowRef ( host
18+ ? formatAddress ( host . href ( ) , host . origin )
19+ : new URLSearchParams ( location . search ) . get ( 'url' ) ?? '' )
1420
1521 async function inspect ( next = target . value ) : Promise < void > {
1622 if ( isStatic . value && snapshot . value )
1723 return
1824 target . value = next . trim ( )
25+ const request = ++ latestRequest
1926 loading . value = true
2027 error . value = null
2128 try {
2229 rpc . value ??= await connectDevframe ( )
2330 await rpc . value . ensureTrusted ( )
24- const result = await rpc . value . call ( 'devframes:plugin:og:resolve-metadata' , { url : target . value } )
31+ const result = await rpc . value . call ( 'devframes:plugin:og:resolve-metadata' , {
32+ url : resolveAddress ( target . value , host ?. origin ) ,
33+ } )
34+ // A page navigation can start a newer request before this one returns.
35+ if ( request !== latestRequest )
36+ return
2537 snapshot . value = result
2638 if ( ( ! target . value || isStatic . value ) && result . requestedUrl )
27- target . value = result . requestedUrl
39+ target . value = formatAddress ( result . requestedUrl , host ?. origin )
2840
2941 const url = new URL ( location . href )
3042 if ( target . value )
@@ -34,18 +46,37 @@ export function useOgViewer() {
3446 history . replaceState ( null , '' , url )
3547 }
3648 catch ( cause ) {
37- error . value = cause instanceof Error ? cause . message : String ( cause )
49+ if ( request === latestRequest ) {
50+ // Drop the last result, so the panel does not show it as the current page.
51+ snapshot . value = null
52+ error . value = cause instanceof Error ? cause . message : String ( cause )
53+ }
3854 }
3955 finally {
40- loading . value = false
56+ if ( request === latestRequest )
57+ loading . value = false
4158 }
4259 }
4360
61+ host ?. onNavigate ( ( href ) => {
62+ // Follow the page only while the address is a page path, not a typed remote URL.
63+ const address = formatAddress ( href , host . origin )
64+ if ( ! isStatic . value && target . value . startsWith ( '/' ) && address !== target . value )
65+ void inspect ( address )
66+ } )
67+
68+ /** Read the current page path again, for a router the browser cannot report. */
69+ function refresh ( ) : Promise < void > {
70+ return inspect ( host ? formatAddress ( host . href ( ) , host . origin ) : target . value )
71+ }
72+
4473 return {
4574 error : readonly ( error ) ,
4675 inspect,
76+ isEmbedded : host !== null ,
4777 isStatic,
4878 loading : readonly ( loading ) ,
79+ refresh,
4980 snapshot : readonly ( snapshot ) ,
5081 target,
5182 }
0 commit comments