This commit is contained in:
Rizky 2023-10-21 13:24:59 +07:00
parent 24159a6035
commit 812802a429
9 changed files with 73 additions and 51 deletions

View File

@ -23,4 +23,4 @@ export const SyncActionPaths = {
"5": "comp.doc", "5": "comp.doc",
"6": "page.list", "6": "page.list",
"7": "page.load" "7": "page.load"
}; };

View File

@ -11,6 +11,12 @@ export type UserConf = typeof defaultConf;
export const user = { export const user = {
conf: { conf: {
_db: null as null | RootDatabase<UserConf>, _db: null as null | RootDatabase<UserConf>,
init() {
this._db = open<UserConf, string>({
name: "user-conf",
path: dir.path(`${g.datadir}/lmdb/user-conf.lmdb`),
});
},
get db() { get db() {
if (!this._db) { if (!this._db) {
this._db = open<UserConf, string>({ this._db = open<UserConf, string>({

View File

@ -1,17 +1,20 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { page } from "web-utils"; import { page, useGlobal } from "web-utils";
import { Loading } from "../../utils/ui/loading"; import { Loading } from "../../utils/ui/loading";
import { bootEd } from "../../render/ed/ed";
import { EDGlobal } from "../../render/ed/logic/ed-global";
export default page({ export default page({
url: "**", url: "**",
component: ({}) => { component: ({}) => {
const p = useGlobal(EDGlobal, "EDITOR");
useEffect(() => { useEffect(() => {
if (localStorage.getItem("prasi-session")) { if (localStorage.getItem("prasi-session")) {
if ( if (
location.pathname === "/ed" || location.pathname === "/ed" ||
location.pathname.startsWith("/ed/") location.pathname.startsWith("/ed/")
) { ) {
navigate("/ed/_/_"); bootEd(p);
} else if (location.pathname.startsWith("/editor")) { } else if (location.pathname.startsWith("/editor")) {
const arr = location.pathname.split("/"); const arr = location.pathname.split("/");
if (arr.length <= 2) { if (arr.length <= 2) {

View File

@ -1,53 +1,17 @@
import { page, useGlobal } from "web-utils"; import { page, useGlobal } from "web-utils";
import { Ed, bootEd } from "../../render/ed/ed";
import { EDGlobal } from "../../render/ed/logic/ed-global"; import { EDGlobal } from "../../render/ed/logic/ed-global";
import { clientStartSync } from "../../utils/sync/client";
import { Loading } from "../../utils/ui/loading"; import { Loading } from "../../utils/ui/loading";
import { Ed } from "../../render/ed/ed";
export default page({ export default page({
url: "/ed/:site_id/:page_id", url: "/ed/:site_id/:page_id",
component: ({}) => { component: ({}) => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
const session = JSON.parse( if (!bootEd(p)) {
localStorage.getItem("prasi-session") || "null" return <Loading note="booting editor" />;
) as { data: { user: { id: string } } };
if (!session) {
navigate("/login");
return <Loading note="logging in" />;
} }
if (!p.sync) {
clientStartSync({
user_id: session.data.user.id,
events: {
editor_start(e) {
if (
!!params.site_id &&
!!params.page_id &&
params.site_id !== "_" &&
params.page_id !== "_"
) {
p.site.id = params.site_id;
p.page.id = params.page_id;
p.render();
} else {
if (e.site_id && e.page_id) {
navigate(`/ed/${e.site_id}/${e.page_id}`);
}
}
},
site_loaded({ site }) {
p.site = site;
p.render();
},
},
}).then((e) => (p.sync = e));
return <Loading note="editor start" />;
}
if (!p.site.id && p.page.id) return <Loading note="waiting page" />;
return <Ed />; return <Ed />;
}, },
}); });

View File

@ -1,13 +1,14 @@
import { useGlobal } from "web-utils"; import { useGlobal } from "web-utils";
import { Loading } from "../../utils/ui/loading"; import { Loading } from "../../utils/ui/loading";
import { EDGlobal } from "./logic/ed-global"; import { EDGlobal, PG } from "./logic/ed-global";
import { edRoute } from "./logic/ed-route"; import { edRoute } from "./logic/ed-route";
import { clientStartSync } from "../../utils/sync/client";
export const Ed = () => { export const Ed = () => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
edRoute(p); edRoute(p);
console.log(p.status);
if (p.status === "loading") { if (p.status === "loading") {
return <Loading />; return <Loading />;
} }
@ -20,3 +21,46 @@ export const Ed = () => {
} }
return <div>asfa</div>; return <div>asfa</div>;
}; };
export const bootEd = (p: PG) => {
const session = JSON.parse(
localStorage.getItem("prasi-session") || "null"
) as { data: { user: { id: string } } };
if (!session) {
navigate("/login");
return <Loading note="logging in" />;
}
const paramsOK =
!!params.site_id &&
!!params.page_id &&
params.site_id !== "_" &&
params.page_id !== "_";
if (!p.sync) {
clientStartSync({
user_id: session.data.user.id,
events: {
editor_start(e) {
if (!paramsOK) {
if (e.site_id && e.page_id) {
p.site.id = e.site_id;
p.page.id = e.page_id;
navigate(`/ed/${e.site_id}/${e.page_id}`);
}
} else {
p.site.id = params.site_id;
p.page.id = params.page_id;
p.render();
}
},
site_loaded({ site }) {
p.site = site;
p.render();
},
},
}).then((e) => (p.sync = e));
return false;
}
return true;
};

View File

@ -9,6 +9,7 @@ import { g } from "./utils/global";
import { createLogger } from "./utils/logger"; import { createLogger } from "./utils/logger";
import { preparePrisma } from "./utils/prisma"; import { preparePrisma } from "./utils/prisma";
import { syncActionDefinition } from "utils/sync-def"; import { syncActionDefinition } from "utils/sync-def";
import { user } from "../../app/srv/ws/sync/user";
g.status = "init"; g.status = "init";
@ -23,6 +24,7 @@ if (g.mode === "dev") {
await startDevWatcher(); await startDevWatcher();
} }
user.conf.init();
await preparePrisma(); await preparePrisma();
await ensureNotRunning(); await ensureNotRunning();
@ -33,10 +35,10 @@ if (g.db) {
} }
await syncActionDefinition(); await syncActionDefinition();
await parcelBuild();
await generateAPIFrm(); await generateAPIFrm();
await prepareApiRoutes(); await prepareApiRoutes();
await createServer(); await createServer();
await prepareAPITypes(); await prepareAPITypes();
await parcelBuild();
g.status = "ready"; g.status = "ready";

View File

@ -1,6 +1,6 @@
import { dir } from "dir"; import { dir } from "dir";
import { SyncActions } from "../../../app/srv/ws/sync/actions"; import { SyncActions } from "../../../app/srv/ws/sync/actions";
import { writeAsync } from "fs-jetpack"; import { readAsync, writeAsync } from "fs-jetpack";
export const syncActionDefinition = async () => { export const syncActionDefinition = async () => {
const def: any = {}; const def: any = {};
@ -22,10 +22,13 @@ export const syncActionDefinition = async () => {
}; };
walk(SyncActions, def, []); walk(SyncActions, def, []);
await writeAsync( const content = `\
dir.path("app/srv/ws/sync/actions-def.ts"),
`\
export const SyncActionDefinition = ${JSON.stringify(def, null, 2)}; export const SyncActionDefinition = ${JSON.stringify(def, null, 2)};
export const SyncActionPaths = ${JSON.stringify(paths, null, 2)}; ` export const SyncActionPaths = ${JSON.stringify(paths, null, 2)};
); `;
const path = dir.path("app/srv/ws/sync/actions-def.ts");
if ((await readAsync(path)) !== content) {
await writeAsync(path, content);
}
}; };

Binary file not shown.

Binary file not shown.