commit 639e92f19de6e3efa3fa782637632d2e0f45d865
parent 9a4758330311548996185841bae076d35930d3ca
Author: Daniel D’Aquino <daniel@daquino.me>
Date: Mon, 1 Apr 2024 14:21:20 +0000
Move helper functions and types from PurpleCheckout into a common file
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 92 insertions(+), 83 deletions(-)
diff --git a/src/components/sections/PurpleCheckout.tsx b/src/components/sections/PurpleCheckout.tsx
@@ -27,6 +27,7 @@ import {
import { Info } from "lucide-react";
import { ErrorDialog } from "../ErrorDialog";
import { PurpleLayout } from "../PurpleLayout";
+import { AccountInfo, Profile, getProfile, getPurpleAccountInfo } from "@/utils/PurpleUtils";
export function PurpleCheckout() {
@@ -481,14 +482,6 @@ function StepHeader({ stepNumber, title, done, active }: { stepNumber: number, t
// MARK: - Types
-
-interface AccountInfo {
- pubkey: string,
- created_at: number, // Unix timestamp in seconds
- expiry: number | null, // Unix timestamp in seconds
- active: boolean,
-}
-
interface LNCheckout {
id: string,
verified_pubkey?: string,
@@ -515,78 +508,3 @@ interface ProductTemplate {
}
type ProductTemplates = Record<string, ProductTemplate>
-
-interface Profile {
- pubkey: string
- name: string
- picture: string
- about: string
-}
-
-// MARK: - Helper functions
-
-const getPurpleAccountInfo = async (pubkey: string): Promise<AccountInfo | null> => {
- const response = await fetch(process.env.NEXT_PUBLIC_PURPLE_API_BASE_URL + "/accounts/" + pubkey, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json'
- },
- })
- if (response.status === 404) {
- return null
- }
- const data: AccountInfo = await response.json()
- return data
-}
-
-const getProfile = async (pubkey: string): Promise<Profile | null> => {
- const profile_event: NostrEvent | null = await getProfileEvent(pubkey)
- if (!profile_event) {
- return null
- }
- try {
- const profile_data = JSON.parse(profile_event.content)
- const profile = {
- pubkey: profile_event.pubkey,
- name: profile_data.name,
- picture: profile_data.picture,
- about: profile_data.about,
- }
- return profile
- }
- catch (e) {
- return null
- }
-}
-
-const getProfileEvent = async (pubkey: string): Promise<NostrEvent | null> => {
- const relay = await Relay.connect('wss://relay.damus.io')
-
- const events: Array<NostrEvent> = []
- return new Promise((resolve, reject) => {
-
- // let's query for an event that exist
- const sub = relay.subscribe([
- {
- authors: [pubkey],
- kinds: [0],
- },
- ], {
- onevent(event: NostrEvent) {
- events.push(event)
- },
- oneose() {
- // Get the most recent event, based on `created_at` field
- if (events.length === 0) {
- // No events found
- resolve(null)
- }
- else {
- const most_recent_event = events.sort((a, b) => a.created_at - b.created_at)[0]
- resolve(most_recent_event)
- }
- relay.close()
- }
- })
- });
-}
diff --git a/src/utils/PurpleUtils.ts b/src/utils/PurpleUtils.ts
@@ -0,0 +1,91 @@
+// MARK: - Types
+
+import { NostrEvent, Relay } from "nostr-tools"
+
+interface AccountInfo {
+pubkey: string,
+created_at: number, // Unix timestamp in seconds
+expiry: number | null, // Unix timestamp in seconds
+subscriber_number: number,
+active: boolean,
+testflight_url: string | null,
+}
+
+interface Profile {
+pubkey: string
+name: string
+picture: string
+about: string
+}
+
+// MARK: - Helper functions
+
+const getPurpleAccountInfo = async (pubkey: string): Promise<AccountInfo | null> => {
+const response = await fetch(process.env.NEXT_PUBLIC_PURPLE_API_BASE_URL + "/accounts/" + pubkey, {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+})
+if (response.status === 404) {
+ return null
+}
+const data: AccountInfo = await response.json()
+return data
+}
+
+const getProfile = async (pubkey: string): Promise<Profile | null> => {
+const profile_event: NostrEvent | null = await getProfileEvent(pubkey)
+if (!profile_event) {
+ return null
+}
+try {
+ const profile_data = JSON.parse(profile_event.content)
+ const profile = {
+ pubkey: profile_event.pubkey,
+ name: profile_data.name,
+ picture: profile_data.picture,
+ about: profile_data.about,
+ }
+ return profile
+}
+catch (e) {
+ return null
+}
+}
+
+const getProfileEvent = async (pubkey: string): Promise<NostrEvent | null> => {
+const relay = await Relay.connect('wss://relay.damus.io')
+
+const events: Array<NostrEvent> = []
+return new Promise((resolve, reject) => {
+
+ // let's query for an event that exist
+ const sub = relay.subscribe([
+ {
+ authors: [pubkey],
+ kinds: [0],
+ },
+ ], {
+ onevent(event: NostrEvent) {
+ events.push(event)
+ },
+ oneose() {
+ // Get the most recent event, based on `created_at` field
+ if (events.length === 0) {
+ // No events found
+ resolve(null)
+ }
+ else {
+ const most_recent_event = events.sort((a, b) => a.created_at - b.created_at)[0]
+ resolve(most_recent_event)
+ }
+ relay.close()
+ }
+ })
+});
+}
+
+export { getPurpleAccountInfo, getProfile, getProfileEvent }
+export type { AccountInfo, Profile }
+