Skip to content

Commit ed7f8c5

Browse files
authored
Merge pull request #2 from urbit/m/app-session
ui: minimal viable automated session creation
2 parents c9d9228 + d603253 commit ed7f8c5

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

ui/App.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,48 @@ import 'xterm/css/xterm.css';
1313
import { ThemeProvider } from 'styled-components';
1414
import { Tabs } from './Tabs';
1515
import Buffer from './Buffer';
16-
import { DEFAULT_SESSION } from './constants';
16+
import { DEFAULT_SESSION, SESSION_ID_REGEX } from './constants';
1717
import { showSlog } from './lib/blit';
1818
import { InfoButton } from './InfoButton';
19-
import { scrySessions } from './lib/utils';
19+
import { scrySessions, pokeTask } from './lib/utils';
2020

2121
const 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

2960
export default function TermApp() {

0 commit comments

Comments
 (0)