@@ -17,11 +17,13 @@ import {
1717} from "@t3tools/contracts" ;
1818import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
1919import {
20+ guessThreadLinkRef ,
2021 hasThreadLinks ,
2122 parseThreadLinkHref ,
2223 relabelThreadLinks ,
23- resolveThreadLinkRef ,
24- threadLinkEnvironmentIds ,
24+ threadLinkCandidates ,
25+ threadLinkRefs ,
26+ type ThreadLink ,
2527} from "@t3tools/shared/threadLinks" ;
2628import * as Option from "effect/Option" ;
2729import * as Schema from "effect/Schema" ;
@@ -47,41 +49,35 @@ const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(
4749const EMPTY_THREAD_ATOM = Atom . make < EnvironmentThread | null > ( null ) . pipe (
4850 Atom . withLabel ( "mobile-thread:empty" ) ,
4951) ;
50- const EMPTY_THREAD_TITLES : ReadonlyMap < ThreadId , string > = new Map ( ) ;
51- const EMPTY_THREAD_LINK_TITLES_ATOM = Atom . make (
52- new Map < EnvironmentId , ReadonlyMap < ThreadId , string > > ( ) ,
53- ) . pipe ( Atom . withLabel ( "mobile-thread-link-titles:empty" ) ) ;
52+ const EMPTY_THREAD_LINK_TITLES_ATOM = Atom . make < ReadonlyMap < string , string > > ( new Map ( ) ) . pipe (
53+ Atom . withLabel ( "mobile-thread-link-titles:empty" ) ,
54+ ) ;
5455
55- const threadTitlesAtom = Atom . family ( ( environmentId : EnvironmentId ) => {
56- let previous = EMPTY_THREAD_TITLES ;
57- return Atom . make ( ( get ) => {
58- const index = get ( environmentThreadShells . environmentThreadIndexAtom ( environmentId ) ) ;
59- const unchanged =
60- index . size === previous . size &&
61- Array . from ( index ) . every ( ( [ threadId , shell ] ) => previous . get ( threadId ) === shell . title ) ;
62- if ( ! unchanged ) {
63- previous = new Map ( Array . from ( index , ( [ threadId , shell ] ) => [ threadId , shell . title ] ) ) ;
64- }
65- return previous ;
66- } ) . pipe ( Atom . withLabel ( `mobile-thread-titles:${ environmentId } ` ) ) ;
56+ const decodeThreadKey = Schema . decodeUnknownSync ( Schema . Tuple ( [ EnvironmentId , ThreadId ] ) ) ;
57+ const threadKey = ( ref : ScopedThreadRef ) => JSON . stringify ( [ ref . environmentId , ref . threadId ] ) ;
58+
59+ // Null when this client doesn't know the thread. Keyed, so a link never walks an environment's threads.
60+ const threadTitleAtom = Atom . family ( ( key : string ) => {
61+ const ref = scopeThreadRef ( ...decodeThreadKey ( JSON . parse ( key ) ) ) ;
62+ return Atom . make ( ( get ) => get ( environmentThreadShells . threadShellAtom ( ref ) ) ?. title ?? null ) . pipe (
63+ Atom . withLabel ( `mobile-thread-title:${ key } ` ) ,
64+ ) ;
6765} ) ;
6866
69- const decodeEnvironmentIds = Schema . decodeUnknownSync ( Schema . Array ( EnvironmentId ) ) ;
67+ const decodeThreadKeys = Schema . decodeUnknownSync ( Schema . Array ( Schema . String ) ) ;
7068
7169const threadLinkTitlesAtom = Atom . family ( ( key : string ) => {
72- const environmentIds = decodeEnvironmentIds ( JSON . parse ( key ) ) ;
73- return Atom . make (
74- ( get ) =>
75- new Map (
76- environmentIds . map ( ( environmentId ) => [
77- environmentId ,
78- get ( threadTitlesAtom ( environmentId ) ) ,
79- ] ) ,
80- ) ,
81- ) . pipe ( Atom . withLabel ( `mobile-thread-link-titles:${ key } ` ) ) ;
70+ const refKeys = decodeThreadKeys ( JSON . parse ( key ) ) ;
71+ return Atom . make ( ( get ) => {
72+ const titles = new Map < string , string > ( ) ;
73+ for ( const refKey of refKeys ) {
74+ const title = get ( threadTitleAtom ( refKey ) ) ;
75+ if ( title !== null ) titles . set ( refKey , title ) ;
76+ }
77+ return titles ;
78+ } ) . pipe ( Atom . withLabel ( `mobile-thread-link-titles:${ key } ` ) ) ;
8279} ) ;
8380
84- const decodeThreadKey = Schema . decodeUnknownSync ( Schema . Tuple ( [ EnvironmentId , ThreadId ] ) ) ;
8581const decodeThreadChipTargets = Schema . decodeUnknownSync (
8682 Schema . Array (
8783 Schema . Struct ( {
@@ -135,18 +131,11 @@ const threadChipIconsAtom = Atom.family((key: string) => {
135131 return Atom . make ( ( get ) => {
136132 const icons = new Map < string , string > ( ) ;
137133 for ( const target of targets ) {
138- const source = scopeThreadRef ( target . environmentId , target . threadId ) ;
139134 const link = target . link ? parseThreadLinkHref ( target . href ) : null ;
140- const ref = link
141- ? ( resolveThreadLinkRef (
142- link ,
143- target . environmentId ,
144- get ( environmentThreadShells . threadRefsAtom ) ,
145- ) ?? source )
146- : source ;
147- const icon = get (
148- threadProjectChipIconAtom ( JSON . stringify ( [ ref . environmentId , ref . threadId ] ) ) ,
149- ) ;
135+ const ref =
136+ ( link ? resolveThreadLink ( get , link , target . environmentId ) : null ) ??
137+ scopeThreadRef ( target . environmentId , target . threadId ) ;
138+ const icon = get ( threadProjectChipIconAtom ( threadKey ( ref ) ) ) ;
150139 if ( icon ) icons . set ( target . href , icon ) ;
151140 }
152141 return icons ;
@@ -213,12 +202,25 @@ export function useChildThreadInputs(ref: ScopedThreadRef) {
213202export function useLiveThreadLinkLabels ( markdown : string , environmentId : EnvironmentId ) : string {
214203 const titles = useAtomValue (
215204 hasThreadLinks ( markdown )
216- ? threadLinkTitlesAtom ( JSON . stringify ( threadLinkEnvironmentIds ( markdown , environmentId ) ) )
205+ ? threadLinkTitlesAtom ( JSON . stringify ( threadLinkRefs ( markdown , environmentId ) . map ( threadKey ) ) )
217206 : EMPTY_THREAD_LINK_TITLES_ATOM ,
218207 ) ;
219- return relabelThreadLinks ( markdown , environmentId , ( ref ) =>
220- titles . get ( ref . environmentId ) ?. get ( ref . threadId ) ,
208+ return relabelThreadLinks ( markdown , environmentId , ( ref ) => titles . get ( threadKey ( ref ) ) ) ;
209+ }
210+
211+ /** Keyed reads; every thread is read only to place a v1 link its message's environment lacks. */
212+ export function resolveThreadLink (
213+ read : < A > ( atom : Atom . Atom < A > ) => A ,
214+ link : ThreadLink ,
215+ messageEnvironmentId : EnvironmentId ,
216+ ) : ScopedThreadRef | null {
217+ const known = threadLinkCandidates ( link , messageEnvironmentId ) . find (
218+ ( ref ) => read ( threadTitleAtom ( threadKey ( ref ) ) ) !== null ,
221219 ) ;
220+ if ( known ) return known ;
221+ return link . environmentId === null
222+ ? guessThreadLinkRef ( link , read ( environmentThreadShells . threadRefsAtom ) )
223+ : null ;
222224}
223225
224226export function useThreadChipIcons (
0 commit comments