@@ -17,6 +17,8 @@ import { createSessionActivity } from "./session-activity"
1717import { createServerEvents } from "./event-source"
1818import { resolveProjectName } from "./project-name"
1919import type { GlobalSession } from "@opencode-ai/sdk/v2"
20+ import type { Event } from "@opencode-ai/sdk/v2"
21+ import { useLocation } from "@solidjs/router"
2022export { linkUrl } from "./link-url"
2123
2224export type DotState = "pending" | "busy" | "unread" | "idle"
@@ -63,6 +65,7 @@ type DashboardCtx = {
6365const Ctx = createContext < DashboardCtx > ( )
6466
6567export function DashboardProvider ( props : ParentProps ) {
68+ const route = useLocation ( )
6669 const [ allProjects , setAllProjectsRaw ] = createSignal ( localStorage . getItem ( "es-app-all-projects" ) === "true" )
6770 const setAllProjects = ( value : boolean ) => {
6871 localStorage . setItem ( "es-app-all-projects" , String ( value ) )
@@ -83,16 +86,91 @@ export function DashboardProvider(props: ParentProps) {
8386 ( name ) => es . state ( name || undefined ) ,
8487 )
8588 const [ sessionsError , setSessionsError ] = createSignal ( "" )
86- const [ globalSessions , { refetch : refetchGlobal } ] = createResource < GlobalSession [ ] > ( async ( _ , info ) => {
89+ let revision = 0
90+ const changed = new Map < string , { revision : number ; session : GlobalSession | null } > ( )
91+ const merge = ( sessions : GlobalSession [ ] , updates : Iterable < [ string , { session : GlobalSession | null } ] > ) => {
92+ const rows = new Map ( sessions . map ( ( session ) => [ session . id , session ] ) )
93+ for ( const [ id , value ] of updates ) {
94+ if ( value . session ) rows . set ( id , value . session )
95+ else rows . delete ( id )
96+ }
97+ return [ ...rows . values ( ) ]
98+ }
99+ const [ globalSessions , { refetch : refetchGlobal , mutate : mutateGlobal } ] = createResource < GlobalSession [ ] > ( async ( _ , info ) => {
87100 setSessionsError ( "" )
101+ const before = revision
88102 try {
89- return await oc . allSessions ( false )
103+ const sessions = await oc . allSessions ( false )
104+ const updates = [ ...changed ] . filter ( ( [ , item ] ) => item . revision > before )
105+ for ( const [ id , item ] of changed ) if ( item . revision <= before ) changed . delete ( id )
106+ return merge ( sessions , updates )
90107 } catch {
91108 setSessionsError ( "Could not load session relationships. Subagent activity and requests may be incomplete. Retrying automatically." )
92- return info . value ?? [ ]
109+ return merge ( info . value ?? [ ] , changed )
93110 }
94111 } )
95- const activity = createSessionActivity ( ( ) => globalSessions ( ) ?? [ ] , refetchGlobal )
112+ const [ visibleSessions , setVisibleSessions ] = createSignal ( new Set < string > ( ) )
113+ const PIN_KEY = "es-app-sidebar-pins"
114+ const pinKey = ( s : Pick < SessionRow , "id" | "directory" > ) => JSON . stringify ( [ s . directory , s . id ] )
115+ const loadPins = ( ) => {
116+ try {
117+ const value : unknown = JSON . parse ( localStorage . getItem ( PIN_KEY ) ?? "[]" )
118+ return Array . isArray ( value ) ? [ ...new Set ( value . filter ( ( key ) : key is string => typeof key === "string" ) ) ] : [ ]
119+ } catch {
120+ return [ ]
121+ }
122+ }
123+ const [ pins , setPins ] = createSignal < string [ ] > ( loadPins ( ) )
124+ const relevantDirectories = ( ) => {
125+ const rows = globalSessions ( ) ?? [ ]
126+ const selected = / ^ \/ s e s s i o n \/ ( [ ^ / ] + ) / . exec ( route . pathname ) ?. [ 1 ]
127+ const ids = new Set ( [
128+ ...visibleSessions ( ) ,
129+ ...pins ( ) . flatMap ( ( key ) => rows . filter ( ( row ) => pinKey ( row ) === key ) . map ( ( row ) => row . id ) ) ,
130+ ...( selected ? [ selected ] : [ ] ) ,
131+ ...( state ( ) ?. attention ?? [ ] ) . flatMap ( ( item : { session ?: string } ) => item . session ? [ item . session ] : [ ] ) ,
132+ ...( notificationState ( ) ?. notifications ?? [ ] ) . map ( ( item ) => item . session ) ,
133+ ...( state ( ) ?. threads ?? [ ] ) . flatMap ( ( thread : { sessions ?: { id : string ; live ?: string } [ ] } ) =>
134+ ( thread . sessions ?? [ ] ) . filter ( ( session ) => session . live === "busy" || session . live === "retry" ) . map ( ( session ) => session . id ) ) ,
135+ ] )
136+ // A visible root needs its descendants' requests even when they have no sidebar rows.
137+ const children = new Map < string , string [ ] > ( )
138+ for ( const row of rows ) {
139+ if ( ! row . parentID ) continue
140+ children . set ( row . parentID , [ ...( children . get ( row . parentID ) ?? [ ] ) , row . id ] )
141+ }
142+ const queue = [ ...ids ]
143+ for ( const id of queue ) for ( const child of children . get ( id ) ?? [ ] ) {
144+ if ( ids . has ( child ) ) continue
145+ ids . add ( child )
146+ queue . push ( child )
147+ }
148+ const dirs = rows . filter ( ( row ) => ids . has ( row . id ) ) . map ( ( row ) => row . directory )
149+ const direct = new URLSearchParams ( route . search ) . get ( "directory" )
150+ if ( selected && direct ) dirs . push ( direct )
151+ dirs . push ( ...( notificationState ( ) ?. notifications ?? [ ] ) . map ( ( item ) => item . directory ) )
152+ return [ ...new Set ( dirs . filter ( Boolean ) ) ]
153+ }
154+ const sessionEvent = ( event : Event , directory : string ) => {
155+ if ( event . type !== "session.created" && event . type !== "session.updated" && event . type !== "session.deleted" ) return false
156+ const info = event . properties . info
157+ if ( ! info || typeof info !== "object" || ! ( "id" in info ) || info . id !== event . properties . sessionID ||
158+ ! ( "directory" in info ) || info . directory !== directory || ! info . id ) return false
159+ const removed = event . type === "session.deleted"
160+ if ( ! removed && ( ! ( "title" in info ) || typeof info . title !== "string" ||
161+ ! ( "time" in info ) || ! info . time || typeof info . time . updated !== "number" ||
162+ ! ( "projectID" in info ) || typeof info . projectID !== "string" ||
163+ ! ( "slug" in info ) || typeof info . slug !== "string" ||
164+ ! ( "version" in info ) || typeof info . version !== "string" ) ) return false
165+ const session = removed ? null : info as GlobalSession
166+ changed . set ( info . id , { revision : ++ revision , session } )
167+ mutateGlobal ( ( previous ) => merge ( previous ?? [ ] , [ [ info . id , { session } ] ] ) )
168+ return true
169+ }
170+ const activity = createSessionActivity ( ( ) => globalSessions ( ) ?? [ ] , refetchGlobal , {
171+ directories : relevantDirectories ,
172+ sessionEvent,
173+ } )
96174
97175 // one SSE subscription per selected editspace
98176 createEffect ( ( ) => {
@@ -112,7 +190,6 @@ export function DashboardProvider(props: ParentProps) {
112190 const [ viewed , setViewed ] = createSignal < Record < string , number > > (
113191 JSON . parse ( localStorage . getItem ( VIEWED_KEY ) ?? "{}" ) ,
114192 )
115- const [ visibleSessions , setVisibleSessions ] = createSignal ( new Set < string > ( ) )
116193 const [ idleSeen , setIdleSeen ] = createSignal < Record < string , number > > (
117194 JSON . parse ( localStorage . getItem ( "es-app-idle-seen" ) ?? "{}" ) ,
118195 )
@@ -197,17 +274,6 @@ export function DashboardProvider(props: ParentProps) {
197274 . filter ( ( s ) => ! archivedIds ( ) . has ( s . id ) )
198275 . sort ( ( a , b ) => b . updated - a . updated ) ,
199276 )
200- const PIN_KEY = "es-app-sidebar-pins"
201- const pinKey = ( s : Pick < SessionRow , "id" | "directory" > ) => JSON . stringify ( [ s . directory , s . id ] )
202- const loadPins = ( ) => {
203- try {
204- const value : unknown = JSON . parse ( localStorage . getItem ( PIN_KEY ) ?? "[]" )
205- return Array . isArray ( value ) ? [ ...new Set ( value . filter ( ( key ) : key is string => typeof key === "string" ) ) ] : [ ]
206- } catch {
207- return [ ]
208- }
209- }
210- const [ pins , setPins ] = createSignal < string [ ] > ( loadPins ( ) )
211277 const isPinned = ( s : Pick < SessionRow , "id" | "directory" > ) => pins ( ) . includes ( pinKey ( s ) )
212278 const togglePin = ( s : Pick < SessionRow , "id" | "directory" > ) => {
213279 const key = pinKey ( s )
0 commit comments