DM styles update
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { useMemo } from "react";
|
||||
import { useRequestBuilder, useUserProfile } from "../src";
|
||||
import { SnortContext, useRequestBuilder, useUserProfile } from "../src";
|
||||
|
||||
import { FlatNoteStore, NostrSystem, RequestBuilder, TaggedNostrEvent } from "@snort/system";
|
||||
import { NostrSystem, NoteCollection, RequestBuilder, TaggedNostrEvent } from "@snort/system";
|
||||
|
||||
const System = new NostrSystem({});
|
||||
|
||||
@ -9,7 +9,7 @@ const System = new NostrSystem({});
|
||||
["wss://relay.snort.social", "wss://nos.lol"].forEach(r => System.ConnectToRelay(r, { read: true, write: false }));
|
||||
|
||||
export function Note({ ev }: { ev: TaggedNostrEvent }) {
|
||||
const profile = useUserProfile(System, ev.pubkey);
|
||||
const profile = useUserProfile(ev.pubkey);
|
||||
|
||||
return (
|
||||
<div>
|
||||
@ -27,7 +27,7 @@ export function UserPosts(props: { pubkey: string }) {
|
||||
return rb;
|
||||
}, [props.pubkey]);
|
||||
|
||||
const data = useRequestBuilder<FlatNoteStore>(System, FlatNoteStore, sub);
|
||||
const data = useRequestBuilder(NoteCollection, sub);
|
||||
return (
|
||||
<>
|
||||
{data.data.map(a => (
|
||||
@ -38,5 +38,7 @@ export function UserPosts(props: { pubkey: string }) {
|
||||
}
|
||||
|
||||
export function MyApp() {
|
||||
return <UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />;
|
||||
return <SnortContext.Provider value={System}>
|
||||
<UserPosts pubkey="63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed" />
|
||||
</SnortContext.Provider>;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@snort/system-react",
|
||||
"version": "1.0.11",
|
||||
"version": "1.0.12",
|
||||
"description": "React hooks for @snort/system",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -15,10 +15,12 @@
|
||||
"dist"
|
||||
],
|
||||
"dependencies": {
|
||||
"@snort/shared": "^1.0.4",
|
||||
"@snort/system": "^1.0.16",
|
||||
"react": "^18.2.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@snort/shared": "^1.0.4",
|
||||
"@snort/system": "^1.0.17"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.14",
|
||||
"typescript": "^5.1.6"
|
||||
|
4
packages/system-react/src/context.tsx
Normal file
4
packages/system-react/src/context.tsx
Normal file
@ -0,0 +1,4 @@
|
||||
import { createContext } from "react";
|
||||
import { NostrSystem, SystemInterface } from "@snort/system";
|
||||
|
||||
export const SnortContext = createContext<SystemInterface>(new NostrSystem({}));
|
@ -1,3 +1,5 @@
|
||||
export * from "./useRequestBuilder";
|
||||
export * from "./useSystemState";
|
||||
export * from "./useUserProfile";
|
||||
export * from "./context";
|
||||
export * from "./useUserSearch";
|
@ -1,15 +1,16 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { RequestBuilder, EmptySnapshot, NoteStore, StoreSnapshot, SystemInterface } from "@snort/system";
|
||||
import { useContext, useSyncExternalStore } from "react";
|
||||
import { RequestBuilder, EmptySnapshot, NoteStore, StoreSnapshot } from "@snort/system";
|
||||
import { unwrap } from "@snort/shared";
|
||||
import { SnortContext } from "./context";
|
||||
|
||||
/**
|
||||
* Send a query to the relays and wait for data
|
||||
*/
|
||||
const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TStore["getSnapshotData"]>>(
|
||||
system: SystemInterface,
|
||||
type: { new (): TStore },
|
||||
type: { new(): TStore },
|
||||
rb: RequestBuilder | null,
|
||||
) => {
|
||||
const system = useContext(SnortContext);
|
||||
const subscribe = (onChanged: () => void) => {
|
||||
if (rb) {
|
||||
const q = system.Query<TStore>(type, rb);
|
||||
|
@ -1,10 +1,12 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { HexKey, MetadataCache, NostrSystem } from "@snort/system";
|
||||
import { useContext, useSyncExternalStore } from "react";
|
||||
import { HexKey, MetadataCache } from "@snort/system";
|
||||
import { SnortContext } from "./context";
|
||||
|
||||
/**
|
||||
* Gets a profile from cache or requests it from the relays
|
||||
*/
|
||||
export function useUserProfile(system: NostrSystem, pubKey?: HexKey): MetadataCache | undefined {
|
||||
export function useUserProfile(pubKey?: HexKey): MetadataCache | undefined {
|
||||
const system = useContext(SnortContext);
|
||||
return useSyncExternalStore<MetadataCache | undefined>(
|
||||
h => {
|
||||
if (pubKey) {
|
||||
|
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