wip fix prasi

This commit is contained in:
Rizky 2024-02-27 20:07:59 +07:00
parent fcf953b478
commit a55c2e48e2
7 changed files with 151 additions and 107 deletions

File diff suppressed because one or more lines are too long

View File

@ -83,50 +83,65 @@ export const edInitSync = (p: PG) => {
if (!params.page_id) {
if (location.pathname.startsWith("/ed")) {
if (!params.site_id) {
_db.page
.findFirst({
(async () => {
const e = await _db.page.findFirst({
where: {
is_deleted: false,
is_default_layout: false,
site: params.site_id
? { id: params.site_id }
: {
id_user: p.user.id,
},
name: {
contains: "root",
mode: "insensitive",
},
},
select: { id: true, id_site: true },
orderBy: {
site: {
name: "asc",
},
},
});
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
else {
const e = await _db.page.findFirst({
where: {
is_deleted: false,
is_default_layout: false,
site: {
id_user: p.user.id,
site: params.site_id
? { id: params.site_id }
: {
id_user: p.user.id,
},
name: {
contains: "home",
mode: "insensitive",
},
},
select: { id: true, id_site: true },
})
.then((e) => {
});
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
else {
const e = await _db.page.findFirst({
where: {
is_deleted: false,
is_default_layout: false,
site: params.site_id
? { id: params.site_id }
: {
id_user: p.user.id,
},
},
select: { id: true, id_site: true },
});
if (e) location.href = `/ed/${e.id_site}/${e.id}`;
});
} else {
_db.page
.findFirst({
where: {
is_deleted: false,
is_default_layout: false,
id_site: params.site_id,
},
select: { id: true, id_site: true },
})
.then(async (e) => {
if (e) location.href = `/ed/${params.site_id}/${e.id}`;
else {
const res = await _db.page.create({
data: {
content_tree: {
childs: [],
id: "root",
type: "root",
},
name: "home",
url: "/",
site: { connect: { id: params.site_id } },
},
});
if (res) location.href = `/ed/${params.site_id}/${res.id}`;
}
});
}
}
}
})();
return false;
}
}

View File

@ -43,6 +43,7 @@ export const base = {
mode: "" as "desktop" | "mobile",
route: {
status: "init" as "init" | "loading" | "ready",
pages: [] as { id: string; url: string }[],
router: null as null | RadixRouter<{ id: string; url: string }>,
},
comp: {

View File

@ -3,17 +3,20 @@ import { IRoot } from "../../../utils/types/root";
import { prodCache } from "./cache";
import { get, set } from "idb-keyval";
export const loadPage = (page_id: string) => {
export const loadPage = (page_id: string, use_cache?: boolean) => {
return new Promise<{
id: string;
url: string;
root: IRoot;
}>(async (done) => {
let returned = false;
const cached = await get(`page-${page_id}`, prodCache);
if (cached) {
done(cached);
returned = true;
if (use_cache !== false) {
const cached = await get(`page-${page_id}`, prodCache);
if (cached) {
done(cached);
returned = true;
}
}
const res = (await (

View File

@ -34,6 +34,7 @@ const getRoute = () => {
export const initBaseRoute = async () => {
const router = createRouter<{ id: string; url: string }>();
const pages = [] as { id: string; url: string }[];
try {
const res = await getRoute();
@ -61,11 +62,12 @@ export const initBaseRoute = async () => {
for (const item of res.urls) {
router.insert(item.url, item);
pages.push(item);
}
}
} catch (e) {}
return router;
return { router, pages };
};
const injectSiteScript = () => {

View File

@ -1,6 +1,6 @@
import { createRoot } from "react-dom/client";
import { defineReact, defineWindow } from "web-utils";
import { Root } from "./root";
import { Root, isPreview } from "./root";
import { initBaseConfig } from "./base/base";
import { w } from "./w";
@ -14,15 +14,7 @@ import { w } from "./w";
w.navigateOverride = (_href: string) => {
if (_href && _href.startsWith("/")) {
if (
location.hostname.split(".").length === 4 ||
location.hostname === "prasi.app" ||
location.hostname === "prasi.avolut.com" ||
location.hostname.includes("ngrok") ||
location.hostname === "localhost" ||
location.hostname === "127.0.0.1" ||
location.hostname === "10.0.2.2" // android localhost
) {
if (isPreview()) {
if (
location.pathname.startsWith("/prod") &&
!_href.startsWith("/prod")

View File

@ -1,4 +1,5 @@
import { FC, useState } from "react";
import { validate } from "uuid";
import { GlobalContext, useLocal } from "web-utils";
import { DeadEnd } from "../../utils/ui/deadend";
import { Loading } from "../../utils/ui/loading";
@ -11,6 +12,18 @@ import { detectResponsiveMode } from "./base/responsive";
import { initBaseRoute, rebuildMeta } from "./base/route";
import { w } from "./w";
export const isPreview = () => {
return (
location.hostname.split(".").length === 4 ||
location.hostname === "prasi.app" ||
location.hostname === "prasi.avolut.com" ||
location.hostname.includes("ngrok") ||
location.hostname === "localhost" ||
location.hostname === "127.0.0.1" ||
location.hostname === "10.0.2.2"
); // android localhost
};
export const Root = () => {
// #region context
const local = useLocal({ page_id: "" });
@ -24,10 +37,11 @@ export const Root = () => {
if (base.route.status !== "ready") {
if (base.route.status === "init") {
base.route.status = "loading";
initBaseRoute().then(async (router) => {
initBaseRoute().then(async ({ router, pages }) => {
detectResponsiveMode();
base.route.status = "ready";
base.route.router = router;
base.route.pages = pages;
const site_script = evalCJS(
await (
@ -53,7 +67,22 @@ export const Root = () => {
const router = base.route.router;
if (!router) return <DeadEnd>Failed to create Router</DeadEnd>;
const page = router.lookup(base.pathname);
let page_id_from_url = "";
const isPreviewProd = isPreview() && location.pathname.startsWith("/prod");
if (isPreviewProd) {
const parts = location.pathname.split("/");
if (validate(parts[3])) {
page_id_from_url = parts[3];
}
}
let page = router.lookup(base.pathname);
if (page_id_from_url) {
const found = base.route.pages.find((e) => page_id_from_url === e.id);
if (found) {
page = found;
}
}
if (!page) return <DeadEnd>Page Not Found</DeadEnd>;
if (page.id !== local.page_id) {
@ -67,18 +96,20 @@ export const Root = () => {
const cache = base.page.cache[page.id];
if (!cache) {
loadPage(page.id)
loadPage(page.id, !isPreviewProd)
.then(async ({ root }) => {
const p = {
id: page.id,
url: page.url,
root,
meta: {},
};
await scanComponent(root.childs);
rebuildMeta(p.meta, root);
base.page.cache[p.id] = p;
render();
if (page) {
const p = {
id: page.id,
url: page.url,
root,
meta: {},
};
await scanComponent(root.childs);
rebuildMeta(p.meta, root);
base.page.cache[p.id] = p;
render();
}
})
.catch(() => {
render();