From 7c64a42f27eecd414d4716571a0eb85aa0a47627 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 20 Dec 2023 02:13:49 +0100 Subject: [PATCH 1/4] chore: ensured budget is set after payment of Lsat has been made --- src/components/AddContentModal/index.tsx | 2 +- .../App/Helper/AskQuestion/index.tsx | 15 +++-- .../App/Helper/SentimentAnalysis/index.tsx | 2 +- src/components/App/Helper/TeachMe/index.tsx | 22 +++++--- .../App/SecondarySidebar/Sentiment/index.tsx | 2 +- src/components/App/index.tsx | 2 +- src/components/DataRetriever/index.tsx | 6 +- src/network/fetchGraphData/index.ts | 55 +++++++++++-------- src/stores/useDataStore/index.ts | 6 +- src/utils/payLsat/index.ts | 7 ++- 10 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/components/AddContentModal/index.tsx b/src/components/AddContentModal/index.tsx index 2819e5c2a..607b477f6 100644 --- a/src/components/AddContentModal/index.tsx +++ b/src/components/AddContentModal/index.tsx @@ -127,7 +127,7 @@ const handleSubmitForm = async ( // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (err: any) { if (err.status === 402) { - await payLsat() + await payLsat(setBudget) await updateBudget(setBudget) diff --git a/src/components/App/Helper/AskQuestion/index.tsx b/src/components/App/Helper/AskQuestion/index.tsx index ffd5915ec..ca1d223b3 100644 --- a/src/components/App/Helper/AskQuestion/index.tsx +++ b/src/components/App/Helper/AskQuestion/index.tsx @@ -100,12 +100,15 @@ export const AskQuestion = () => { // @ts-ignore await sphinx.enable() - await postAskQuestion({ - expertise_level: selectedValue, - question_text: question, - search_term: searchTerm, - transcripts, - }) + await postAskQuestion( + { + expertise_level: selectedValue, + question_text: question, + search_term: searchTerm, + transcripts, + }, + setBudget, + ) await updateBudget(setBudget) diff --git a/src/components/App/Helper/SentimentAnalysis/index.tsx b/src/components/App/Helper/SentimentAnalysis/index.tsx index 64e3b5554..742e7508c 100644 --- a/src/components/App/Helper/SentimentAnalysis/index.tsx +++ b/src/components/App/Helper/SentimentAnalysis/index.tsx @@ -50,7 +50,7 @@ export const SentimentAnalysis = memo(() => { sphinx.enable(), ) - getSentimentData({ topic: search, cutoff_date: String(value.unix()) }) + getSentimentData(setBudget, { topic: search, cutoff_date: String(value.unix()) }) .then(async (r) => { setSentimentData( r?.data diff --git a/src/components/App/Helper/TeachMe/index.tsx b/src/components/App/Helper/TeachMe/index.tsx index 6540dc3ab..3753cecd5 100644 --- a/src/components/App/Helper/TeachMe/index.tsx +++ b/src/components/App/Helper/TeachMe/index.tsx @@ -102,10 +102,13 @@ export const TeachMe = () => { // @ts-ignore await sphinx.enable() - await postTeachMe({ - term: searchTerm, - transcripts, - }) + await postTeachMe( + { + term: searchTerm, + transcripts, + }, + setBudget, + ) await updateBudget(setBudget) @@ -113,10 +116,13 @@ export const TeachMe = () => { type: 'success', }) - await postInstagraph({ - term: searchTerm, - transcripts, - }) + await postInstagraph( + { + term: searchTerm, + transcripts, + }, + setBudget, + ) await updateBudget(setBudget) diff --git a/src/components/App/SecondarySidebar/Sentiment/index.tsx b/src/components/App/SecondarySidebar/Sentiment/index.tsx index 758eae142..8083bdb1f 100644 --- a/src/components/App/SecondarySidebar/Sentiment/index.tsx +++ b/src/components/App/SecondarySidebar/Sentiment/index.tsx @@ -36,7 +36,7 @@ export const Sentiment = () => { sphinx.enable(), ) - getSentimentData() + getSentimentData(setBudget) .then(async (r) => { setSentimentData( r?.data diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index 5d7162c75..1cbc5c574 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -112,7 +112,7 @@ export const App = () => { setSphinxModalOpen(false) } - await fetchData(searchTerm) + await fetchData(setBudget, searchTerm) setSidebarOpen(true) }, [fetchData, searchTerm, setSphinxModalOpen, setSidebarOpen, setBudget]) diff --git a/src/components/DataRetriever/index.tsx b/src/components/DataRetriever/index.tsx index 0a1d83dd2..800f83e31 100644 --- a/src/components/DataRetriever/index.tsx +++ b/src/components/DataRetriever/index.tsx @@ -2,6 +2,7 @@ import invariant from 'invariant' import { PropsWithChildren, useCallback, useEffect, useState } from 'react' import { Vector3 } from 'three' import { useDataStore, useSelectedNode } from '~/stores/useDataStore' +import { useUserStore } from '~/stores/useUserStore' import { NodeExtended } from '~/types' import { Splash } from '../App/Splash' import { PATHWAY_RANGE } from './constants' @@ -10,13 +11,14 @@ type Props = PropsWithChildren export const DataRetriever = ({ children }: Props) => { const fetchData = useDataStore((s) => s.fetchData) + const [setBudget] = useUserStore((s) => [s.setBudget]) const [loading, setLoading] = useState(false) useEffect(() => { setLoading(true) - fetchData() - }, [fetchData]) + fetchData(setBudget) + }, [fetchData, setBudget]) if (loading) { return diff --git a/src/network/fetchGraphData/index.ts b/src/network/fetchGraphData/index.ts index e62d0d44c..ca7d26f89 100644 --- a/src/network/fetchGraphData/index.ts +++ b/src/network/fetchGraphData/index.ts @@ -59,15 +59,19 @@ type TopicMap = Record const shouldIncludeTopics = true const maxScale = 26 -export const fetchGraphData = async (search: string, graphStyle: 'split' | 'force' | 'sphere' | 'earth') => { +export const fetchGraphData = async ( + search: string, + graphStyle: 'split' | 'force' | 'sphere' | 'earth', + setBudget: (value: number | null) => void, +) => { try { - return getGraphData(search, graphStyle) + return getGraphData(search, graphStyle, setBudget) } catch (e) { return defaultData } } -const fetchNodes = async (search: string): Promise => { +const fetchNodes = async (search: string, setBudget: (value: number | null) => void): Promise => { if (!search) { try { const response = await api.get(`/prediction/content/latest`) @@ -97,9 +101,9 @@ const fetchNodes = async (search: string): Promise => { // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error.status === 402) { - await payLsat() + await payLsat(setBudget) - return fetchNodes(search) + return fetchNodes(search, setBudget) } throw error @@ -122,10 +126,13 @@ export const getTrends = async () => { * } */ -export const getSentimentData = async (args?: { - topic: string - cutoff_date: string -}): Promise => { +export const getSentimentData = async ( + setBudget: (value: number | null) => void, + args?: { + topic: string + cutoff_date: string + }, +): Promise => { const search = args && new URLSearchParams(args) const endpoint = search ? `/sentiments?${search.toString()}` : '/sentiments' @@ -149,16 +156,16 @@ export const getSentimentData = async (args?: { // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error.status === 402) { - await payLsat() + await payLsat(setBudget) - return getSentimentData(args) + return getSentimentData(setBudget, args) } throw error } } -export const postInstagraph = async (data: TeachData): Promise => { +export const postInstagraph = async (data: TeachData, setBudget: (value: number | null) => void): Promise => { const lsatToken = await getLSat() try { @@ -167,9 +174,9 @@ export const postInstagraph = async (data: TeachData): Promise => { // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error.status === 402) { - await payLsat() + await payLsat(setBudget) - await postInstagraph(data) + await postInstagraph(data, setBudget) return } @@ -178,7 +185,7 @@ export const postInstagraph = async (data: TeachData): Promise => { } } -export const postTeachMe = async (data: TeachData): Promise => { +export const postTeachMe = async (data: TeachData, setBudget: (value: number | null) => void): Promise => { const lsatToken = await getLSat() try { @@ -187,9 +194,9 @@ export const postTeachMe = async (data: TeachData): Promise => { // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error.status === 402) { - await payLsat() + await payLsat(setBudget) - await postTeachMe(data) + await postTeachMe(data, setBudget) return } @@ -198,7 +205,7 @@ export const postTeachMe = async (data: TeachData): Promise => { } } -export const postAskQuestion = async (data: QuestionData): Promise => { +export const postAskQuestion = async (data: QuestionData, setBudget: (value: number | null) => void): Promise => { const lsatToken = await getLSat() try { @@ -207,9 +214,9 @@ export const postAskQuestion = async (data: QuestionData): Promise => { // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error.status === 402) { - await payLsat() + await payLsat(setBudget) - await postAskQuestion(data) + await postAskQuestion(data, setBudget) return } @@ -326,9 +333,13 @@ const generateGuestsMap = ( return updatedGuestMap // Return the new variable } -export const getGraphData = async (searchterm: string, graphStyle: 'split' | 'force' | 'sphere' | 'earth') => { +export const getGraphData = async ( + searchterm: string, + graphStyle: 'split' | 'force' | 'sphere' | 'earth', + setBudget: (value: number | null) => void, +) => { try { - const dataInit = await fetchNodes(searchterm) + const dataInit = await fetchNodes(searchterm, setBudget) return formatFetchNodes(dataInit, searchterm, graphStyle) } catch (e) { diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index c09fde312..4656a570f 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -39,7 +39,7 @@ type DataStore = { setScrollEventsDisabled: (scrollEventsDisabled: boolean) => void setCategoryFilter: (categoryFilter: NodeType | null) => void setDisableCameraRotation: (rotation: boolean) => void - fetchData: (search?: string | null) => void + fetchData: (setBudget: (value: number | null) => void, search?: string | null) => void setData: (data: GraphData) => void setGraphStyle: (graphStyle: GraphStyle) => void setGraphRadius: (graphRadius?: number | null) => void @@ -111,14 +111,14 @@ const defaultData: Omit< export const useDataStore = create((set, get) => ({ ...defaultData, - fetchData: async (search) => { + fetchData: async (setBudget, search) => { if (get().isFetching) { return } set({ isFetching: true, sphinxModalIsOpen: true }) - const data = await fetchGraphData(search || '', get().graphStyle) + const data = await fetchGraphData(search || '', get().graphStyle, setBudget) if (search) { await saveSearchTerm() diff --git a/src/utils/payLsat/index.ts b/src/utils/payLsat/index.ts index 75da954f9..df6fc438f 100644 --- a/src/utils/payLsat/index.ts +++ b/src/utils/payLsat/index.ts @@ -2,9 +2,10 @@ import { Lsat } from 'lsat-js' import * as sphinx from 'sphinx-bridge' import { requestProvider } from 'webln' import { buyLsat } from '~/network/buyLsat' +import { updateBudget } from '../setBudget' // eslint-disable-next-line @typescript-eslint/no-explicit-any -export async function payLsat(): Promise { +export async function payLsat(setBudget: (value: number | null) => void): Promise { let lsat: Lsat // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -62,6 +63,8 @@ export async function payLsat(): Promise { ) } + await updateBudget(setBudget) + return } } @@ -90,4 +93,6 @@ export async function payLsat(): Promise { }), ) } + + await updateBudget(setBudget) } From f0f5d1d4d6122d9c8ba706b2dee7bb80a38dd33c Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 20 Dec 2023 02:43:36 +0100 Subject: [PATCH 2/4] fix: use budget value to get display cyrrent budget on screen --- src/utils/payLsat/index.ts | 49 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/utils/payLsat/index.ts b/src/utils/payLsat/index.ts index df6fc438f..3c6377e40 100644 --- a/src/utils/payLsat/index.ts +++ b/src/utils/payLsat/index.ts @@ -2,7 +2,6 @@ import { Lsat } from 'lsat-js' import * as sphinx from 'sphinx-bridge' import { requestProvider } from 'webln' import { buyLsat } from '~/network/buyLsat' -import { updateBudget } from '../setBudget' // eslint-disable-next-line @typescript-eslint/no-explicit-any export async function payLsat(setBudget: (value: number | null) => void): Promise { @@ -53,18 +52,20 @@ export async function payLsat(setBudget: (value: number | null) => void): Promis // @ts-ignore const LSATRes = await sphinx.saveLsat(lsat.invoice, lsat.baseMacaroon, window.location.host) - localStorage.setItem( - 'lsat', - JSON.stringify({ - macaroon: lsat.baseMacaroon, - identifier: lsat.id, - preimage: LSATRes.lsat.split(':')[1], - }), - ) + if (LSATRes?.lsat) { + localStorage.setItem( + 'lsat', + JSON.stringify({ + macaroon: lsat.baseMacaroon, + identifier: lsat.id, + preimage: LSATRes.lsat.split(':')[1], + }), + ) + + await setBudget(budgetAmount) + } } - await updateBudget(setBudget) - return } } @@ -74,8 +75,10 @@ export async function payLsat(setBudget: (value: number | null) => void): Promis const webln = await requestProvider() + const budgetAmount = 50 + try { - await buyLsat(50) + await buyLsat(budgetAmount) // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { @@ -84,15 +87,17 @@ export async function payLsat(setBudget: (value: number | null) => void): Promis // pay lsat invoice const preimage = await webln.sendPayment(lsat.invoice) - localStorage.setItem( - 'lsat', - JSON.stringify({ - macaroon: lsat.baseMacaroon, - identifier: lsat.id, - preimage: preimage.preimage, - }), - ) - } + if (preimage?.preimage) { + localStorage.setItem( + 'lsat', + JSON.stringify({ + macaroon: lsat.baseMacaroon, + identifier: lsat.id, + preimage: preimage.preimage, + }), + ) + } - await updateBudget(setBudget) + await setBudget(budgetAmount) + } } From 9d2a9dcf4f13d6feb43f817c4f0dad36b93320f6 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 20 Dec 2023 18:40:54 +0100 Subject: [PATCH 3/4] fix: ensured we update budget after every search --- src/components/App/index.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index 1cbc5c574..3abb3ce09 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -105,8 +105,6 @@ export const App = () => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore await sphinx.enable() - - await updateBudget(setBudget) } setSphinxModalOpen(false) @@ -114,6 +112,10 @@ export const App = () => { await fetchData(setBudget, searchTerm) setSidebarOpen(true) + + if (searchTerm) { + await updateBudget(setBudget) + } }, [fetchData, searchTerm, setSphinxModalOpen, setSidebarOpen, setBudget]) useEffect(() => { From a5e6e5fba5245322e08a67574bd4b2fec89d98bb Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 20 Dec 2023 19:31:43 +0100 Subject: [PATCH 4/4] fix: fix error, where I am not passing setBudget into the fetchData function --- src/components/App/SideBar/Latest/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/App/SideBar/Latest/index.tsx b/src/components/App/SideBar/Latest/index.tsx index e07ddbc43..6d0b811b9 100644 --- a/src/components/App/SideBar/Latest/index.tsx +++ b/src/components/App/SideBar/Latest/index.tsx @@ -15,7 +15,7 @@ type Props = { // eslint-disable-next-line no-underscore-dangle const _View = ({ isSearchResult }: Props) => { - const [nodeCount, setNodeCount] = useUserStore((s) => [s.nodeCount, s.setNodeCount]) + const [nodeCount, setNodeCount, setBudget] = useUserStore((s) => [s.nodeCount, s.setNodeCount, s.setBudget]) const [fetchData] = [useDataStore((s) => s.fetchData)] const getLatest = async () => { @@ -23,7 +23,7 @@ const _View = ({ isSearchResult }: Props) => { return } - await fetchData() + await fetchData(setBudget) setNodeCount('CLEAR') }