mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 02:00:31 +08:00
Open rad: links in the app
This commit is contained in:
+3
-1
@@ -14,6 +14,7 @@
|
||||
resetFontSize,
|
||||
} from "@app/lib/appearance.svelte";
|
||||
import { checkAuth, startup } from "@app/lib/auth.svelte";
|
||||
import { deepLinksReady, listenForDeepLinks } from "@app/lib/deepLink";
|
||||
import { nodeRunning } from "@app/lib/events";
|
||||
import { dynamicInterval } from "@app/lib/interval";
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
@@ -197,9 +198,10 @@
|
||||
setUnlistenNodeEvents(() => clearInterval(interval));
|
||||
}
|
||||
|
||||
void listenForDeepLinks();
|
||||
try {
|
||||
await invoke("authenticate");
|
||||
void router.loadFromLocation();
|
||||
void router.loadFromLocation().then(deepLinksReady);
|
||||
dynamicInterval(
|
||||
"auth",
|
||||
checkAuth,
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { ErrorWrapper } from "@bindings/error/ErrorWrapper";
|
||||
|
||||
import { get } from "svelte/store";
|
||||
|
||||
import { deepLinksReady } from "@app/lib/deepLink";
|
||||
import { dynamicInterval } from "@app/lib/interval";
|
||||
import { invoke } from "@app/lib/invoke";
|
||||
import * as router from "@app/lib/router";
|
||||
@@ -23,7 +24,9 @@ export async function checkAuth() {
|
||||
import.meta.env.VITE_AUTH_LONG_DELAY || 30_000,
|
||||
);
|
||||
if (get(router.activeRouteStore).resource === "booting") {
|
||||
void router.push({ resource: "inbox" });
|
||||
void router.push({ resource: "inbox" }).then(deepLinksReady);
|
||||
} else {
|
||||
deepLinksReady();
|
||||
}
|
||||
} catch (err) {
|
||||
const error = err as ErrorWrapper;
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import type { RepoRoute } from "@app/views/repo/router";
|
||||
import type { Config } from "@bindings/config/Config";
|
||||
|
||||
import { cachedConfig, cachedRepoById } from "@app/lib/invoke";
|
||||
import { referenceRoute } from "@app/lib/mentions";
|
||||
import { show } from "@app/lib/modal";
|
||||
import type { RadReference, RadUri } from "@app/lib/radUri";
|
||||
import { parseRadUri } from "@app/lib/radUri";
|
||||
import * as router from "@app/lib/router";
|
||||
import { explorerHost, explorerLink } from "@app/lib/utils";
|
||||
|
||||
import OpenInBrowser from "@app/modals/OpenInBrowser.svelte";
|
||||
|
||||
export type DeepLinkTarget =
|
||||
{ type: "route"; route: RepoRoute } | { type: "external"; url: string };
|
||||
|
||||
export function parseDeepLink(
|
||||
link: string,
|
||||
): { type: "uri"; uri: RadUri } | undefined {
|
||||
const uri = parseRadUri(link.trim());
|
||||
|
||||
return uri && { type: "uri", uri };
|
||||
}
|
||||
|
||||
export function deepLinkTarget(
|
||||
reference: RadReference,
|
||||
local: boolean,
|
||||
config: Config,
|
||||
): DeepLinkTarget | undefined {
|
||||
const route = local ? referenceRoute(reference) : undefined;
|
||||
if (route) return { type: "route", route };
|
||||
|
||||
const url = explorerLink(reference, config);
|
||||
|
||||
return url ? { type: "external", url } : undefined;
|
||||
}
|
||||
|
||||
// Links can arrive before the app has authenticated and routed, so they wait
|
||||
// until it has, and are opened one at a time.
|
||||
export function createLinkQueue(open: (link: string) => Promise<void>) {
|
||||
const pending: string[] = [];
|
||||
let ready = false;
|
||||
let flushing = false;
|
||||
|
||||
async function flush() {
|
||||
if (!ready || flushing) return;
|
||||
flushing = true;
|
||||
try {
|
||||
for (let link = pending.shift(); link; link = pending.shift()) {
|
||||
await open(link).catch(console.error);
|
||||
}
|
||||
} finally {
|
||||
flushing = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
receive(links: string[]) {
|
||||
pending.push(...links);
|
||||
void flush();
|
||||
},
|
||||
ready() {
|
||||
ready = true;
|
||||
void flush();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// An untrusted link only ever navigates; it never fetches or seeds, and
|
||||
// leaving the app for the browser needs the user's confirmation.
|
||||
async function openDeepLink(link: string) {
|
||||
const reference = parseDeepLink(link);
|
||||
if (!reference) return;
|
||||
|
||||
const [config, repo] = await Promise.all([
|
||||
cachedConfig(),
|
||||
cachedRepoById(`rad:${reference.uri.repo}`).catch(() => undefined),
|
||||
]);
|
||||
const target = deepLinkTarget(reference, Boolean(repo), config);
|
||||
if (target?.type === "route") {
|
||||
await router.push(target.route);
|
||||
} else if (target?.type === "external") {
|
||||
show({
|
||||
component: OpenInBrowser,
|
||||
props: { url: target.url, host: explorerHost(config) },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const queue = createLinkQueue(openDeepLink);
|
||||
const launchKey = "deepLink:launch";
|
||||
|
||||
export function deepLinksReady() {
|
||||
queue.ready();
|
||||
}
|
||||
|
||||
export async function listenForDeepLinks() {
|
||||
if (!window.__TAURI_INTERNALS__) return;
|
||||
|
||||
const { getCurrent, onOpenUrl } =
|
||||
await import("@tauri-apps/plugin-deep-link");
|
||||
await onOpenUrl(links => queue.receive(links));
|
||||
|
||||
// A reload of the webview reports the same launch link again.
|
||||
const launched = (await getCurrent()) ?? [];
|
||||
const seen = readLaunch();
|
||||
if (launched.length > 0 && JSON.stringify(launched) !== seen) {
|
||||
writeLaunch(JSON.stringify(launched));
|
||||
queue.receive(launched);
|
||||
}
|
||||
}
|
||||
|
||||
function readLaunch(): string | null {
|
||||
try {
|
||||
return sessionStorage.getItem(launchKey);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function writeLaunch(value: string) {
|
||||
try {
|
||||
sessionStorage.setItem(launchKey, value);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<script lang="ts">
|
||||
import { hide } from "@app/lib/modal";
|
||||
|
||||
import Button from "@app/components/Button.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
|
||||
interface Props {
|
||||
url: string;
|
||||
host: string;
|
||||
}
|
||||
|
||||
const { url, host }: Props = $props();
|
||||
|
||||
async function open() {
|
||||
const { open } = await import("@tauri-apps/plugin-shell");
|
||||
await open(url);
|
||||
hide();
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.modal {
|
||||
width: 26rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
border: 1px solid var(--color-border-subtle);
|
||||
border-radius: var(--border-radius-lg);
|
||||
background-color: var(--color-surface-canvas);
|
||||
overflow: hidden;
|
||||
}
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0 1.5rem;
|
||||
height: 3.25rem;
|
||||
flex-shrink: 0;
|
||||
border-bottom: 1px solid var(--color-border-subtle);
|
||||
}
|
||||
.title {
|
||||
font: var(--txt-heading-s);
|
||||
color: var(--color-text-primary);
|
||||
}
|
||||
.body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
padding: 1.5rem;
|
||||
font: var(--txt-body-m-regular);
|
||||
color: var(--color-text-secondary);
|
||||
}
|
||||
.url {
|
||||
font: var(--txt-code-regular);
|
||||
color: var(--color-text-tertiary);
|
||||
word-break: break-all;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 0.5rem;
|
||||
padding: 0 1.5rem 1.5rem;
|
||||
}
|
||||
.confirm-label {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="modal">
|
||||
<div class="header">
|
||||
<span class="title">Open in browser</span>
|
||||
<Button variant="naked" onclick={hide}>
|
||||
<span style:color="var(--color-text-tertiary)">
|
||||
<Icon name="close" />
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div class="body">
|
||||
<span>
|
||||
A link was opened for something that isn't on this node. It can be viewed
|
||||
on {host} instead.
|
||||
</span>
|
||||
<span class="url">{url}</span>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<Button variant="outline" onclick={hide}>Cancel</Button>
|
||||
<Button variant="ghost" onclick={() => void open()}>
|
||||
<span class="confirm-label">
|
||||
<Icon name="open-external" />
|
||||
Open in {host}
|
||||
</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user