@@ -13,17 +13,48 @@ import 'xterm/css/xterm.css';
1313import { ThemeProvider } from 'styled-components' ;
1414import { Tabs } from './Tabs' ;
1515import Buffer from './Buffer' ;
16- import { DEFAULT_SESSION } from './constants' ;
16+ import { DEFAULT_SESSION , SESSION_ID_REGEX } from './constants' ;
1717import { showSlog } from './lib/blit' ;
1818import { InfoButton } from './InfoButton' ;
19- import { scrySessions } from './lib/utils' ;
19+ import { scrySessions , pokeTask } from './lib/utils' ;
2020
2121const initSessions = async ( ) => {
2222 const response = await api . scry ( scrySessions ( ) ) ;
2323
2424 useTermState . getState ( ) . set ( ( state ) => {
2525 state . names = response . sort ( ) ;
2626 } ) ;
27+
28+ // if there is a query parameters called 'into',
29+ // select that session, creating it if necessary
30+ //
31+ let match = RegExp ( '[?&]into=([^&]*)' ) . exec ( window . location . search ) ;
32+ let agent = match && decodeURIComponent ( match [ 1 ] . replace ( / \+ / g, ' ' ) ) ;
33+ if ( agent && SESSION_ID_REGEX . test ( agent ) ) {
34+ let session : string = agent ;
35+ // the session already exists, so we can simply select it
36+ //
37+ if ( response . indexOf ( agent ) > - 1 ) {
38+ useTermState . getState ( ) . set ( ( state ) => {
39+ state . selected = session ;
40+ } ) ;
41+ }
42+ // the session does not yet exist, so we create it,
43+ // and connect it to the agent with the same name
44+ //
45+ else {
46+ try {
47+ await api . poke ( pokeTask ( session , { open : { term : agent , apps : [ ] } } ) ) ;
48+ useTermState . getState ( ) . set ( ( state ) => {
49+ state . names = [ session , ...state . names ] . sort ( ) ;
50+ state . selected = session ;
51+ state . sessions [ session ] = null ;
52+ } ) ;
53+ } catch ( error ) {
54+ console . log ( 'unable to create session:' , error ) ;
55+ }
56+ }
57+ }
2758} ;
2859
2960export default function TermApp ( ) {
0 commit comments