-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
324 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useNClient } from "../state/client"; | ||
import { decodeBech32 } from "@nostr-ts/common"; | ||
import { Box, Text } from "@chakra-ui/react"; | ||
import { Event } from "./event"; | ||
|
||
interface OnDemandEventProps { | ||
note: string; | ||
index: number; | ||
} | ||
|
||
export function OnDemandEvent({ note, index }: OnDemandEventProps) { | ||
const MAX_RETRIES = 20; | ||
const RETRY_INTERVAL = 1000; | ||
|
||
const eventId = useRef(""); | ||
const relayUrls = useRef<string[]>([]); | ||
|
||
const [connected] = useNClient((state) => [state.connected]); | ||
const [eventData] = useNClient((state) => [ | ||
state.events[note] ? state.events[note][0] : null, | ||
]); | ||
const [hasTimeout, setHasTimeout] = useState(false); | ||
|
||
const fetchEvent = async (retryCount = 0) => { | ||
if (retryCount > MAX_RETRIES) { | ||
setHasTimeout(true); | ||
return; | ||
} | ||
|
||
const event = await useNClient.getState().getEvent(eventId.current, { | ||
view: note, | ||
retryCount, | ||
relayUrls: relayUrls.current, | ||
}); | ||
|
||
if (!event) { | ||
setTimeout(() => fetchEvent(retryCount + 1), RETRY_INTERVAL); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
// Decode note | ||
try { | ||
const decoded = decodeBech32(note); | ||
for (const item of decoded.tlvItems) { | ||
if (item.type === 0) { | ||
eventId.current = item.value as string; | ||
} else if (item.type === 1) { | ||
relayUrls.current.push(item.value as string); | ||
} | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return; | ||
} | ||
|
||
if (note && connected && (!index || index <= 2)) { | ||
fetchEvent(); | ||
} | ||
|
||
// return () => { | ||
// useNClient.getState().unsubscribeByToken(note); | ||
// }; | ||
}, [note, index, connected]); | ||
|
||
return ( | ||
<Box p={1} m={1}> | ||
{eventData ? ( | ||
<Event data={eventData} level={0} /> | ||
) : hasTimeout ? ( | ||
<Text fontSize="sm" color="gray.500"> | ||
Couldn't find {note}. | ||
</Text> | ||
) : ( | ||
<Text fontSize="sm" color="gray.500"> | ||
Loading ... {note} | ||
</Text> | ||
)} | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Link } from "@chakra-ui/react"; | ||
import { NavLink } from "react-router-dom"; | ||
import { useNClient } from "../state/client"; | ||
import { UserRecord, decodeBech32 } from "@nostr-ts/common"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { excerpt } from "../lib/excerpt"; | ||
|
||
interface OnDemandUsernameProps { | ||
npub?: string; | ||
} | ||
|
||
export function OnDemandUsername({ npub }: OnDemandUsernameProps) { | ||
const [status] = useNClient((state) => [state.status]); | ||
|
||
const pubkey = useRef<string | null>(null); | ||
const [username, setUsername] = useState<string>(""); | ||
const [profileLink, setProfileLink] = useState<string>(""); | ||
const [isLoadingUser, setIsLoadingUser] = useState<boolean>(false); | ||
|
||
const usernameFromUser = (record: UserRecord) => { | ||
const data = record.user.data; | ||
return data && data.display_name ? `@${data.display_name}` : ""; | ||
}; | ||
|
||
const fetchUser = async (pk: string, retryCount: number = 0) => { | ||
if (retryCount > 20) { | ||
setIsLoadingUser(false); | ||
return; | ||
} | ||
|
||
const user = await useNClient.getState().getUser(pk); | ||
|
||
if (user) { | ||
setUsername(usernameFromUser(user)); | ||
setIsLoadingUser(false); | ||
} else { | ||
if (retryCount === 2) { | ||
await useNClient.getState().requestInformation( | ||
{ | ||
idsOrKeys: [pk], | ||
source: "users", | ||
}, | ||
{ timeoutIn: 10000 } | ||
); | ||
} | ||
|
||
setTimeout(() => fetchUser(pk, retryCount + 1), 1000); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!npub || !["online", "offline"].includes(status)) return; | ||
|
||
try { | ||
const decoded = decodeBech32(npub); | ||
const pkItem = decoded.tlvItems.find((item) => item.type === 0); | ||
|
||
if (pkItem) { | ||
pubkey.current = pkItem.value as string; | ||
setProfileLink(`/p/${npub}`); | ||
setIsLoadingUser(true); | ||
fetchUser(pubkey.current); | ||
} | ||
} catch (e) { | ||
console.error("Error decoding bech32:", e); | ||
} | ||
}, [status, npub]); | ||
|
||
return ( | ||
<> | ||
{npub ? ( | ||
<Link as={NavLink} to={profileLink} color={"gray.500"}> | ||
{username === "" || isLoadingUser ? excerpt(npub, 10) : username} | ||
</Link> | ||
) : ( | ||
<>...</> | ||
)} | ||
</> | ||
); | ||
} |
Oops, something went wrong.