Skip to content

Commit

Permalink
feat: remember ws url in hub (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilzona authored and miralemd committed Jan 27, 2020
1 parent a813d70 commit 01ce46c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
28 changes: 2 additions & 26 deletions commands/serve/web/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import AppContext from '../contexts/AppContext';
import NebulaContext from '../contexts/NebulaContext';
import VizContext from '../contexts/VizContext';

import storageFn from '../storage';

const rtlShape = {
size: 'large',
viewBox: '0 0 20 20',
Expand Down Expand Up @@ -64,32 +66,6 @@ const languages = [
'ru-RU',
];

const storageFn = app => {
const stored = window.localStorage.getItem('nebula-dev');
const parsed = stored ? JSON.parse(stored) : {};
const appid = app.id;

const s = {
save(name, value) {
parsed[name] = value;
window.localStorage.setItem('nebula-dev', JSON.stringify(parsed));
},
get(name) {
return parsed[name];
},
props(name, v) {
if (v) {
s.save(`props:${appid}:${name}`, JSON.stringify(v));
return undefined;
}
const p = s.get(`props:${appid}:${name}`);
return p ? JSON.parse(p) : {};
},
};

return s;
};

export default function App({ app, info }) {
const storage = useMemo(() => storageFn(app), [app]);
const [activeViz, setActiveViz] = useState(null);
Expand Down
22 changes: 14 additions & 8 deletions commands/serve/web/components/Hub.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ import Typography from '@material-ui/core/Typography';
import CircularProgress from '@material-ui/core/CircularProgress';

import { info as connectionInfo, connect } from '../connect';
import storageFn from '../storage';

const theme = createTheme('light');
const storage = storageFn({});

function URLInput({ info }) {
const [url, setUrl] = useState(storage.get('nebula-ws-url') || 'ws://localhost:9076');
let typedUrl;
const onKeyDown = e => {
switch (e.key) {
case 'Enter':
window.location.href = `${window.location.origin}?engine_url=${e.target.value}`;
typedUrl = e.target.value;
if (typedUrl === '') {
typedUrl = url;
} else {
storage.save('nebula-ws-url', typedUrl);
setUrl(typedUrl);
}
window.location.href = `${window.location.origin}?engine_url=${typedUrl}`;
break;
case 'Escape':
break;
Expand All @@ -30,14 +41,9 @@ function URLInput({ info }) {
return (
<>
<Box style={{ height: '30vh' }} />
<Typography variant="h5">Engine WebSocket URL</Typography>
<Box boxShadow={24}>
<OutlinedInput
autoFocus
fullWidth
placeholder="Engine WebSocket URL"
onKeyDown={onKeyDown}
defaultValue={info.engineUrl}
/>
<OutlinedInput autoFocus fullWidth placeholder={url} onKeyDown={onKeyDown} defaultValue={info.engineUrl} />
</Box>
</>
);
Expand Down
27 changes: 27 additions & 0 deletions commands/serve/web/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const storageFn = app => {
const stored = window.localStorage.getItem('nebula-dev');
const parsed = stored ? JSON.parse(stored) : {};
const appid = app.id;

const s = {
save(name, value) {
parsed[name] = value;
window.localStorage.setItem('nebula-dev', JSON.stringify(parsed));
},
get(name) {
return parsed[name];
},
props(name, v) {
if (v) {
s.save(`props:${appid}:${name}`, JSON.stringify(v));
return undefined;
}
const p = s.get(`props:${appid}:${name}`);
return p ? JSON.parse(p) : {};
},
};

return s;
};

export default storageFn;

0 comments on commit 01ce46c

Please sign in to comment.