mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 18:20:30 +08:00
Add our own router
This commit is contained in:
+38
-8
@@ -1,16 +1,46 @@
|
||||
<script lang="ts">
|
||||
import { Router, Route } from "svelte-routing";
|
||||
import { onMount } from "svelte";
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import * as router from "@app/lib/router";
|
||||
import { theme } from "@app/components/ThemeSwitch.svelte";
|
||||
import { unreachable } from "@app/lib/utils";
|
||||
|
||||
import DesignSystem from "@app/views/DesignSystem.svelte";
|
||||
import Repos from "@app/views/Repos.svelte";
|
||||
import Startup from "@app/views/Startup.svelte";
|
||||
import Home from "@app/views/Home.svelte";
|
||||
import AuthenticationError from "@app/views/AuthenticationError.svelte";
|
||||
|
||||
const activeRouteStore = router.activeRouteStore;
|
||||
void router.loadFromLocation();
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
await invoke("authenticate");
|
||||
void router.push({ resource: "home" });
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} catch (e: any) {
|
||||
void router.push({
|
||||
resource: "authenticationError",
|
||||
params: {
|
||||
error: e.err,
|
||||
hint: e.hint,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$: document.documentElement.setAttribute("data-theme", $theme);
|
||||
</script>
|
||||
|
||||
<Router>
|
||||
<Route path="/" component={Startup} />
|
||||
<Route path="/repos" component={Repos} />
|
||||
<Route path="/design-system" component={DesignSystem} />
|
||||
</Router>
|
||||
{#if $activeRouteStore.resource === "booting"}
|
||||
<!-- Don't show anything -->
|
||||
{:else if $activeRouteStore.resource === "home"}
|
||||
<Home />
|
||||
{:else if $activeRouteStore.resource === "authenticationError"}
|
||||
<AuthenticationError {...$activeRouteStore.params} />
|
||||
{:else if $activeRouteStore.resource === "designSystem"}
|
||||
<DesignSystem />
|
||||
{:else}
|
||||
{unreachable($activeRouteStore)}
|
||||
{/if}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
import Icon from "./Icon.svelte";
|
||||
import Popover from "./Popover.svelte";
|
||||
import ThemeSwitch from "./ThemeSwitch.svelte";
|
||||
|
||||
export let currentPage: string;
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@@ -26,17 +28,30 @@
|
||||
gap: 0.5rem;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.navigation :global(svg:hover) {
|
||||
display: flex;
|
||||
color: var(--color-fill-secondary);
|
||||
}
|
||||
</style>
|
||||
|
||||
<header class="flex-item">
|
||||
<div class="wrapper flex-item">
|
||||
<div class="wrapper-left flex-item">
|
||||
<div class="flex-item" style:gap="0.5rem">
|
||||
<Icon name="arrow-left" />
|
||||
<Icon name="arrow-right" />
|
||||
<div class="flex-item navigation" style:gap="0.5rem">
|
||||
<Icon
|
||||
name="arrow-left"
|
||||
on:click={() => {
|
||||
window.history.back();
|
||||
}} />
|
||||
<Icon
|
||||
name="arrow-right"
|
||||
on:click={() => {
|
||||
window.history.forward();
|
||||
}} />
|
||||
</div>
|
||||
<Fill variant="ghost" stylePadding="0 0.5rem" styleHeight="32px">
|
||||
Repositories
|
||||
{currentPage}
|
||||
</Fill>
|
||||
<Border variant="ghost" stylePadding="0 0.25rem" styleHeight="32px">
|
||||
<Icon name="plus" />
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
svg {
|
||||
display: flex;
|
||||
flex-shrink: 0;
|
||||
-webkit-touch-callout: none;
|
||||
-webkit-user-select: none;
|
||||
user-select: none;
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<script lang="ts" strictEvents>
|
||||
import type { Route } from "@app/lib/router/definitions";
|
||||
|
||||
import { createEventDispatcher } from "svelte";
|
||||
import { push, routeToPath } from "@app/lib/router";
|
||||
|
||||
export let route: Route;
|
||||
export let disabled: boolean = false;
|
||||
|
||||
const dispatch = createEventDispatcher<{
|
||||
afterNavigate: null;
|
||||
}>();
|
||||
|
||||
function navigateToRoute(event: MouseEvent): void {
|
||||
event.preventDefault();
|
||||
if (disabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
void push(route);
|
||||
dispatch("afterNavigate");
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
a {
|
||||
color: var(--color-fill-secondary);
|
||||
text-decoration: none;
|
||||
}
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
text-decoration-thickness: 1px;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<a on:click={navigateToRoute} href={routeToPath(route)}>
|
||||
<slot />
|
||||
</a>
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright © 2021 The Radicle Upstream Contributors
|
||||
//
|
||||
// This file is part of radicle-upstream, distributed under the GPLv3
|
||||
// with Radicle Linking Exception. For full terms see the included
|
||||
// LICENSE file.
|
||||
|
||||
//@ts-expect-error the typescript bindings are out of date.
|
||||
import * as Bacon from "baconjs";
|
||||
|
||||
// A task executor that runs only one task concurrently. If a new task
|
||||
// is run, any previously running task is aborted and the promise
|
||||
// returned from `run()` will return undefined.
|
||||
//
|
||||
// import * as mutexExecutor from "ui/src/mutexExecutor"
|
||||
// const executor = mutexExecutor.create()
|
||||
// const first = await executor.run(async () => {
|
||||
// await sleep(1000)
|
||||
// return "first"
|
||||
// })
|
||||
// const second = await executor.run(async () => "second")
|
||||
//
|
||||
// In the example above the promise `first` will resolve to `undefined`
|
||||
// while the promise `second` will resolve to "second".
|
||||
//
|
||||
// If the first tasks throws after the second task has run the
|
||||
// behavior is the same.
|
||||
//
|
||||
// const first = await executor.run(async () => {
|
||||
// await sleep(1000)
|
||||
// throw new Error()
|
||||
// })
|
||||
//
|
||||
// The task call back receives an AbortSignal as a parameter. The abort
|
||||
// event is emitted when another task is run.
|
||||
export function create(): MutexExecutor {
|
||||
return new MutexExecutor();
|
||||
}
|
||||
|
||||
// A worker that asynchronously process one item at a time and provides
|
||||
// the result as an event stream.
|
||||
//
|
||||
// import * as mutexExecutor from "ui/src/mutexExecutor"
|
||||
// const worker = mutexExecutor.createWorker(async (value) => {
|
||||
// await sleep(1000)
|
||||
// return value
|
||||
// })
|
||||
//
|
||||
// const firstPromise = worker.output.firstToPromise()
|
||||
// worker.push("first)
|
||||
// assert.equal(await firstPromise, "first")
|
||||
//
|
||||
// When an item is submitted to the worker while the previous items is
|
||||
// still being processed, the result of the first item will not be
|
||||
// emitted to `worker.output`. Instead, only the last item will be
|
||||
// emitted.
|
||||
export function createWorker<In, Out>(
|
||||
fn: (x: In, abortSignal: AbortSignal) => Promise<Out>,
|
||||
): MutexWorker<In, Out> {
|
||||
return new MutexWorker(fn);
|
||||
}
|
||||
|
||||
class MutexExecutor {
|
||||
private runningTaskId = 0;
|
||||
private abortController: AbortController | null = null;
|
||||
|
||||
public async run<T>(
|
||||
f: (abortSignal: AbortSignal) => Promise<T>,
|
||||
): Promise<T | undefined> {
|
||||
this.runningTaskId += 1;
|
||||
const taskId = this.runningTaskId;
|
||||
|
||||
if (this.abortController) {
|
||||
this.abortController.abort();
|
||||
}
|
||||
this.abortController = new AbortController();
|
||||
return f(this.abortController.signal).then(
|
||||
data => {
|
||||
if (this.runningTaskId === taskId) {
|
||||
return data;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
err => {
|
||||
if (this.runningTaskId === taskId) {
|
||||
throw err;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MutexWorker<In, Out> {
|
||||
private outputBus = new Bacon.Bus<Out>();
|
||||
private executor = new MutexExecutor();
|
||||
|
||||
public output: Bacon.EventStream<Out>;
|
||||
|
||||
public constructor(
|
||||
private fn: (x: In, abortSignal: AbortSignal) => Promise<Out>,
|
||||
) {
|
||||
this.output = this.outputBus.toEventStream();
|
||||
}
|
||||
|
||||
public async submit(x: In): Promise<void> {
|
||||
const output = await this.executor.run(abort => this.fn(x, abort));
|
||||
if (output !== undefined) {
|
||||
this.outputBus.push(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import type { LoadedRoute, Route } from "@app/lib/router/definitions";
|
||||
|
||||
import { get, writable } from "svelte/store";
|
||||
|
||||
import * as mutexExecutor from "@app/lib/mutexExecutor";
|
||||
import * as utils from "@app/lib/utils";
|
||||
import { loadRoute } from "@app/lib/router/definitions";
|
||||
|
||||
export { type Route };
|
||||
|
||||
const InitialStore = { resource: "booting" as const };
|
||||
|
||||
export const isLoading = writable<boolean>(true);
|
||||
export const activeRouteStore = writable<LoadedRoute>(InitialStore);
|
||||
export const activeUnloadedRouteStore = writable<Route>(InitialStore);
|
||||
|
||||
let currentUrl: URL | undefined;
|
||||
|
||||
export async function loadFromLocation(): Promise<void> {
|
||||
await navigateToUrl("replace", new URL(window.location.href));
|
||||
}
|
||||
|
||||
async function navigateToUrl(
|
||||
action: "push" | "replace",
|
||||
url: URL,
|
||||
): Promise<void> {
|
||||
const { pathname, hash } = url;
|
||||
|
||||
if (url.origin !== window.origin) {
|
||||
throw new Error("Cannot navigate to other origin");
|
||||
}
|
||||
|
||||
if (
|
||||
currentUrl &&
|
||||
currentUrl.pathname === pathname &&
|
||||
currentUrl.search === url.search
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const relativeUrl = pathname + url.search + (hash || "");
|
||||
url = new URL(relativeUrl, window.origin);
|
||||
const route = urlToRoute(url);
|
||||
|
||||
if (route) {
|
||||
await navigate(action, route);
|
||||
} else {
|
||||
await navigate(action, {
|
||||
// On 404 we redirect to the Home page, shouldn't happen.
|
||||
resource: "home",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("popstate", () => loadFromLocation());
|
||||
|
||||
const loadExecutor = mutexExecutor.create();
|
||||
|
||||
async function navigate(
|
||||
action: "push" | "replace",
|
||||
newRoute: Route,
|
||||
): Promise<void> {
|
||||
isLoading.set(true);
|
||||
const path = routeToPath(newRoute);
|
||||
|
||||
if (action === "push") {
|
||||
window.history.pushState(newRoute, "", path);
|
||||
} else if (action === "replace") {
|
||||
window.history.replaceState(newRoute, "");
|
||||
}
|
||||
currentUrl = new URL(window.location.href);
|
||||
const currentLoadedRoute = get(activeRouteStore);
|
||||
|
||||
const loadedRoute = await loadExecutor.run(async () => {
|
||||
return loadRoute(newRoute, currentLoadedRoute);
|
||||
});
|
||||
|
||||
// Only let the last request through.
|
||||
if (loadedRoute === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
setTitle(loadedRoute);
|
||||
activeRouteStore.set(loadedRoute);
|
||||
activeUnloadedRouteStore.set(newRoute);
|
||||
isLoading.set(false);
|
||||
}
|
||||
|
||||
function setTitle(loadedRoute: LoadedRoute) {
|
||||
const title: string[] = [];
|
||||
|
||||
if (loadedRoute.resource === "booting") {
|
||||
title.push("Radicle");
|
||||
} else if (loadedRoute.resource === "home") {
|
||||
title.push("Home");
|
||||
title.push("Radicle");
|
||||
} else if (loadedRoute.resource === "authenticationError") {
|
||||
title.push("Authentication Error");
|
||||
title.push("Radicle");
|
||||
} else if (loadedRoute.resource === "designSystem") {
|
||||
title.push("Design System");
|
||||
title.push("Radicle");
|
||||
} else {
|
||||
utils.unreachable(loadedRoute);
|
||||
}
|
||||
|
||||
document.title = title.join(" · ");
|
||||
}
|
||||
|
||||
export async function push(newRoute: Route): Promise<void> {
|
||||
await navigate("push", newRoute);
|
||||
}
|
||||
|
||||
export async function replace(newRoute: Route): Promise<void> {
|
||||
await navigate("replace", newRoute);
|
||||
}
|
||||
|
||||
function urlToRoute(url: URL): Route | null {
|
||||
const segments = url.pathname.substring(1).split("/");
|
||||
|
||||
const resource = segments.shift();
|
||||
switch (resource) {
|
||||
case "": {
|
||||
return { resource: "home" };
|
||||
}
|
||||
case "authenticationError": {
|
||||
return { resource: "authenticationError", params: { error: "" } };
|
||||
}
|
||||
case "designSystem": {
|
||||
return { resource: "designSystem" };
|
||||
}
|
||||
default: {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function routeToPath(route: Route): string {
|
||||
if (route.resource === "home") {
|
||||
return "/";
|
||||
} else if (route.resource === "authenticationError") {
|
||||
return "/authenticationError";
|
||||
} else if (route.resource === "designSystem") {
|
||||
return "/designSystem";
|
||||
} else if (route.resource === "booting") {
|
||||
return "";
|
||||
} else {
|
||||
return utils.unreachable(route);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
interface BootingRoute {
|
||||
resource: "booting";
|
||||
}
|
||||
|
||||
interface AuthenticationErrorRoute {
|
||||
resource: "authenticationError";
|
||||
params: {
|
||||
error: string;
|
||||
hint?: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface HomeRoute {
|
||||
resource: "home";
|
||||
}
|
||||
|
||||
interface DesignSystemRoute {
|
||||
resource: "designSystem";
|
||||
}
|
||||
|
||||
export type Route =
|
||||
| BootingRoute
|
||||
| DesignSystemRoute
|
||||
| HomeRoute
|
||||
| AuthenticationErrorRoute;
|
||||
|
||||
export type LoadedRoute =
|
||||
| BootingRoute
|
||||
| DesignSystemRoute
|
||||
| HomeRoute
|
||||
| AuthenticationErrorRoute;
|
||||
|
||||
export async function loadRoute(
|
||||
route: Route,
|
||||
_previousLoaded: LoadedRoute,
|
||||
): Promise<LoadedRoute> {
|
||||
return route;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<script lang="ts">
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
|
||||
export let error: string;
|
||||
export let hint: string | undefined = undefined;
|
||||
</script>
|
||||
|
||||
<style>
|
||||
main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
row-gap: 0.5rem;
|
||||
}
|
||||
|
||||
/* This tag comes from the backend. */
|
||||
:global(code) {
|
||||
font-family: var(--font-family-monospace);
|
||||
font-size: var(--font-size-small);
|
||||
background-color: var(--color-fill-ghost);
|
||||
padding: 0.125rem 0.25rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<main>
|
||||
<Icon name="warning" size="32" />
|
||||
<div class="txt-medium txt-semibold">
|
||||
{error}
|
||||
</div>
|
||||
{#if hint}
|
||||
<div class="txt-small">{@html hint}</div>
|
||||
{/if}
|
||||
</main>
|
||||
@@ -1,42 +1,18 @@
|
||||
<script lang="ts">
|
||||
import Border from "@app/components/Border.svelte";
|
||||
import Button from "@app/components/Button.svelte";
|
||||
import Fill from "@app/components/Fill.svelte";
|
||||
import Header from "@app/components/Header.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
|
||||
let theme = "dark";
|
||||
|
||||
$: document.documentElement.setAttribute("data-theme", theme);
|
||||
import Link from "@app/components/Link.svelte";
|
||||
</script>
|
||||
|
||||
<Header currentPage="Design System" />
|
||||
|
||||
<div style="display: flex; gap: 1rem;">
|
||||
<div style="display: flex; gap: 1rem;">
|
||||
<Border variant="secondary">
|
||||
<Fill
|
||||
stylePadding="0 0.5rem"
|
||||
variant={theme === "dark" ? "secondary" : "transparent"}
|
||||
on:click={() => {
|
||||
theme = "dark";
|
||||
}}>
|
||||
<Icon name="seedling" />
|
||||
Dark
|
||||
</Fill>
|
||||
|
||||
<Fill
|
||||
stylePadding="0 0.5rem"
|
||||
variant={theme === "light" ? "secondary" : "transparent"}
|
||||
on:click={() => {
|
||||
theme = "light";
|
||||
}}>
|
||||
<Icon name="seedling" />
|
||||
Light
|
||||
</Fill>
|
||||
</Border>
|
||||
</div>
|
||||
|
||||
<div style="display: flex; gap: 1rem;">
|
||||
<Button variant="primary"><Icon name="seedling" />Press me</Button>
|
||||
<Button variant="secondary"><Icon name="seedling" /> Press me</Button>
|
||||
<Button variant="ghost"><Icon name="seedling" /> Press me</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
👉 <Link route={{ resource: "home" }}>Home</Link>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Header from "@app/components/Header.svelte";
|
||||
import Link from "@app/components/Link.svelte";
|
||||
</script>
|
||||
|
||||
<Header currentPage="Repositories" />
|
||||
|
||||
👉 <Link route={{ resource: "designSystem" }}>Design System</Link>
|
||||
@@ -1,47 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { onMount } from "svelte";
|
||||
|
||||
import Repos from "@app/views/Repos.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
|
||||
let loading = true;
|
||||
let error: { err: string; hint?: string } | undefined = undefined;
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
await invoke("authenticate");
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} catch (e: any) {
|
||||
error = e;
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style>
|
||||
main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
row-gap: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
{#if error}
|
||||
<main>
|
||||
<Icon name="warning" size="32" />
|
||||
<div class="txt-medium txt-semibold">
|
||||
{error.err}
|
||||
</div>
|
||||
{#if error.hint}
|
||||
<div class="txt-small">{@html error.hint}</div>
|
||||
{/if}
|
||||
</main>
|
||||
{:else if !loading}
|
||||
<Repos />
|
||||
{/if}
|
||||
Reference in New Issue
Block a user