Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/ui/src/components/site-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { clsx } from 'clsx';
import {
useEffect,
useMemo,
useRef,
useState,
type MouseEvent,
type PointerEvent as ReactPointerEvent,
Expand Down Expand Up @@ -456,6 +457,15 @@ function SiteSection( {
} ) {
const { site, latestSession } = row;
const navigate = useNavigate();
const sectionRef = useRef< HTMLElement >( null );
const isActive = isChatActive || isContextActive;
// Keep the active site visible — e.g. when launch restores a site that
// sits below the sidebar's fold. `nearest` no-ops when already visible.
useEffect( () => {
if ( isActive ) {
sectionRef.current?.scrollIntoView?.( { block: 'nearest' } );
}
}, [ isActive ] );
const isStarting = useIsSiteStarting( site.id );
const isStopping = useIsSiteStopping( site.id );
const { status } = deriveSiteStatus( site, isStarting, isStopping );
Expand Down Expand Up @@ -494,6 +504,7 @@ function SiteSection( {

return (
<section
ref={ sectionRef }
className={ clsx(
styles.site,
isChatActive && styles.siteActive,
Expand Down
3 changes: 3 additions & 0 deletions apps/ui/src/components/site-list/style.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
.site {
display: flex;
flex-direction: column;
/* Keep scrollIntoView targets clear of the sidebar's sticky header and
footer, which paint over the scrollport's edges. */
scroll-margin-block: 64px;
}

.siteHeader {
Expand Down
32 changes: 32 additions & 0 deletions apps/ui/src/lib/last-visited.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Remembers the most recently visited site so the `/` index route can return
* the user to it, instead of always landing on the first site in the list.
* The id is validated against live data at redirect time, so a stale id
* (deleted site) just falls through.
*/

export interface LastVisited {
siteId?: string;
}

const STORAGE_KEY = 'studio-ui-last-visited-v1';

export function readLastVisited(): LastVisited {
try {
const stored = window.localStorage.getItem( STORAGE_KEY );
const parsed = stored ? JSON.parse( stored ) : {};
return {
siteId: typeof parsed.siteId === 'string' ? parsed.siteId : undefined,
};
} catch {
return {};
}
}

export function writeLastVisited( next: LastVisited ): void {
try {
window.localStorage.setItem( STORAGE_KEY, JSON.stringify( next ) );
} catch {
// Best-effort; without it the index route falls back to the first site.
}
}
10 changes: 10 additions & 0 deletions apps/ui/src/ui-classic/router/layout-dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useSessionPreviewAnnotationsHandler,
useSessionPreviewUI,
} from '@/hooks/use-session-ui';
import { writeLastVisited } from '@/lib/last-visited';
import { rootRoute } from '../layout-root';

// Only session detail routes host the preview; on every other route
Expand Down Expand Up @@ -78,6 +79,15 @@ function DashboardLayoutContent() {
setLastPreviewSiteId( routeSite.id );
}
}, [ routeSite ] );
// Remember the user's site so the `/` index route can return here
// instead of defaulting to the first site.
const sessionSiteId = sessionSite?.id;
useEffect( () => {
const siteId = sessionSiteId ?? newSessionSiteId;
if ( siteId ) {
writeLastVisited( { siteId } );
}
}, [ sessionSiteId, newSessionSiteId ] );
const lastPreviewSite = lastPreviewSiteId
? sites?.find( ( site ) => site.id === lastPreviewSiteId )
: undefined;
Expand Down
114 changes: 114 additions & 0 deletions apps/ui/src/ui-classic/router/route-index/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { writeLastVisited } from '@/lib/last-visited';
import { indexRoute } from './index';
import type { AiSessionSummary, SiteDetails } from '@/data/core';

function createSite( overrides: Partial< SiteDetails > = {} ): SiteDetails {
return {
id: 'site-1',
name: 'Site One',
path: '/Users/example/Studio/site-one',
port: 8881,
running: false,
phpVersion: '8.2',
...overrides,
};
}

function createSession( overrides: Partial< AiSessionSummary > = {} ): AiSessionSummary {
return {
id: 'session-1',
filePath: '/tmp/session.jsonl',
createdAt: '2026-06-26T11:00:00.000Z',
updatedAt: '2026-06-26T11:00:00.000Z',
ownerSiteId: 'site-1',
ownerSitePath: '/Users/example/Studio/site-one',
ownerSiteName: 'Site One',
activeEnvironment: 'local',
eventCount: 1,
...overrides,
};
}

async function runBeforeLoad( sites: SiteDetails[], sessions: AiSessionSummary[] ) {
const context = {
queryClient: {
fetchQuery: ( { queryFn }: { queryFn: () => unknown } ) => queryFn(),
},
connector: {
getSites: async () => sites,
getSessions: async () => sessions,
getAuthUser: async () => null,
getOnboardingCompleted: async () => true,
},
};
try {
await indexRoute.options.beforeLoad?.( { context } as never );
} catch ( thrown ) {
return ( thrown as { options: { to: string; params?: Record< string, string > } } ).options;
}
throw new Error( 'Expected beforeLoad to throw a redirect' );
}

describe( 'indexRoute.beforeLoad', () => {
beforeEach( () => {
window.localStorage.clear();
} );

it( 'redirects to onboarding when there are no sites', async () => {
const redirect = await runBeforeLoad( [], [] );
expect( redirect.to ).toBe( '/onboarding' );
} );

it( "redirects to the last visited site's newest active session", async () => {
writeLastVisited( { siteId: 'site-2' } );
const redirect = await runBeforeLoad(
[ createSite(), createSite( { id: 'site-2', path: '/Users/example/Studio/site-two' } ) ],
[
createSession(),
createSession( { id: 'session-2', ownerSiteId: 'site-2', archived: true } ),
createSession( { id: 'session-3', ownerSiteId: 'site-2' } ),
]
);
expect( redirect.to ).toBe( '/sessions/$sessionId' );
expect( redirect.params ).toEqual( { sessionId: 'session-3' } );
} );

it( 'falls back to the top site in sidebar order when the last visited site was deleted', async () => {
writeLastVisited( { siteId: 'deleted-site' } );
const redirect = await runBeforeLoad(
[
createSite(),
createSite( { id: 'site-2', path: '/Users/example/Studio/site-two', sortOrder: 1000 } ),
],
[ createSession(), createSession( { id: 'session-2', ownerSiteId: 'site-2' } ) ]
);
expect( redirect.to ).toBe( '/sessions/$sessionId' );
expect( redirect.params ).toEqual( { sessionId: 'session-2' } );
} );

it( 'falls back to the top site in sidebar order, not fetch order, when nothing is stored', async () => {
const redirect = await runBeforeLoad(
[
createSite(),
createSite( { id: 'site-2', path: '/Users/example/Studio/site-two', sortOrder: 1000 } ),
],
[ createSession(), createSession( { id: 'session-2', ownerSiteId: 'site-2' } ) ]
);
expect( redirect.to ).toBe( '/sessions/$sessionId' );
expect( redirect.params ).toEqual( { sessionId: 'session-2' } );
} );

it( 'redirects to a new session when the target site has no active sessions', async () => {
writeLastVisited( { siteId: 'site-2' } );
const redirect = await runBeforeLoad(
[ createSite(), createSite( { id: 'site-2', path: '/Users/example/Studio/site-two' } ) ],
[
createSession(),
createSession( { id: 'session-2', ownerSiteId: 'site-2', archived: true } ),
]
);
expect( redirect.to ).toBe( '/sites/$siteId/new' );
expect( redirect.params ).toEqual( { siteId: 'site-2' } );
} );
} );
22 changes: 20 additions & 2 deletions apps/ui/src/ui-classic/router/route-index/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { aiSessionBelongsToSite } from '@studio/common/ai/sessions/owner-site';
import { sortSites } from '@studio/common/lib/sort-sites';
import { createRoute, redirect } from '@tanstack/react-router';
import { resolveAgenticFeatures } from '@/data/queries/use-agentic-features';
import { SESSIONS_QUERY_KEY } from '@/data/queries/use-sessions';
import { SITES_QUERY_KEY } from '@/data/queries/use-sites';
import { readLastVisited } from '@/lib/last-visited';
import { rootRoute } from '../layout-root';

export const indexRoute = createRoute( {
Expand Down Expand Up @@ -31,11 +33,27 @@ export const indexRoute = createRoute( {
queryKey: SESSIONS_QUERY_KEY,
queryFn: () => context.connector.getSessions(),
} );
const topSession = sessions.find( ( session ) => aiSessionBelongsToSite( session, firstSite ) );

// Return the user to their last visited site (recorded by the dashboard
// layout), validating against live data so a stale id from a deleted
// site falls through to the sidebar's top site — not the raw fetch
// order. `sortSites` sorts in place, so sort a copy.
const lastVisited = readLastVisited();
const targetSite =
( lastVisited.siteId && sites.find( ( site ) => site.id === lastVisited.siteId ) ) ||
sortSites( [ ...sites ] )[ 0 ];

// Sessions arrive sorted newest-first, so the first session owned by
// the site is its most recently updated active one.
const topSession = sessions.find(
( session ) => ! session.archived && aiSessionBelongsToSite( session, targetSite )
);
if ( topSession ) {
throw redirect( { to: '/sessions/$sessionId', params: { sessionId: topSession.id } } );
}

throw redirect( { to: '/sites/$siteId/new', params: { siteId: firstSite.id } } );
// No sessions yet: the new-session route creates (or reuses) an empty
// session for the site and redirects to it.
throw redirect( { to: '/sites/$siteId/new', params: { siteId: targetSite.id } } );
},
} );
Loading