-
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.
Merge pull request #21 from sepehr-safari/implement-ndk
Replace nostr-hooks with NDK
- Loading branch information
Showing
20 changed files
with
553 additions
and
291 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Ignore artifacts: | ||
build | ||
coverage |
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,14 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": false, | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"endOfLine": "lf", | ||
"printWidth": 100, | ||
"trailingComma": "es5", | ||
"singleAttributePerLine": false, | ||
"jsxSingleQuote": false, | ||
"arrowParens": "always", | ||
"bracketSameLine": false, | ||
"bracketSpacing": true | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<!DOCTYPE html> | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
|
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ export default { | |
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,43 +1,62 @@ | ||
import { useSubscribe } from "nostr-hooks"; | ||
import React from "react"; | ||
import { RELAYS, appCustomDataTag, appCustomDataValues } from "../constants"; | ||
import { NDKEvent, NDKFilter } from "@nostr-dev-kit/ndk"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
|
||
import { appCustomDataTag, appCustomDataValues } from "../constants"; | ||
import useStore from "../state/store"; | ||
import { Item } from "../types"; | ||
|
||
export function useItems(walletPubkey?: string) { | ||
const { events, invalidate, eose } = useSubscribe({ | ||
relays: RELAYS, | ||
filters: walletPubkey | ||
? [ | ||
{ | ||
authors: [walletPubkey], | ||
kinds: [30078], | ||
limit: 100, | ||
// custom hashtag filter support is not supported by all relays? | ||
// [`#${appCustomDataTag}`]: [appCustomDataValues.item], | ||
}, | ||
] | ||
: [], | ||
options: { | ||
enabled: !!walletPubkey, | ||
}, | ||
}); | ||
|
||
const items = React.useMemo( | ||
export function useItems() { | ||
const [events, setEvents] = useState<NDKEvent[]>([]); | ||
const [eose, setEose] = useState<boolean>(false); | ||
|
||
const ndk = useStore((store) => store.ndk); | ||
const walletPubkey = useStore((store) => store.provider)?.publicKey; | ||
|
||
useEffect(() => { | ||
if (!walletPubkey || !ndk) { | ||
return; | ||
} | ||
|
||
const filters: NDKFilter[] = [ | ||
{ | ||
authors: [walletPubkey], | ||
kinds: [30078], | ||
limit: 100, | ||
// custom hashtag filter support is not supported by all relays? | ||
// [`#${appCustomDataTag}`]: [appCustomDataValues.item], | ||
}, | ||
]; | ||
|
||
setEose(false); | ||
|
||
const subscription = ndk.subscribe(filters); | ||
|
||
subscription.on("event", (event) => { | ||
setEvents((prevEvents) => | ||
prevEvents.some((existingEvent) => existingEvent.id === event.id) | ||
? prevEvents | ||
: [...prevEvents, event] | ||
); | ||
}); | ||
|
||
subscription.on("eose", () => { | ||
setEose(true); | ||
}); | ||
}, [ndk, walletPubkey, setEvents]); | ||
|
||
const items = useMemo( | ||
() => | ||
// TODO: remove filter when above relay filter works | ||
events | ||
.filter((event) => | ||
event.tags.some( | ||
(t) => | ||
t[0] === appCustomDataTag && t[1] === appCustomDataValues.item | ||
) | ||
event.tags.some((t) => t[0] === appCustomDataTag && t[1] === appCustomDataValues.item) | ||
) | ||
.map((event) => JSON.parse(event.content) as Item), | ||
[events] | ||
); | ||
|
||
const isLoading = !eose && !events.length; | ||
const isLoading = !eose && events.length == 0; | ||
// console.log(items, events); | ||
|
||
return { items, invalidate, isLoading }; | ||
return { items, isLoading }; | ||
} |
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
Oops, something went wrong.