add route guard for pages that require database connection

This commit is contained in:
liamcottle
2025-02-14 00:34:15 +13:00
parent f0a96b04ee
commit 2a7ed6203f
3 changed files with 22 additions and 20 deletions

View File

@@ -4,23 +4,22 @@ import {getRxStorageDexie} from 'rxdb/plugins/storage-dexie';
import GlobalState from "./GlobalState.js";
import Utils from "./Utils.js";
var database = null;
async function initDatabase(publicKeyHex) {
// close any exsiting database connection
if(database){
await database.close();
if(GlobalState.database){
await GlobalState.database.close();
}
// create a database with a unique name per identity
database = await createRxDatabase({
GlobalState.database = await createRxDatabase({
name: `meshcore_companion_db_${publicKeyHex}`,
storage: getRxStorageDexie(),
allowSlowCount: true,
});
// add database schemas
await database.addCollections({
await GlobalState.database.addCollections({
messages: {
schema: {
version: 0,
@@ -125,7 +124,7 @@ class Message {
// insert a message into the database
static async insert(data) {
return await database.messages.insert({
return await GlobalState.database.messages.insert({
id: v4(),
status: data.status,
to: Utils.bytesToHex(data.to),
@@ -142,7 +141,7 @@ class Message {
}
static async getMessageById(id) {
return await database.messages.findOne({
return await GlobalState.database.messages.findOne({
selector: {
id: {
$eq: id,
@@ -152,7 +151,7 @@ class Message {
}
static async deleteMessageById(id) {
return await database.messages.findOne({
return await GlobalState.database.messages.findOne({
selector: {
id: {
$eq: id,
@@ -166,7 +165,7 @@ class Message {
// find one latest message by ack code
// this will prevent updating older messages that might have the same ack code
const message = database.messages.findOne({
const message = GlobalState.database.messages.findOne({
selector: {
expected_ack_crc: {
$eq: ackCode,
@@ -213,12 +212,12 @@ class Message {
// get all messages
static getAllMessages() {
return database.messages.find();
return GlobalState.database.messages.find();
}
// get direct messages for the provided public key
static getContactMessages(publicKey) {
return database.messages.find({
return GlobalState.database.messages.find({
selector: {
$or: [
// messages from us to other contact
@@ -251,7 +250,7 @@ class Message {
// get unread direct messages count for the provided public key
static getContactMessagesUnreadCount(publicKey, messagesLastReadTimestamp) {
return database.messages.count({
return GlobalState.database.messages.count({
selector: {
timestamp: {
$gt: messagesLastReadTimestamp,
@@ -277,7 +276,7 @@ class ContactMessagesReadState {
// update the read state of messages for the provided public key
static async touch(publicKey) {
return await database.contact_messages_read_state.upsert({
return await GlobalState.database.contact_messages_read_state.upsert({
id: Utils.bytesToHex(publicKey),
timestamp: Date.now(),
});
@@ -285,7 +284,7 @@ class ContactMessagesReadState {
// get the read state of messages for the provided public key
static get(publicKey) {
return database.contact_messages_read_state.findOne({
return GlobalState.database.contact_messages_read_state.findOne({
selector: {
id: {
$eq: Utils.bytesToHex(publicKey),
@@ -300,7 +299,7 @@ class ChannelMessage {
// insert a channel message into the database
static async insert(data) {
return await database.channel_messages.insert({
return await GlobalState.database.channel_messages.insert({
id: v4(),
channel_idx: data.channel_idx,
from: data.from != null ? Utils.bytesToHex(data.from) : null,
@@ -314,7 +313,7 @@ class ChannelMessage {
// get channel messages for the provided channel idx
static getChannelMessages(channelIdx) {
return database.channel_messages.find({
return GlobalState.database.channel_messages.find({
selector: {
channel_idx: {
$eq: channelIdx,

View File

@@ -3,6 +3,7 @@ import {reactive} from "vue";
// global state
const globalState = reactive({
connection: null,
database: null,
selfInfo: null,
contacts: [],
channels: [

View File

@@ -6,9 +6,9 @@ import "./style.css";
import App from './components/App.vue';
import GlobalState from "./js/GlobalState.js";
// helper function that force redirects to the main page if there is no device connection
function handleRouteThatRequiresDeviceConnection() {
if(!GlobalState.connection){
// helper function that force redirects to the main page if there is no database connection
function handleRouteThatRequiresDatabase() {
if(!GlobalState.database){
return {
name: 'main',
};
@@ -31,18 +31,20 @@ const routes = [
path: '/contacts/:publicKey/messages',
props: true,
component: () => import("./components/pages/ContactMessagesPage.vue"),
beforeEnter: handleRouteThatRequiresDatabase,
},
{
name: "channel.messages",
path: '/channels/:channelIdx/messages',
props: true,
component: () => import("./components/pages/ChannelMessagesPage.vue"),
beforeEnter: handleRouteThatRequiresDatabase,
},
{
name: "settings.radio",
path: '/settings/radio',
component: () => import("./components/pages/RadioSettingsPage.vue"),
beforeEnter: handleRouteThatRequiresDeviceConnection,
beforeEnter: handleRouteThatRequiresDatabase,
},
];