-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a tiny router to handle pags and auth state
- Loading branch information
1 parent
e659e3a
commit a02ea1c
Showing
15 changed files
with
169 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { $authState } from "./store.js"; | ||
import { $auth_state } from "./store.js"; | ||
|
||
export function hydrate() { | ||
const hydrate_el = document.querySelector("#APP_DATA"); | ||
|
||
$authState.value.user = JSON.parse( | ||
hydrate_el.textContent ?? '{"user": null}', | ||
).user; | ||
if (hydrate_el === null) { | ||
throw new Error("[UI]: hydration failure."); | ||
} | ||
|
||
const parsed = JSON.parse(hydrate_el.textContent); | ||
|
||
$auth_state.value = parsed.user; | ||
|
||
hydrate_el.remove(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { $authState } from "../store.js"; | ||
import { $auth_state } from "../store.js"; | ||
import { html } from "htm"; | ||
|
||
export default function () { | ||
return html` | ||
<div>Hello ${$authState.value.user.name}! Welcome to Okane.</div> | ||
`; | ||
export function home() { | ||
return html`<div>Hello ${$auth_state.value.name}! Welcome to Okane.</div>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { effect, html, signal } from "htm"; | ||
import { $current_route } from "./store.js"; | ||
|
||
/** | ||
* @type {import('types/router.d.ts').RouteRecord} | ||
*/ | ||
const PATH_TO_ROUTE = { | ||
login: async () => { | ||
const mod = await import("./pages/login_form.js"); | ||
return mod.login_form; | ||
}, | ||
home: async () => { | ||
const mod = await import("./pages/home.js"); | ||
return mod.home; | ||
}, | ||
}; | ||
|
||
/** | ||
* @type Map<string, import('types/router.d.ts').TPage> | ||
*/ | ||
const ROUTE_CACHE = new Map(); | ||
|
||
const current_component = signal(null); | ||
|
||
effect(() => { | ||
const curr_route = $current_route.value; | ||
|
||
if (curr_route === null) { | ||
current_component.value = null; | ||
return; | ||
} | ||
|
||
const from_cache = ROUTE_CACHE.get(curr_route); | ||
|
||
if (from_cache) { | ||
current_component.value = from_cache; | ||
return; | ||
} | ||
|
||
const component_loader = PATH_TO_ROUTE[curr_route]; | ||
|
||
if (!component_loader) { | ||
throw new Error(`${curr_route} doesn't have a route record`); | ||
} | ||
|
||
component_loader().then((func) => { | ||
current_component.value = func; | ||
ROUTE_CACHE.set(curr_route, func); | ||
}); | ||
}); | ||
|
||
/** | ||
* 60 loc router with lazy loading of pages | ||
*/ | ||
export function Router() { | ||
return html` | ||
${current_component.value === null | ||
? null | ||
: html`<${current_component.value}></${current_component.value}>`} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import { signal } from "htm"; | ||
import { signal, effect } from "htm"; | ||
|
||
export const $hashState = signal(null); | ||
/** | ||
* @type {import('htm').Signal<string|null>} | ||
*/ | ||
export const $current_route = signal(null); | ||
|
||
window.addEventListener("hashchange", () => { | ||
let hash = window.location.hash.replace(/^#/, ""); | ||
const hash = window.location.hash.replace(/^#/, ""); | ||
|
||
if (!$auth_state.peek()) { | ||
$current_route.value = "login"; | ||
return; | ||
} | ||
|
||
if (hash.length === 0) { | ||
$hashState.value = null; | ||
$current_route.value = null; | ||
} else { | ||
$hashState.value = hash; | ||
$current_route.value = hash; | ||
} | ||
}); | ||
|
||
export function goto(path) { | ||
window.location.hash = path; | ||
} | ||
|
||
export const $authState = signal({ | ||
user: null, | ||
/** | ||
* @type {import('htm').Signal<import('types/models.d.ts').User>} | ||
*/ | ||
export const $auth_state = signal(null); | ||
|
||
effect(() => { | ||
if ($auth_state.value) { | ||
goto("home"); | ||
} else { | ||
goto("login"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Signal, ReadonlySignal } from "@preact/signals-core"; | ||
|
||
declare module "htm" { | ||
export * from "@preact/signals-core"; | ||
|
||
export function html( | ||
strings: TemplateStringsArray, | ||
...values: Array<string | number | Signal<any> | ReadonlySignal<any>> | ||
): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface User { | ||
id: number; | ||
name: string; | ||
email: string; | ||
created_at: string; | ||
} | ||
|
||
export interface ResourceData<R> { | ||
data: R; | ||
} | ||
|
||
export interface APIError { | ||
error: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* render function of a page | ||
*/ | ||
export type TPage = () => any; | ||
|
||
export type RouteRecord = Record<string, () => Promise<TPage>>; |