This commit is contained in:
Rizky 2024-02-08 07:29:16 +07:00
parent 9e1b693779
commit dc44d54924
3 changed files with 102 additions and 68 deletions

View File

@ -2,6 +2,7 @@ import { createRoot } from "react-dom/client";
import { defineReact, defineWindow } from "web-utils";
import { Root } from "./root";
import { initBaseConfig } from "./base/base";
import { w } from "./w";
(async () => {
initBaseConfig();
@ -10,6 +11,30 @@ import { initBaseConfig } from "./base/base";
const root = createRoot(div);
await defineWindow(false);
defineReact();
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 (
location.pathname.startsWith("/deploy") &&
!_href.startsWith("/deploy")
) {
const patharr = location.pathname.split("/");
_href = `/deploy/${patharr[2]}${_href}`;
}
}
}
return _href;
};
root.render(<Root />);
if (document.body.classList.contains("opacity-0")) {
document.body.classList.remove("opacity-0");

View File

@ -1,18 +1,23 @@
import { useLocal } from "web-utils";
import { FC, useState } from "react";
import { DeadEnd } from "../../utils/ui/deadend";
import { Loading } from "../../utils/ui/loading";
import { evalCJS } from "../ed/logic/ed-sync";
import { Vi } from "../vi/vi";
import { base } from "./base/base";
import { scanComponent } from "./base/component";
import { loadPage } from "./base/page";
import { detectResponsiveMode } from "./base/responsive";
import { initBaseRoute, rebuildMeta } from "./base/route";
import { scanComponent } from "./base/component";
import { Vi } from "../vi/vi";
import { evalCJS } from "../ed/logic/ed-sync";
const w = window as any;
import { w } from "./w";
import { GlobalContext } from "web-utils";
export const Root = () => {
const local = useLocal({});
// #region context
const [_, set] = useState({});
const render = () => set({});
w.prasiContext.render = render;
const Provider = GlobalContext.Provider as FC<{ value: any; children: any }>;
// #endregion
// #region init
if (base.route.status !== "ready") {
@ -30,11 +35,11 @@ export const Root = () => {
);
if (site_script) {
for (const [k, v] of Object.entries(site_script)) {
w[k] = v;
(window as any)[k] = v;
}
}
local.render();
render();
});
}
return <Loading note="Loading router" />;
@ -66,10 +71,10 @@ export const Root = () => {
await scanComponent(root.childs);
rebuildMeta(p.meta, root);
base.page.cache[p.id] = p;
local.render();
render();
})
.catch(() => {
local.render();
render();
});
return <Loading note="Loading page" />;
@ -80,61 +85,63 @@ export const Root = () => {
// #endregion
return (
<div className={cx("relative flex flex-1 items-center justify-center")}>
<div
className={cx(
"absolute flex flex-col items-stretch flex-1 bg-white main-content-preview",
base.mode === "mobile"
? css`
@media (min-width: 768px) {
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
width: 375px;
top: 0px;
overflow-x: hidden;
overflow-y: auto;
bottom: 0px;
}
@media (max-width: 767px) {
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
overflow-y: auto;
}
`
: "inset-0 overflow-auto",
<Provider value={w.prasiContext}>
<div className={cx("relative flex flex-1 items-center justify-center")}>
<div
className={cx(
"absolute flex flex-col items-stretch flex-1 bg-white main-content-preview",
base.mode === "mobile"
? css`
@media (min-width: 768px) {
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
width: 375px;
top: 0px;
overflow-x: hidden;
overflow-y: auto;
bottom: 0px;
}
@media (max-width: 767px) {
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
overflow-y: auto;
}
`
: "inset-0 overflow-auto",
css`
contain: content;
`
)}
>
<Vi
api_url={base.site.api_url}
entry={Object.values(base.page.root.childs)
.filter((e) => e)
.map((e) => e.id)}
meta={base.page.meta}
mode={base.mode}
page_id={base.page.id}
site_id={base.site.id}
db={base.site.db}
api={base.site.api}
layout={
base.layout.id && base.layout.root && base.layout.meta
? {
id: base.layout.id,
meta: base.layout.meta,
entry: Object.values(base.layout.root.childs)
.filter((e) => e)
.map((e) => e.id),
}
: undefined
}
script={{ init_local_effect: base.init_local_effect }}
/>
css`
contain: content;
`
)}
>
<Vi
api_url={base.site.api_url}
entry={Object.values(base.page.root.childs)
.filter((e) => e)
.map((e) => e.id)}
meta={base.page.meta}
mode={base.mode}
page_id={base.page.id}
site_id={base.site.id}
db={base.site.db}
api={base.site.api}
layout={
base.layout.id && base.layout.root && base.layout.meta
? {
id: base.layout.id,
meta: base.layout.meta,
entry: Object.values(base.layout.root.childs)
.filter((e) => e)
.map((e) => e.id),
}
: undefined
}
script={{ init_local_effect: base.init_local_effect }}
/>
</div>
</div>
</div>
</Provider>
);
};

View File

@ -1,6 +1,8 @@
export const w = window as unknown as {
_prasi: {
context: {};
render: () => {};
prasiContext: {
global: any;
render: () => void;
};
params: any;
navigateOverride: (href: string) => void;
};