DM styles update
This commit is contained in:
37
packages/system-react/src/useUserSearch.tsx
Normal file
37
packages/system-react/src/useUserSearch.tsx
Normal file
@ -0,0 +1,37 @@
|
||||
import { useContext } from "react";
|
||||
import { NostrPrefix, UserProfileCache, tryParseNostrLink } from "@snort/system";
|
||||
import { fetchNip05Pubkey } from "@snort/shared";
|
||||
import { SnortContext } from "./context";
|
||||
|
||||
export function useUserSearch() {
|
||||
const system = useContext(SnortContext);
|
||||
const cache = system.ProfileLoader.Cache as UserProfileCache;
|
||||
|
||||
async function search(input: string): Promise<Array<string>> {
|
||||
// try exact match first
|
||||
if (input.length === 64 && [...input].every(c => !isNaN(parseInt(c, 16)))) {
|
||||
return [input];
|
||||
}
|
||||
|
||||
if (input.startsWith(NostrPrefix.PublicKey) || input.startsWith(NostrPrefix.Profile)) {
|
||||
const link = tryParseNostrLink(input);
|
||||
if (link) {
|
||||
return [link.id]
|
||||
}
|
||||
}
|
||||
|
||||
if (input.includes("@")) {
|
||||
const [name, domain] = input.split("@");
|
||||
const pk = await fetchNip05Pubkey(name, domain);
|
||||
if (pk) {
|
||||
return [pk];
|
||||
}
|
||||
}
|
||||
|
||||
// search cache
|
||||
const cacheResults = await cache.search(input);
|
||||
return cacheResults.map(v => v.pubkey);
|
||||
}
|
||||
|
||||
return search;
|
||||
}
|
Reference in New Issue
Block a user