forked from mystiq/hydrogen-web
add export/import of session data
This commit is contained in:
parent
e2b9e9dfdc
commit
6f73d3983f
2 changed files with 13 additions and 2 deletions
|
@ -32,6 +32,7 @@ class SessionItemViewModel extends EventEmitter {
|
|||
|
||||
async clear() {
|
||||
this._isClearing = true;
|
||||
alert(JSON.stringify(this._sessionInfo, undefined, 2));
|
||||
this.emit("change", "isClearing");
|
||||
try {
|
||||
await this._pickerVM.clear(this.id);
|
||||
|
@ -71,7 +72,7 @@ export default class SessionPickerViewModel {
|
|||
this._storageFactory = storageFactory;
|
||||
this._sessionStore = sessionStore;
|
||||
this._sessionCallback = sessionCallback;
|
||||
this._sessions = new SortedArray((s1, s2) => (s1.sessionInfo.lastUsed || 0) - (s2.sessionInfo.lastUsed || 0));
|
||||
this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id));
|
||||
}
|
||||
|
||||
async load() {
|
||||
|
@ -86,6 +87,15 @@ export default class SessionPickerViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
async import(json) {
|
||||
const sessionInfo = JSON.parse(json);
|
||||
const sessionId = (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString();
|
||||
sessionInfo.id = sessionId;
|
||||
sessionInfo.lastUsed = sessionId;
|
||||
await this._sessionStore.add(sessionInfo);
|
||||
this._sessions.set(new SessionItemViewModel(sessionInfo, this));
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
const idx = this._sessions.array.findIndex(s => s.id === id);
|
||||
await this._sessionStore.delete(id);
|
||||
|
|
|
@ -54,7 +54,8 @@ export default class SessionPickerView extends TemplateView {
|
|||
return t.div({className: "SessionPickerView"}, [
|
||||
t.h1(["Pick a session"]),
|
||||
this._sessionList.mount(),
|
||||
t.button({onClick: () => this.viewModel.cancel()}, ["Log in to a new session instead"])
|
||||
t.p(t.button({onClick: () => this.viewModel.cancel()}, ["Log in to a new session instead"])),
|
||||
t.p(t.button({onClick: () => this.viewModel.import(prompt("JSON"))}, ["Import Session JSON"]))
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue