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) {
|
||||
window.navigator.registerProtocolHandler(
|
||||
"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'");
|
||||
}
|
||||
|
@ -49,10 +49,12 @@ const Artwork: Array<ArtworkEntry> = [
|
||||
|
||||
export async function getNip05PubKey(addr: string): Promise<string> {
|
||||
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) {
|
||||
const data = await rsp.json();
|
||||
const pKey = data.names[username];
|
||||
const pKey = data.names[username.toLowerCase()];
|
||||
if (pKey) {
|
||||
return pKey;
|
||||
}
|
||||
|
@ -1,19 +1,22 @@
|
||||
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 { useNavigate, useParams } from "react-router-dom";
|
||||
|
||||
import Spinner from "Icons/Spinner";
|
||||
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() {
|
||||
const params = useParams();
|
||||
const [loading, setLoading] = useState(true);
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const link = decodeURIComponent(params["*"] ?? "").toLowerCase();
|
||||
|
||||
useEffect(() => {
|
||||
if (link.length > 0) {
|
||||
async function handleLink(link: string) {
|
||||
const nav = parseNostrLink(link);
|
||||
if (nav) {
|
||||
if ((nav.relays?.length ?? 0) > 0) {
|
||||
@ -30,9 +33,34 @@ export default function NostrLinkHandler() {
|
||||
} 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(() => {
|
||||
if (link.length > 0) {
|
||||
handleLink(link).catch(console.error);
|
||||
}
|
||||
}, [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?",
|
||||
element: <SearchPage />,
|
||||
},
|
||||
{
|
||||
path: "/handler/*",
|
||||
element: <NostrLinkHandler />,
|
||||
},
|
||||
...NewUserRoutes,
|
||||
...WalletRoutes,
|
||||
{
|
||||
path: "/*",
|
||||
element: <NostrLinkHandler />,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user