Files
notesnook/apps/web/src/common/db.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-02-17 01:24:20 +05:00
import StorageInterface from "../interfaces/storage";
import EventSource from "eventsource";
2021-04-06 11:55:23 +05:00
import Config from "../utils/config";
import http from "notes-core/utils/http";
import { EV, EVENTS } from "notes-core/common";
import { getCurrentHash, hashNavigate } from "../navigation";
2021-02-17 01:24:20 +05:00
/**
* @type {import("notes-core/api").default}
*/
var db;
function initializeDatabase() {
2021-04-06 11:55:23 +05:00
return import("notes-core/api").then(async ({ default: Database }) => {
2021-02-17 01:24:20 +05:00
db = new Database(StorageInterface, EventSource);
// db.host({
// API_HOST: "https://api.notesnook.com",
// AUTH_HOST: "https://auth.streetwriters.co",
// SSE_HOST: "https://events.streetwriters.co",
// });
2021-07-31 15:27:16 +05:00
// db.host({
// API_HOST: "http://localhost:5264",
// AUTH_HOST: "http://localhost:8264",
// SSE_HOST: "http://localhost:7264",
// });
db.host({
2021-07-31 15:27:16 +05:00
API_HOST: "http://192.168.10.23:5264",
AUTH_HOST: "http://192.168.10.23:8264",
SSE_HOST: "http://192.168.10.23:7264",
});
2021-04-06 11:55:23 +05:00
await db.init();
if (!isAppHydrated()) {
2021-05-31 00:59:49 +05:00
if (process.env.REACT_APP_CI) return;
try {
loadDefaultNotes(db);
} catch (e) {}
2021-04-06 11:55:23 +05:00
}
2021-02-17 01:24:20 +05:00
});
}
export { db, initializeDatabase };
2021-04-06 11:55:23 +05:00
function isAppHydrated() {
return Config.get("hydrated", false);
}
2021-02-17 01:24:20 +05:00
2021-04-06 11:55:23 +05:00
function setAppHydrated() {
return Config.set("hydrated", true);
}
async function loadDefaultNotes(db) {
2021-07-03 12:42:37 +05:00
const notes = await http.get("/notes/index_v14.json");
2021-04-06 11:55:23 +05:00
if (!notes) return;
let autoOpenId;
const hash = getCurrentHash().replaceAll("#", "");
2021-04-06 11:55:23 +05:00
for (let note of notes) {
const content = await http.get(note.webContent);
let id = await db.notes.add({
title: note.title,
headline: note.headline,
localOnly: true,
content: { type: "tiny", data: content },
});
if (note.autoOpen) autoOpenId = id;
}
if (autoOpenId) {
hashNavigate(`/notes/${autoOpenId}/edit`);
if (hash) setTimeout(() => hashNavigate(hash), 100);
}
setAppHydrated();
EV.publish(EVENTS.appRefreshRequested);
2021-04-06 11:55:23 +05:00
}