2021-11-25 15:00:52 +03:00
|
|
|
import React, {
|
2022-03-03 02:34:19 +03:00
|
|
|
useCallback, useEffect
|
2021-11-25 15:00:52 +03:00
|
|
|
} from 'react';
|
|
|
|
|
2022-03-04 02:10:54 +03:00
|
|
|
import useTermState from './state';
|
|
|
|
import { useDark } from './lib/useDark';
|
2021-12-06 18:05:54 +03:00
|
|
|
import api from './api';
|
2021-11-25 15:00:52 +03:00
|
|
|
|
2022-03-04 02:10:54 +03:00
|
|
|
import { _dark, _light } from '@tlon/indigo-react';
|
2021-11-25 15:00:52 +03:00
|
|
|
|
2021-12-06 18:05:54 +03:00
|
|
|
import 'xterm/css/xterm.css';
|
|
|
|
|
2021-11-25 15:00:52 +03:00
|
|
|
import {
|
2022-03-03 02:34:19 +03:00
|
|
|
scrySessions
|
2021-12-08 19:12:38 +03:00
|
|
|
} from '@urbit/api/term';
|
2021-11-25 15:00:52 +03:00
|
|
|
|
2021-12-09 02:00:25 +03:00
|
|
|
import { ThemeProvider } from 'styled-components';
|
2022-03-03 02:34:19 +03:00
|
|
|
import { Tabs } from './Tabs';
|
|
|
|
import Buffer from './Buffer';
|
|
|
|
import { DEFAULT_SESSION } from './constants';
|
|
|
|
import { showSlog } from './lib/blit';
|
2022-04-01 16:59:39 +03:00
|
|
|
import { InfoButton } from './InfoButton';
|
2021-11-25 15:00:52 +03:00
|
|
|
|
2022-04-11 22:05:25 +03:00
|
|
|
const initSessions = async () => {
|
|
|
|
const response = await api.scry(scrySessions());
|
|
|
|
|
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.names = response.sort();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-10 01:25:10 +03:00
|
|
|
export default function TermApp() {
|
2022-03-03 02:34:19 +03:00
|
|
|
const { names, selected } = useTermState();
|
2021-11-25 15:00:52 +03:00
|
|
|
const dark = useDark();
|
|
|
|
|
|
|
|
const setupSlog = useCallback(() => {
|
|
|
|
console.log('slog: setting up...');
|
|
|
|
let available = false;
|
|
|
|
const slog = new EventSource('/~_~/slog', { withCredentials: true });
|
|
|
|
|
2022-03-10 01:25:10 +03:00
|
|
|
slog.onopen = () => {
|
2021-11-25 15:00:52 +03:00
|
|
|
console.log('slog: opened stream');
|
|
|
|
available = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
slog.onmessage = (e) => {
|
2022-03-03 02:34:19 +03:00
|
|
|
const session = useTermState.getState().sessions[DEFAULT_SESSION];
|
2021-11-25 15:00:52 +03:00
|
|
|
if (!session) {
|
2022-03-03 02:34:19 +03:00
|
|
|
console.log('slog: default session mia!', 'msg:', e.data);
|
|
|
|
console.log(Object.keys(useTermState.getState().sessions), session);
|
2021-11-25 15:00:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
showSlog(session.term, e.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
slog.onerror = (e) => {
|
|
|
|
console.error('slog: eventsource error:', e);
|
|
|
|
if (available) {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (slog.readyState !== EventSource.CLOSED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('slog: reconnecting...');
|
|
|
|
setupSlog();
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-03 02:34:19 +03:00
|
|
|
useTermState.getState().set((state) => {
|
2021-12-08 19:12:38 +03:00
|
|
|
state.slogstream = slog;
|
|
|
|
});
|
2022-03-03 02:34:19 +03:00
|
|
|
}, []);
|
2021-11-25 15:00:52 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-03 02:34:19 +03:00
|
|
|
initSessions();
|
|
|
|
setupSlog();
|
|
|
|
}, []);
|
2021-11-25 15:00:52 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-12-09 02:00:25 +03:00
|
|
|
<ThemeProvider theme={dark ? _dark : _light}>
|
2022-04-01 16:59:39 +03:00
|
|
|
<div className="header">
|
|
|
|
<Tabs />
|
|
|
|
<InfoButton />
|
|
|
|
</div>
|
2022-03-03 02:34:19 +03:00
|
|
|
<div className="buffer-container">
|
2022-03-10 01:25:10 +03:00
|
|
|
{names.map((name) => {
|
|
|
|
return <Buffer key={name} name={name} selected={name === selected} dark={dark} />;
|
2022-03-03 02:34:19 +03:00
|
|
|
})}
|
|
|
|
</div>
|
2021-12-09 02:00:25 +03:00
|
|
|
</ThemeProvider>
|
2021-11-25 15:00:52 +03:00
|
|
|
</>
|
|
|
|
);
|
2021-09-14 02:31:17 +03:00
|
|
|
}
|