feat: short links

This commit is contained in:
Kieran 2023-03-31 10:51:50 +01:00
parent 6fe19ee29d
commit 8bd694eb15
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
4 changed files with 57 additions and 27 deletions

View File

@ -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'");
}

View File

@ -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;
}

View File

@ -1,38 +1,66 @@
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();
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(() => {
if (link.length > 0) {
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()}`);
}
}
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>
);
}

View File

@ -92,12 +92,12 @@ export const router = createBrowserRouter([
path: "/search/:keyword?",
element: <SearchPage />,
},
{
path: "/handler/*",
element: <NostrLinkHandler />,
},
...NewUserRoutes,
...WalletRoutes,
{
path: "/*",
element: <NostrLinkHandler />,
},
],
},
]);