From da2cb750016c283d368c8c3890e521ba16a48fb3 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 20 Oct 2023 19:18:21 +0700 Subject: [PATCH] fix ed --- app/srv/ws/sync/actions.ts | 19 ++++++++---- app/srv/ws/sync/actions/index.ts | 1 + app/srv/ws/sync/actions/site_group.ts | 6 ++++ app/srv/ws/sync/actions/site_load.ts | 24 +++++++++++++-- app/srv/ws/sync/editor/load.ts | 4 +-- app/srv/ws/sync/sync-handler.ts | 29 +++++++++++++++--- app/srv/ws/sync/type.ts | 7 ++++- app/srv/ws/sync/user.ts | 8 ++--- app/web/src/base/page/ed.tsx | 2 +- app/web/src/index.tsx | 19 ++++++++++-- app/web/src/render/ed/ed.tsx | 18 ++++++++--- .../ed/logic/{global.ts => ed-global.ts} | 6 ++++ app/web/src/render/ed/logic/ed-route.ts | 21 +++++++++++++ app/web/src/render/ed/logic/route.ts | 8 ----- app/web/src/utils/sync/client.ts | 2 +- package.json | 5 +-- pkgs/core/index.ts | 2 +- pkgs/core/utils/parcel.ts | 1 - undefined/lmdb/user-conf.lmdb | Bin 180224 -> 180224 bytes undefined/lmdb/user-conf.lmdb-lock | Bin 8200 -> 8200 bytes 20 files changed, 142 insertions(+), 40 deletions(-) create mode 100644 app/srv/ws/sync/actions/site_group.ts rename app/web/src/render/ed/logic/{global.ts => ed-global.ts} (80%) create mode 100644 app/web/src/render/ed/logic/ed-route.ts delete mode 100644 app/web/src/render/ed/logic/route.ts diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index 6fca0243..234e830e 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -1,14 +1,21 @@ import { component, site, page } from "dbgen"; +import { ESite } from "../../../web/src/render/ed/logic/ed-global"; +/* + WARNING: + CHANGING FUNCTION NAME / OBJECT STRUCTURE + WILL *BREAK* OFFLINE STORAGE -- + ONLY ADDITION IS ALLOWED +*/ + +export type SAction = typeof SyncActions; export const SyncActions = { site: { - list: () => - ({}) as Promise< - Record - >, - group: () => ({}) as Promise>, - load: (id: string) => ({}) as Promise, + list: async () => + ({}) as Record, + group: async () => ({}) as Record, + load: async (id: string) => ({}) as ESite | undefined, }, comp: { list: () => ({}) as Record>, diff --git a/app/srv/ws/sync/actions/index.ts b/app/srv/ws/sync/actions/index.ts index fde8b50f..cf2dd000 100644 --- a/app/srv/ws/sync/actions/index.ts +++ b/app/srv/ws/sync/actions/index.ts @@ -1 +1,2 @@ export * from "./site_load"; +export * from "./site_group"; diff --git a/app/srv/ws/sync/actions/site_group.ts b/app/srv/ws/sync/actions/site_group.ts new file mode 100644 index 00000000..07bbd230 --- /dev/null +++ b/app/srv/ws/sync/actions/site_group.ts @@ -0,0 +1,6 @@ +import { ActionCtx } from "../type"; + +export const site_group = async function (this: ActionCtx) { + console.log(this.user); + return "gruop"; +}; diff --git a/app/srv/ws/sync/actions/site_load.ts b/app/srv/ws/sync/actions/site_load.ts index 368a98d3..c97ac1f5 100644 --- a/app/srv/ws/sync/actions/site_load.ts +++ b/app/srv/ws/sync/actions/site_load.ts @@ -1,3 +1,23 @@ -export const site_load = async () => { - return "moka"; +import { validate } from "uuid"; +import { ActionCtx } from "../type"; +import { ESite } from "../../../../web/src/render/ed/logic/ed-global"; +import { SAction } from "../actions"; + +export const site_load: SAction["site"]["load"] = async function ( + this: ActionCtx, + id: string +) { + if (validate(id)) { + const site = await db.site.findFirst({ where: { id } }); + if (site) { + return { + id: site.id, + config: site.config as ESite["config"], + domain: site.domain, + js: site.js || "", + js_compiled: site.js_compiled || "", + name: site.name, + }; + } + } }; diff --git a/app/srv/ws/sync/editor/load.ts b/app/srv/ws/sync/editor/load.ts index c5105b00..00f7c057 100644 --- a/app/srv/ws/sync/editor/load.ts +++ b/app/srv/ws/sync/editor/load.ts @@ -1,7 +1,7 @@ import { user } from "../user"; -export const loadUserConf = async (user_id: string) => { - const conf = user.conf.getOrCreate(user_id); +export const loadDefaultSite = async (user_id: string) => { + const conf = user.conf.get(user_id); if (conf) { if (!conf.site_id) { const site = await db.site.findFirst({ diff --git a/app/srv/ws/sync/sync-handler.ts b/app/srv/ws/sync/sync-handler.ts index cbb0bca0..18c338ba 100644 --- a/app/srv/ws/sync/sync-handler.ts +++ b/app/srv/ws/sync/sync-handler.ts @@ -3,16 +3,18 @@ import { ServerWebSocket, WebSocketHandler } from "bun"; import { Packr } from "msgpackr"; import { WSData } from "../../../../pkgs/core/server/create"; import { ClientEvent } from "../../../web/src/utils/sync/client"; -import { loadUserConf } from "./editor/load"; -import { SyncType } from "./type"; +import { loadDefaultSite } from "./editor/load"; +import { ActionCtx, SyncType } from "./type"; import { SyncActionPaths } from "./actions-def"; import * as actions from "./actions/index"; +import { UserConf, user } from "./user"; const packr = new Packr({ structuredClone: true }); const conns = new Map< string, { user_id: string; + conf?: UserConf & { toJSON: () => UserConf }; ws: ServerWebSocket; msg: { pending: Record>; @@ -51,18 +53,35 @@ export const syncHandler: WebSocketHandler = { if (msg.type === SyncType.UserID) { const { user_id } = msg; conn.user_id = user_id; - const conf = await loadUserConf(user_id); + + const conf = user.conf.getOrCreate(user_id); + if (!conf.site_id) { + await loadDefaultSite(user_id); + } + conn.conf = new Proxy(conf, { + get(_, p) { + const conf = user.conf.get(user_id); + if (p === "toJSON") return () => conf; + if (conf) return conf[p as keyof typeof conf]; + }, + set(_, p, newValue) { + user.conf.set(user_id, p as keyof UserConf, newValue); + return true; + }, + }) as UserConf & { toJSON: () => UserConf }; send(ws, { type: SyncType.Event, event: "editor_start" as ClientEvent, - data: conf, + data: conn.conf.toJSON(), }); } if (msg.type === SyncType.Action) { const code = msg.code as keyof typeof SyncActionPaths; const actionName = SyncActionPaths[code].replace(/\./gi, "_"); if (actionName) { - const action = (actions as any)[actionName]; + const action = (actions as any)[actionName].bind({ + user: { id: conn.user_id, conf: conn.conf }, + } as ActionCtx); ws.sendBinary( packr.pack({ diff --git a/app/srv/ws/sync/type.ts b/app/srv/ws/sync/type.ts index 75cd976d..4360da65 100644 --- a/app/srv/ws/sync/type.ts +++ b/app/srv/ws/sync/type.ts @@ -1,7 +1,12 @@ +import { UserConf } from "./user"; + export enum SyncType { ClientID, UserID, Event, Action, ActionResult, -} \ No newline at end of file +} +export type ActionCtx = { + user: { id: string; conf: UserConf & { toJSON: () => UserConf } }; +}; diff --git a/app/srv/ws/sync/user.ts b/app/srv/ws/sync/user.ts index b50aa450..4472f7dc 100644 --- a/app/srv/ws/sync/user.ts +++ b/app/srv/ws/sync/user.ts @@ -6,7 +6,7 @@ const defaultConf = { site_id: "", page_id: "", }; -type UserConf = typeof defaultConf; +export type UserConf = typeof defaultConf; const db = open({ name: "user-conf", @@ -16,12 +16,12 @@ const db = open({ export const user = { conf: { getOrCreate(user_id: string) { - const res = db.get(user_id); + let res = db.get(user_id); if (!res) { db.put(user_id, structuredClone(defaultConf)); - return db.get(user_id); + res = db.get(user_id); } - return res; + return res as UserConf; }, get(user_id: string) { return db.get(user_id); diff --git a/app/web/src/base/page/ed.tsx b/app/web/src/base/page/ed.tsx index f6436031..0f4a35e4 100644 --- a/app/web/src/base/page/ed.tsx +++ b/app/web/src/base/page/ed.tsx @@ -1,5 +1,5 @@ import { page, useGlobal } from "web-utils"; -import { EDGlobal } from "../../render/ed/logic/global"; +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"; diff --git a/app/web/src/index.tsx b/app/web/src/index.tsx index a816efcd..15223d5d 100644 --- a/app/web/src/index.tsx +++ b/app/web/src/index.tsx @@ -10,9 +10,23 @@ const start = async () => { let react = { root: null as null | ReactRoot, }; - if (!["localhost", "127.0.0.1"].includes(location.hostname)) { + if (true || !["localhost", "127.0.0.1"].includes(location.hostname)) { const sw = await registerServiceWorker(); + + const cacheCurrentPage = () => { + const swc = navigator.serviceWorker.controller; + if (swc) { + [location.href, "", "/", "/ed", "/ed/_/_", "/login"].forEach((url) => { + swc.postMessage({ + type: "add-cache", + url: url, + }); + }); + } + }; + cacheCurrentPage(); navigator.serviceWorker.addEventListener("message", (e) => { + cacheCurrentPage(); if (react.root) { if (e.data.type === "offline") { w.offline = true; @@ -100,7 +114,8 @@ const start = async () => { className="bg-green-600 text-white px-4 py-2 rounded-full text-sm" onClick={click} > - App Updated, Ready to use offline + App Updated{" "} + {e.data.version} diff --git a/app/web/src/render/ed/ed.tsx b/app/web/src/render/ed/ed.tsx index 0576a00b..b66e4c27 100644 --- a/app/web/src/render/ed/ed.tsx +++ b/app/web/src/render/ed/ed.tsx @@ -1,12 +1,22 @@ import { useGlobal } from "web-utils"; import { Loading } from "../../utils/ui/loading"; -import { EDGlobal } from "./logic/global"; -import { edRoute } from "./logic/route"; +import { EDGlobal } from "./logic/ed-global"; +import { edRoute } from "./logic/ed-route"; export const Ed = () => { const p = useGlobal(EDGlobal, "EDITOR"); edRoute(p); - - return ; + console.log(p.status); + if (p.status === "loading") { + return ; + } + if (p.status === "site-not-found" || p.status === "page-not-found") { + return ( +
+ {p.status === "site-not-found" ? "Site not found" : "Page not found"} +
+ ); + } + return
asfa
; }; diff --git a/app/web/src/render/ed/logic/global.ts b/app/web/src/render/ed/logic/ed-global.ts similarity index 80% rename from app/web/src/render/ed/logic/global.ts rename to app/web/src/render/ed/logic/ed-global.ts index dfe0c341..10d983ff 100644 --- a/app/web/src/render/ed/logic/global.ts +++ b/app/web/src/render/ed/logic/ed-global.ts @@ -14,6 +14,12 @@ const EmptyPage = { }; export const EDGlobal = { + status: "init" as + | "init" + | "loading" + | "site-not-found" + | "page-not-found" + | "ready", sync: null as unknown as Awaited>, site: EmptySite, page: EmptyPage, diff --git a/app/web/src/render/ed/logic/ed-route.ts b/app/web/src/render/ed/logic/ed-route.ts new file mode 100644 index 00000000..cdcf6312 --- /dev/null +++ b/app/web/src/render/ed/logic/ed-route.ts @@ -0,0 +1,21 @@ +import { PG } from "./ed-global"; + +export const edRoute = async (p: PG) => { + if (p.status === "init") { + if (!p.site.domain && !p.site.name) { + p.status = "loading"; + const site = await p.sync.site.load(p.site.id); + if (!site) { + p.status = "site-not-found"; + p.render(); + return; + } + + p.site = site; + } + + if (p.site) { + console.log(p.site); + } + } +}; diff --git a/app/web/src/render/ed/logic/route.ts b/app/web/src/render/ed/logic/route.ts deleted file mode 100644 index d8af2a14..00000000 --- a/app/web/src/render/ed/logic/route.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { PG } from "./global"; - -export const edRoute = async (p: PG) => { - if (!p.site.domain && !p.site.name) { - const res = await p.sync.site.load(p.site.id); - console.log(res); - } -}; diff --git a/app/web/src/utils/sync/client.ts b/app/web/src/utils/sync/client.ts index c22715c6..bd701a72 100644 --- a/app/web/src/utils/sync/client.ts +++ b/app/web/src/utils/sync/client.ts @@ -8,7 +8,7 @@ import { SyncActionDefinition } from "../../../../srv/ws/sync/actions-def"; import { initIDB } from "./idb"; import { SyncType } from "../../../../srv/ws/sync/type"; import { w } from "../types/general"; -import { ESite } from "../../render/ed/logic/global"; +import { ESite } from "../../render/ed/logic/ed-global"; const packr = new Packr({ structuredClone: true }); const conf = { ws: null as null | WebSocket, diff --git a/package.json b/package.json index f9da7524..e8090210 100644 --- a/package.json +++ b/package.json @@ -3,16 +3,17 @@ "module": "src/index.ts", "type": "module", "scripts": { - "dev": "bun clean && bun run --silent --watch ./pkgs/core/index.ts dev", + "dev": "bun run --silent --watch ./pkgs/core/index.ts dev", "clean": "rm -rf app/static && rm -rf app/web/.parcel-cache", "build": "bun run --silent ./pkgs/core/build.ts", "db-pull": "bun run ./pkgs/core/db-pull.ts", + "parcel": "bun clean && bun run ./pkgs/core/parcel.ts", "prod": "bun run --silent ./pkgs/core/index.ts", "local-prod": "bun run build && bun run db-pull && bun run ./pkgs/core/index.ts", "pull": "cd app/db && bun prisma db pull && bun prisma generate", "pkgs-upgrade": "bun run --silent ./pkgs/core/upgrade.ts" }, - "workspaces": [ + "workspaces": [ "app/*", "pkgs/*" ], diff --git a/pkgs/core/index.ts b/pkgs/core/index.ts index 5af17d15..08c41a02 100644 --- a/pkgs/core/index.ts +++ b/pkgs/core/index.ts @@ -31,7 +31,7 @@ if (g.db) { g.log.error(`[DB ERROR]\n${e.message}`); }); } - + await syncActionDefinition(); await parcelBuild(); await generateAPIFrm(); diff --git a/pkgs/core/utils/parcel.ts b/pkgs/core/utils/parcel.ts index 1d23479a..1ccf719e 100644 --- a/pkgs/core/utils/parcel.ts +++ b/pkgs/core/utils/parcel.ts @@ -3,7 +3,6 @@ import { dirAsync } from "fs-jetpack"; import { dir } from "./dir"; import { g } from "./global"; - const decoder = new TextDecoder(); export const parcelBuild = async () => { await dirAsync("app/static"); diff --git a/undefined/lmdb/user-conf.lmdb b/undefined/lmdb/user-conf.lmdb index 69dfee7a3c8f6aebb896982fe70e91846b98625d..1c020573ef56ef5653e7e7704dd8ab68cc859c4f 100644 GIT binary patch delta 313 zcmZo@;BIK(W@MPm=+Mu}#Q*^k8)yEPXO!GHGhKd?L&U@h91{~PK-`J)8xLg3Pdtz_ zF@S?nax-Vce|tu8Aobo}kOit0MvHIfbYS{!&&diE2l5-3=Gz;JL;1{58fGOYl+OgE z6~HtHgV^+y`pgX5uuxAobQ~=9yFyM3tP%V=Fp delta 320 zcmZWkJ8l9o5cRW7w1x(nq+=x%C8DCGn>_+0H;9zv0;%v7Or*?im2D|4K$L9BJvacO zO1g~~OoYUYG*54SZ^qh=wH=60WDM<#I&I=hI?twVIyQ+ooAQNAef65U>fKEe=%ukW zK?Yb8?zyOuwM!kloAiMOD$>>^9Z?7wd`CO-{v&SP54)#o5-Mjy#0$TRT%DHl?-pIC zjj8ZPu6pD=T)->BQ@(~Rxj))#NW%aC diff --git a/undefined/lmdb/user-conf.lmdb-lock b/undefined/lmdb/user-conf.lmdb-lock index 55179533715c3f571bc28a9bcb041a7bf02eb937..cfb609eb218fc2e7a6ef9dc9716d14f497983bc1 100644 GIT binary patch delta 60 zcmeBh=x`9fci{a#rUjiEk_-^Q2&9-9j3+KE6#WkcP#Fe>$?DG;H!e(1W0CXD^ Ag#Z8m delta 60 zcmeBh=x`9fci{a#rUjiEEDR992&5Pp>?STO6#WkcvT0Dc$?DG;H!e(1W0DC(W A`~Uy|