feat: short links
This commit is contained in:
parent
6fe19ee29d
commit
8bd694eb15
@ -110,7 +110,7 @@ export default function Layout() {
|
|||||||
if ("registerProtocolHandler" in window.navigator) {
|
if ("registerProtocolHandler" in window.navigator) {
|
||||||
window.navigator.registerProtocolHandler(
|
window.navigator.registerProtocolHandler(
|
||||||
"web+nostr",
|
"web+nostr",
|
||||||
`${window.location.protocol}//${window.location.host}/handler/%s`
|
`${window.location.protocol}//${window.location.host}/%s`
|
||||||
);
|
);
|
||||||
console.info("Registered protocol handler for 'web+nostr'");
|
console.info("Registered protocol handler for 'web+nostr'");
|
||||||
}
|
}
|
||||||
|
@ -49,10 +49,12 @@ const Artwork: Array<ArtworkEntry> = [
|
|||||||
|
|
||||||
export async function getNip05PubKey(addr: string): Promise<string> {
|
export async function getNip05PubKey(addr: string): Promise<string> {
|
||||||
const [username, domain] = addr.split("@");
|
const [username, domain] = addr.split("@");
|
||||||
const rsp = await fetch(`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(username)}`);
|
const rsp = await fetch(
|
||||||
|
`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(username.toLocaleLowerCase())}`
|
||||||
|
);
|
||||||
if (rsp.ok) {
|
if (rsp.ok) {
|
||||||
const data = await rsp.json();
|
const data = await rsp.json();
|
||||||
const pKey = data.names[username];
|
const pKey = data.names[username.toLowerCase()];
|
||||||
if (pKey) {
|
if (pKey) {
|
||||||
return pKey;
|
return pKey;
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,66 @@
|
|||||||
import { NostrPrefix } from "@snort/nostr";
|
import { NostrPrefix } from "@snort/nostr";
|
||||||
import { useEffect } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { FormattedMessage } from "react-intl";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
|
|
||||||
|
import Spinner from "Icons/Spinner";
|
||||||
import { setRelays } from "State/Login";
|
import { setRelays } from "State/Login";
|
||||||
import { parseNostrLink, unixNowMs, unwrap } from "Util";
|
import { parseNostrLink, profileLink, unixNowMs, unwrap } from "Util";
|
||||||
|
import { getNip05PubKey } from "Pages/Login";
|
||||||
|
|
||||||
export default function NostrLinkHandler() {
|
export default function NostrLinkHandler() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const link = decodeURIComponent(params["*"] ?? "").toLowerCase();
|
const link = decodeURIComponent(params["*"] ?? "").toLowerCase();
|
||||||
|
|
||||||
|
async function handleLink(link: string) {
|
||||||
|
const nav = parseNostrLink(link);
|
||||||
|
if (nav) {
|
||||||
|
if ((nav.relays?.length ?? 0) > 0) {
|
||||||
|
// todo: add as ephemerial connection
|
||||||
|
dispatch(
|
||||||
|
setRelays({
|
||||||
|
relays: Object.fromEntries(unwrap(nav.relays).map(a => [a, { read: true, write: false }])),
|
||||||
|
createdAt: unixNowMs(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (nav.type === NostrPrefix.Event || nav.type === NostrPrefix.Note || nav.type === NostrPrefix.Address) {
|
||||||
|
navigate(`/e/${nav.encode()}`);
|
||||||
|
} else if (nav.type === NostrPrefix.PublicKey || nav.type === NostrPrefix.Profile) {
|
||||||
|
navigate(`/p/${nav.encode()}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const pubkey = await getNip05PubKey(`${link}@snort.social`);
|
||||||
|
if (pubkey) {
|
||||||
|
navigate(profileLink(pubkey));
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
//ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (link.length > 0) {
|
if (link.length > 0) {
|
||||||
const nav = parseNostrLink(link);
|
handleLink(link).catch(console.error);
|
||||||
if (nav) {
|
|
||||||
if ((nav.relays?.length ?? 0) > 0) {
|
|
||||||
// todo: add as ephemerial connection
|
|
||||||
dispatch(
|
|
||||||
setRelays({
|
|
||||||
relays: Object.fromEntries(unwrap(nav.relays).map(a => [a, { read: true, write: false }])),
|
|
||||||
createdAt: unixNowMs(),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (nav.type === NostrPrefix.Event || nav.type === NostrPrefix.Note || nav.type === NostrPrefix.Address) {
|
|
||||||
navigate(`/e/${nav.encode()}`);
|
|
||||||
} else if (nav.type === NostrPrefix.PublicKey || nav.type === NostrPrefix.Profile) {
|
|
||||||
navigate(`/p/${nav.encode()}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [link]);
|
}, [link]);
|
||||||
|
|
||||||
return <>Could not handle {link}</>;
|
return (
|
||||||
|
<div className="flex f-center">
|
||||||
|
{loading ? (
|
||||||
|
<Spinner width={50} height={50} />
|
||||||
|
) : (
|
||||||
|
<b className="error">
|
||||||
|
<FormattedMessage defaultMessage="Nothing found :/" />
|
||||||
|
</b>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -92,12 +92,12 @@ export const router = createBrowserRouter([
|
|||||||
path: "/search/:keyword?",
|
path: "/search/:keyword?",
|
||||||
element: <SearchPage />,
|
element: <SearchPage />,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
path: "/handler/*",
|
|
||||||
element: <NostrLinkHandler />,
|
|
||||||
},
|
|
||||||
...NewUserRoutes,
|
...NewUserRoutes,
|
||||||
...WalletRoutes,
|
...WalletRoutes,
|
||||||
|
{
|
||||||
|
path: "/*",
|
||||||
|
element: <NostrLinkHandler />,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user