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

70 lines
1.7 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";
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",
// });
db.host({
API_HOST: "http://localhost:5264",
AUTH_HOST: "http://localhost:8264",
SSE_HOST: "http://localhost:7264",
});
2021-04-06 11:55:23 +05:00
await db.init();
if (!isAppHydrated()) {
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) {
const notes = await http.get("/notes/index.json");
if (!notes) return;
let autoOpenId;
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;
}
EV.publish(EVENTS.appRefreshRequested);
2021-04-06 11:55:23 +05:00
if (autoOpenId) quickNavigate(`/notes/${autoOpenId}/edit`);
setAppHydrated();
2021-04-06 11:55:23 +05:00
}
function quickNavigate(url) {
window.history.replaceState(null, null, `#${url}`);
dispatchEvent(new HashChangeEvent("hashchange"));
}