mirror of
https://codeberg.org/emersion/gamja.git
synced 2024-11-28 18:26:19 -05:00
Introduce store helper
Responsible for serializing/deserializing data to be saved in localStorage. Add a prefix to all localStorage entries to avoid conflicts with other webapps. Stop guarding against localStorage not existing, browsers can just implement a dumb interface to disable it.
This commit is contained in:
parent
12a38ace90
commit
695b02caaa
4 changed files with 62 additions and 33 deletions
|
@ -132,9 +132,6 @@ export default {
|
||||||
"quit": {
|
"quit": {
|
||||||
description: "Quit",
|
description: "Quit",
|
||||||
execute: (app, args) => {
|
execute: (app, args) => {
|
||||||
if (window.localStorage) {
|
|
||||||
localStorage.removeItem("autoconnect");
|
|
||||||
}
|
|
||||||
app.close({ name: SERVER_BUFFER });
|
app.close({ name: SERVER_BUFFER });
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -16,6 +16,7 @@ import { strip as stripANSI } from "../lib/ansi.js";
|
||||||
import { SERVER_BUFFER, BufferType, ReceiptType, NetworkStatus, Unread } from "../state.js";
|
import { SERVER_BUFFER, BufferType, ReceiptType, NetworkStatus, Unread } from "../state.js";
|
||||||
import commands from "../commands.js";
|
import commands from "../commands.js";
|
||||||
import { setup as setupKeybindings } from "../keybindings.js";
|
import { setup as setupKeybindings } from "../keybindings.js";
|
||||||
|
import * as store from "../store.js";
|
||||||
|
|
||||||
const configPromise = fetch("../config.json")
|
const configPromise = fetch("../config.json")
|
||||||
.then((resp) => {
|
.then((resp) => {
|
||||||
|
@ -193,8 +194,8 @@ export default class App extends Component {
|
||||||
|
|
||||||
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
|
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
|
||||||
|
|
||||||
if (window.localStorage && localStorage.getItem("autoconnect")) {
|
var connectParams = store.autoconnect.load();
|
||||||
var connectParams = JSON.parse(localStorage.getItem("autoconnect"));
|
if (connectParams) {
|
||||||
this.state.connectParams = {
|
this.state.connectParams = {
|
||||||
...this.state.connectParams,
|
...this.state.connectParams,
|
||||||
...connectParams,
|
...connectParams,
|
||||||
|
@ -202,10 +203,7 @@ export default class App extends Component {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (window.localStorage && localStorage.getItem("receipts")) {
|
this.receipts = store.receipts.load();
|
||||||
var obj = JSON.parse(localStorage.getItem("receipts"));
|
|
||||||
this.receipts = new Map(Object.entries(obj));
|
|
||||||
}
|
|
||||||
|
|
||||||
configPromise.then((config) => {
|
configPromise.then((config) => {
|
||||||
this.handleConfig(config);
|
this.handleConfig(config);
|
||||||
|
@ -376,10 +374,7 @@ export default class App extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
saveReceipts() {
|
saveReceipts() {
|
||||||
if (window.localStorage) {
|
store.receipts.put(this.receipts);
|
||||||
var obj = Object.fromEntries(this.receipts);
|
|
||||||
localStorage.setItem("receipts", JSON.stringify(obj));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getReceipt(target, type) {
|
getReceipt(target, type) {
|
||||||
|
@ -808,12 +803,10 @@ export default class App extends Component {
|
||||||
handleConnectSubmit(connectParams) {
|
handleConnectSubmit(connectParams) {
|
||||||
this.setState({ error: null });
|
this.setState({ error: null });
|
||||||
|
|
||||||
if (window.localStorage) {
|
if (connectParams.autoconnect) {
|
||||||
if (connectParams.autoconnect) {
|
store.autoconnect.put(connectParams);
|
||||||
localStorage.setItem("autoconnect", JSON.stringify(connectParams));
|
} else {
|
||||||
} else {
|
store.autoconnect.put(null);
|
||||||
localStorage.removeItem("autoconnect");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connect(connectParams);
|
this.connect(connectParams);
|
||||||
|
@ -887,8 +880,8 @@ export default class App extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: only clear local storage if this network is stored there
|
// TODO: only clear local storage if this network is stored there
|
||||||
if (buf.network == 1 && window.localStorage) {
|
if (buf.network == 1) {
|
||||||
localStorage.removeItem("autoconnect");
|
store.autoconnect.put(null);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BufferType.CHANNEL:
|
case BufferType.CHANNEL:
|
||||||
|
|
|
@ -74,17 +74,6 @@ export default class ConnectForm extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
var rememberMe = null;
|
|
||||||
if (window.localStorage) {
|
|
||||||
rememberMe = html`
|
|
||||||
<label>
|
|
||||||
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
|
|
||||||
Remember me
|
|
||||||
</label>
|
|
||||||
<br/><br/>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<form onChange=${this.handleChange} onSubmit=${this.handleSubmit}>
|
<form onChange=${this.handleChange} onSubmit=${this.handleSubmit}>
|
||||||
<h2>Connect to IRC</h2>
|
<h2>Connect to IRC</h2>
|
||||||
|
@ -101,7 +90,11 @@ export default class ConnectForm extends Component {
|
||||||
</label>
|
</label>
|
||||||
<br/><br/>
|
<br/><br/>
|
||||||
|
|
||||||
${rememberMe}
|
<label>
|
||||||
|
<input type="checkbox" name="rememberMe" checked=${this.state.rememberMe} disabled=${this.props.disabled}/>
|
||||||
|
Remember me
|
||||||
|
</label>
|
||||||
|
<br/><br/>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Advanced options</summary>
|
<summary>Advanced options</summary>
|
||||||
|
|
46
store.js
Normal file
46
store.js
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
const PREFIX = "gamja_";
|
||||||
|
|
||||||
|
function getItem(k) {
|
||||||
|
k = PREFIX + k;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function setItem(k, v) {
|
||||||
|
k = PREFIX + k;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Item {
|
||||||
|
constructor(k) {
|
||||||
|
this.k = PREFIX + k;
|
||||||
|
}
|
||||||
|
|
||||||
|
load() {
|
||||||
|
var v = localStorage.getItem(this.k);
|
||||||
|
if (!v) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return JSON.parse(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
put(v) {
|
||||||
|
if (v) {
|
||||||
|
localStorage.setItem(this.k, JSON.stringify(v));
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem(this.k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const autoconnect = new Item("autoconnect");
|
||||||
|
|
||||||
|
const rawReceipts = new Item("receipts");
|
||||||
|
|
||||||
|
export const receipts = {
|
||||||
|
load() {
|
||||||
|
var v = rawReceipts.load();
|
||||||
|
return new Map(Object.entries(v || {}));
|
||||||
|
},
|
||||||
|
put(m) {
|
||||||
|
rawReceipts.put(Object.fromEntries(m));
|
||||||
|
},
|
||||||
|
};
|
Loading…
Reference in a new issue