Skip to content

Commit

Permalink
Revert "feat: nip-66 relay status fetching"
Browse files Browse the repository at this point in the history
This reverts commit 1d47dfe.
  • Loading branch information
verbiricha committed Jan 2, 2024
1 parent 1d47dfe commit c609a47
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 106 deletions.
2 changes: 1 addition & 1 deletion apps/badges/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const metadata = {
metadataBase: new URL("https://badges.page"),
metadataBase: new URL('https://badges.page'),
title: "Badges",
description: "Create, collect and award badges",
};
Expand Down
33 changes: 33 additions & 0 deletions apps/emojis/app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Relay, Tag } from "@ngine/core";

export function encodeRelayURL(url: string): string {
url = url.trim();
if (url.startsWith("wss://")) {
url = url.slice(6);
}
return encodeURIComponent(url);
}

export function relayToTag(r: Relay): Tag {
if (r.read && !r.write) {
return ["r", r.url, "read"];
}
if (r.write && !r.read) {
return ["r", r.url, "write"];
}
return ["r", r.url];
}

export function tagToRelay(t: Tag): Relay {
const url = t[1].replace(/\/$/, "");

if (t[2] === "read") {
return { url, read: true, write: false };
}

if (t[2] === "write") {
return { url, read: false, write: true };
}

return { url, read: true, write: true };
}
6 changes: 1 addition & 5 deletions apps/emojis/ui/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { theme } from "./theme";

const cacheAdapter = new NDKCacheAdapterDexie({ dbName: "emojis" });
const ndk = new NDK({
explicitRelayUrls: [
"wss://nos.lol",
"wss://relay.nostr.band",
"wss://frens.nostr1.com",
],
explicitRelayUrls: ["wss://nos.lol", "wss://relay.nostr.band", "wss://frens.nostr1.com"],
outboxRelayUrls: ["wss://purplepag.es"],
enableOutboxModel: true,
cacheAdapter,
Expand Down
6 changes: 3 additions & 3 deletions apps/relays/app/components/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default function Footer(props: StackProps) {
{...props}
>
<Text color="chakra-subtle-text">
Relay data provided by
<Link variant="brand" href="https://nostr.watch">
nostr.watch
Made with ❤️ by{" "}
<Link variant="brand" href="https://snort.social/verbiricha">
verbiricha
</Link>
</Text>
<Text color="chakra-subtle-text">
Expand Down
13 changes: 6 additions & 7 deletions apps/relays/app/components/relay-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import { useRelays } from "@ngine/core";

import Link from "./link";
import RelayFavicon from "./relay-favicon";
import { humanize, encode } from "@lib/urls";
import { encodeRelayURL } from "../utils";

interface RelayLinkProps {
url: string;
}

export default function RelayLink({ url }: RelayLinkProps) {
const relays = useRelays();
const isInMyRelays = useMemo(
() => relays.map(humanize).includes(humanize(url)),
[relays, url],
);
const encoded = useMemo(() => `/relay/${encode(url)}`, [url]);
const domain = useMemo(() => humanize(url), [url]);
const isInMyRelays = useMemo(() => relays.includes(url), [relays, url]);
const encoded = useMemo(() => `/relay/${encodeRelayURL(url)}`, [url]);
const domain = useMemo(() => {
return url.replace("ws://", "").replace("wss://", "");
}, [url]);
return (
<Link key={url} href={encoded}>
<HStack spacing={2}>
Expand Down
5 changes: 3 additions & 2 deletions apps/relays/app/components/relay-metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import RelayFavicon from "./relay-favicon";
import RelaySummary from "./relay-summary";
import ToggleRelay from "./toggle-relay";
import { RelayMetadata } from "../hooks/useRelayMetadata";
import { humanize } from "@lib/urls";

export default function Metadata({
url,
Expand All @@ -16,7 +15,9 @@ export default function Metadata({
url: string;
metadata: RelayMetadata;
}) {
const domain = useMemo(() => humanize(url), [url]);
const domain = useMemo(() => {
return url.replace("ws://", "").replace("wss://", "");
}, [url]);

return (
<Stack>
Expand Down
74 changes: 39 additions & 35 deletions apps/relays/app/components/relays.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,58 @@
import { useRouter } from "next/navigation";
import { useState, useMemo } from "react";
import {
Alert,
AlertIcon,
Stack,
Button,
Text,
Icon,
Input,
InputGroup,
InputLeftElement,
InputRightElement,
Stat,
StatLabel,
StatNumber,
StatGroup,
} from "@chakra-ui/react";
import { useQuery } from "@tanstack/react-query";
import { useIntl, FormattedMessage } from "react-intl";
import { useRelays } from "@ngine/core";

import RelayLink from "./relay-link";
import RelayIcon from "./relay-icon";
import { humanize, encode } from "@lib/urls";
import useRelayStatus from "@hooks/useRelayStatus";
import { encodeRelayURL } from "../utils";

export default function Relays() {
const { formatMessage } = useIntl();
const router = useRouter();
const [relay, setRelay] = useState("");
const { events, online, offline } = useRelayStatus();
const myRelays = useRelays();
const relaySet = useMemo(() => new Set(myRelays.map(humanize)), [myRelays]);
const { data, isFetched, isError } = useQuery({
queryKey: ["relays"],
queryFn: () =>
fetch(`https://api.nostr.watch/v1/online`).then((r) => r.json()),
});

function relayScore(url: string) {
if (relaySet.has(humanize(url))) {
return 42;
if (myRelays.includes(url)) {
return 1;
}
return 0;
}

const relays = useMemo(() => {
const raw = [...online].sort((a, b) => relayScore(b) - relayScore(a));
const raw = data
? [...data].sort((a, b) => relayScore(b) - relayScore(a))
: [];
return raw
.filter((url) => url.toLowerCase().includes(relay.toLowerCase()))
.slice(0, 21);
}, [online, relay]);
}, [relay, data, myRelays]);

function goToRelay(url: string) {
router.push(`/relay/${encode(url)}`);
router.push(`/relay/${encodeRelayURL(url)}`);
}

return (
<Stack spacing={4} w="100%">
<StatGroup>
{/* @ts-ignore */}
<Stat align="center">
<StatNumber>{events.length}</StatNumber>
<StatLabel>Relays</StatLabel>
</Stat>

{/* @ts-ignore */}
<Stat align="center">
<StatNumber>{online.length}</StatNumber>
<StatLabel>Online</StatLabel>
</Stat>

{/* @ts-ignore */}
<Stat align="center">
<StatNumber>{offline.length}</StatNumber>
<StatLabel>Offline</StatLabel>
</Stat>
</StatGroup>
<InputGroup>
<InputLeftElement
pointerEvents="none"
Expand Down Expand Up @@ -108,9 +93,28 @@ export default function Relays() {
</InputRightElement>
</InputGroup>
<Stack>
{relays.map((url) => (
<RelayLink key={url} url={url} />
))}
{isError && (
<Alert>
<AlertIcon />
<FormattedMessage
id="cant-fetch-relays"
description="Error message shown when can't fetch online relays"
defaultMessage="Could not fetch online relays"
/>
</Alert>
)}
{isFetched &&
relays.map((url: string) => <RelayLink key={url} url={url} />)}
{isFetched && relays.length === 0 && (
<Text color="chakra-subtle-text">
<FormattedMessage
id="no-relays-found"
description="Message shown when no known relay matches the search pattern"
defaultMessage="No relays match the term `{ relay }`"
values={{ relay }}
/>
</Text>
)}
</Stack>
</Stack>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/relays/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function relayToTag(r: Relay): Tag {
}

export function tagToRelay(t: Tag): Relay {
const url = t[1];
const url = t[1].replace(/\/$/, "");

if (t[2] === "read") {
return { url, read: true, write: false };
Expand Down
41 changes: 0 additions & 41 deletions apps/relays/hooks/useRelayStatus.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/relays/lib/urls.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/relays/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"baseUrl": "./",
"paths": {
"@ui/*": ["ui/*"],
"@hooks/*": ["hooks/*"],
"@lib/*": ["lib/*"],
}
},
"include": [
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const relaysAtom = atom<Relay[]>((get) => {
relayList?.tags
.filter((t) => t[0] === "r")
.map((t) => {
const url = t[1];
const url = t[1].replace(/\/$/, "");
const read = t.length === 2 || t[2] === "read";
const write = t.length === 2 || t[2] === "write";
return { url, read, write };
Expand Down

0 comments on commit c609a47

Please sign in to comment.