databag/app/mobile/src/context/useStoreContext.hook.js

131 lines
3.7 KiB
JavaScript
Raw Normal View History

import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'databag_v001.db';
export function useStoreContext() {
const [state, setState] = useState({
init: false,
session: null,
2022-09-13 18:46:28 +00:00
sessionId: 0,
profileRevision: null,
cardRevision: null,
channelRevision: null,
accountRevision: null,
});
const db = useRef(null);
2022-09-13 18:46:28 +00:00
const session = useRef(null);
const sessionId = useRef(0);
const updateState = (value) => {
setState((s) => ({ ...s, ...value }))
}
2022-09-13 18:46:28 +00:00
useEffect(() => {
initialize();
}, []);
2022-09-13 18:46:28 +00:00
useEffect(() => {
if (state.init && state.session && sessionId.current === state.sessionId) {
const revision = {
accountRevision: state.accountRevision,
profileRevision: state.profileRevision,
cardRevision: state.cardRevision,
channelRevision: state.channelRevision,
}
const revisionId = `${session.current.guid}_revision`;
db.current.executeSql(`UPDATE app SET value=? WHERE key='${revisionId}';`, [encodeObject(revision)]);
}
2022-09-13 18:46:28 +00:00
}, [state]);
const initialize = async () => {
SQLite.DEBUG(false);
SQLite.enablePromise(true);
db.current = await SQLite.openDatabase({ name: DATABAG_DB, location: "default" });
await db.current.executeSql("CREATE TABLE IF NOT EXISTS app (key text, value text, unique(key));");
await db.current.executeSql("INSERT OR IGNORE INTO app (key, value) values ('session', null);");
2022-09-13 18:46:28 +00:00
session.current = await getAppValue(db.current, 'session');
if (!session.current) {
updateState({ init: true });
}
else {
const revisionId = `${session.current.guid}_revision`;
const revision = await getAppValue(db.current, revisionId, {});
updateState({ init: true, session: session.current, ...revision });
}
};
const actions = {
setSession: async (access) => {
await db.current.executeSql("UPDATE app SET value=? WHERE key='session';", [encodeObject(access)]);
const revisionId = `${access.guid}_revision`;
2022-09-13 18:46:28 +00:00
const revision = await getAppValue(db.current, revisionId, {});
2022-09-13 18:46:28 +00:00
session.current = access;
sessionId.current++;
updateState({ session: access, sessionId: sessionId.current, ...revision });
},
clearSession: async () => {
await db.current.executeSql("UPDATE app set value=? WHERE key='session';", [null]);
2022-09-13 18:46:28 +00:00
session.current = null;
updateState({ session: null });
},
2022-09-13 18:46:28 +00:00
setProfileRevision: (id, profileRevision) => {
if (sessionId.current === id) {
updateState({ profileRevision });
}
},
setAccountRevision: (id, accountRevision) => {
if (sessionId.current === id) {
updateState({ accountRevision });
}
},
setCardRevision: (id, cardRevision) => {
if (sessionId.current === id) {
updateState({ cardRevision });
}
},
setChannelRevision: (channelRevision) => {
if (sessionId.current === id) {
updateState({ channelRevision });
}
},
}
return { state, actions }
}
function decodeObject(s: string) {
if(s == null) {
return null;
}
return JSON.parse(s);
}
function encodeObject(o: any) {
if(o == null) {
return null;
}
return JSON.stringify(o);
}
function hasResult(res) {
if(res === undefined || res[0] === undefined || res[0].rows === undefined || res[0].rows.length == 0) {
return false;
}
return true;
}
2022-09-13 18:46:28 +00:00
async function getAppValue(sql: SQLite.SQLiteDatabase, id: string, unset) {
const res = await sql.executeSql(`SELECT * FROM app WHERE key='${id}';`);
if (hasResult(res)) {
return decodeObject(res[0].rows.item(0).value);
}
2022-09-13 18:46:28 +00:00
return unset;
}