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

@ -11,6 +11,12 @@ export type UserConf = typeof defaultConf;
export const user = {
conf: {
_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() {
if (!this._db) {
this._db = open<UserConf, string>({

View File

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

View File

@ -1,53 +1,17 @@
import { page, useGlobal } from "web-utils";
import { Ed, bootEd } from "../../render/ed/ed";
import { EDGlobal } from "../../render/ed/logic/ed-global";
import { clientStartSync } from "../../utils/sync/client";
import { Loading } from "../../utils/ui/loading";
import { Ed } from "../../render/ed/ed";
export default page({
url: "/ed/:site_id/:page_id",
component: ({}) => {
const p = useGlobal(EDGlobal, "EDITOR");
const session = JSON.parse(
localStorage.getItem("prasi-session") || "null"
) as { data: { user: { id: string } } };
if (!session) {
navigate("/login");
return <Loading note="logging in" />;
if (!bootEd(p)) {
return <Loading note="booting editor" />;
}
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 />;
},
});

View File

@ -1,13 +1,14 @@
import { useGlobal } from "web-utils";
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 { clientStartSync } from "../../utils/sync/client";
export const Ed = () => {
const p = useGlobal(EDGlobal, "EDITOR");
edRoute(p);
console.log(p.status);
if (p.status === "loading") {
return <Loading />;
}
@ -20,3 +21,46 @@ export const Ed = () => {
}
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 { preparePrisma } from "./utils/prisma";
import { syncActionDefinition } from "utils/sync-def";
import { user } from "../../app/srv/ws/sync/user";
g.status = "init";
@ -23,6 +24,7 @@ if (g.mode === "dev") {
await startDevWatcher();
}
user.conf.init();
await preparePrisma();
await ensureNotRunning();
@ -33,10 +35,10 @@ if (g.db) {
}
await syncActionDefinition();
await parcelBuild();
await generateAPIFrm();
await prepareApiRoutes();
await createServer();
await prepareAPITypes();
await parcelBuild();
g.status = "ready";

View File

@ -1,6 +1,6 @@
import { dir } from "dir";
import { SyncActions } from "../../../app/srv/ws/sync/actions";
import { writeAsync } from "fs-jetpack";
import { readAsync, writeAsync } from "fs-jetpack";
export const syncActionDefinition = async () => {
const def: any = {};
@ -22,10 +22,13 @@ export const syncActionDefinition = async () => {
};
walk(SyncActions, def, []);
await writeAsync(
dir.path("app/srv/ws/sync/actions-def.ts"),
`\
const content = `\
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.