This commit is contained in:
Rizky 2023-10-15 11:47:40 +07:00
parent fa3a6c1dd5
commit a295b2ead8
8 changed files with 92 additions and 18 deletions

View File

@ -3,11 +3,11 @@ import { page } from "web-utils";
import { Loading } from "../../utils/ui/loading";
export default page({
url: "**",
url: "*",
component: ({}) => {
useEffect(() => {
if (localStorage.getItem("prasi-session")) {
navigate("/editor/_/_");
navigate("/editor");
} else {
navigate("/login");
}

View File

@ -23,7 +23,7 @@ export default page({
} else {
console.log("navigate to");
localStorage.setItem("prasi-session", JSON.stringify(s));
navigate("/editor/_/_");
navigate("/editor/");
}
} else {
form.init = true;
@ -52,7 +52,7 @@ export default page({
if (rto) {
navigate(rto);
} else {
navigate("/editor/_/_");
navigate("/editor");
}
}
}}

View File

@ -1,13 +1,28 @@
export const all = {
url: "**",
page: () => import("./page/all"),
};
export const login = {
url: "/login",
export const auth_login = {
url: "/login",
page: () => import("./page/auth/login"),
};
export const auth_logout = {
url: "/logout",
page: () => import("./page/auth/logout"),
};
export const auth_register = {
url: "/register",
page: () => import("./page/auth/register"),
};
export const all = {
url: "*",
page: () => import("./page/all"),
};
export const ed = {
url: "/ed/:site_id/:page_id",
page: () => import("./page/ed"),
};
export const editor = {
url: "/editor/:site_id/:page_id",
url: "/editor/:site_id/:page_id",
page: () => import("./page/editor"),
};
export const live = {
url: "/live/:domain/**",
page: () => import("./page/live"),
};

View File

@ -8,7 +8,7 @@ export const Root: FC<{}> = ({}) => {
const local = useLocal(
{
router: createRouter<{ url: string; Page: FC<any> }>({
strictTrailingSlash: true,
strictTrailingSlash: false,
}),
Page: null as any,
},
@ -31,6 +31,8 @@ export const Root: FC<{}> = ({}) => {
const Provider = GlobalContext.Provider as FC<{ value: any; children: any }>;
const found = local.router.lookup(location.pathname);
console.log(found)
if (found) {
w.params = found.params;
local.Page = found.Page;

View File

@ -27,6 +27,7 @@ export const editorWS = async (p: PG) => {
if (p.ws && p.ws.readyState === p.ws.OPEN) {
return;
}
const render = () => {
if (!p.focused && !p.script.active) {
p.render();

View File

@ -1,5 +1,6 @@
import { createRouter } from "radix3";
import { validate } from "uuid";
import { createRouter, type apiClient } from "web-utils";
import { type apiClient } from "web-utils";
import {
createAPI,
createDB,
@ -9,7 +10,6 @@ import {
import importModule from "../../editor/tools/dynamic-import";
import { LSite, PG } from "./global";
import { validateLayout } from "./layout";
import { preload } from "./route";
export const w = window as unknown as {
basepath: string;
@ -195,7 +195,7 @@ export const initLive = async (p: PG, domain: string) => {
}
/** create router */
p.route = createRouter({ strictTrailingSlash: true });
p.route = createRouter({ strictTrailingSlash: false });
if (pages && pages.length > 0) {
for (const page of pages) {
p.route.insert(page.url, page);

View File

@ -11,7 +11,7 @@ export type WSData = { url: URL };
export const createServer = async () => {
g.api = {};
g.router = createRouter({ strictTrailingSlash: true });
g.router = createRouter({ strictTrailingSlash: false });
g.server = Bun.serve({
port: g.port,
websocket: {

View File

@ -1,7 +1,9 @@
import { file } from "bun";
import { watch } from "fs";
import { dirAsync, inspectTreeAsync, readAsync, writeAsync } from "fs-jetpack";
import { dirname } from "path";
import { dir } from "./dir";
import { dirAsync } from "fs-jetpack";
const pagedir = dir.path(`app/web/src/base/page`);
export const startDevWatcher = async () => {
await dirAsync(dir.path(`app/srv/api`));
@ -23,4 +25,58 @@ export const _ = {
);
}
});
await dirAsync(pagedir);
await genPages();
watch(pagedir, async (event, filename) => {
const s = file(dir.path(`${pagedir}/${filename}`));
if (s.size > 0) {
// await Bun.write(
// `app/srv/api/${filename}`,
// `\
// export const all = {
// url: "*",
// page: () => import("./page/all"),
// };
// `
// );
}
});
};
const genPages = async () => {
const res: string[] = [];
const walk = async (arg: any) => {
const pathname = arg.relativePath
.substring(0, arg.relativePath.length - 4)
.substring(2);
if (pathname) {
const src = await readAsync(`${pagedir}/${pathname}.tsx`);
if (src) {
const url = src.split("url:")[1].split(",").shift();
if (url) {
res.push(
`\
export const ${pathname.replace(/\W/gi, "_")} = {
url: ${url},
page: () => import("./page/${pathname}"),
};`
);
}
}
}
if (arg.children) {
for (const c of arg.children) {
await walk(c);
}
}
};
await walk(
await inspectTreeAsync(pagedir, {
relativePath: true,
})
);
await writeAsync(dirname(pagedir) + "/pages.ts", `${res.join("\n")}\n`);
};