Skip to content

Commit

Permalink
Merge pull request #709 from stakwork/fix/set-budget-after-payment
Browse files Browse the repository at this point in the history
Fix/set budget after payment
  • Loading branch information
Rassl authored Dec 21, 2023
2 parents c392bfa + a5e6e5f commit 504f861
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/components/AddContentModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 9 additions & 6 deletions src/components/App/Helper/AskQuestion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/components/App/Helper/SentimentAnalysis/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions src/components/App/Helper/TeachMe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,27 @@ export const TeachMe = () => {
// @ts-ignore
await sphinx.enable()

await postTeachMe({
term: searchTerm,
transcripts,
})
await postTeachMe(
{
term: searchTerm,
transcripts,
},
setBudget,
)

await updateBudget(setBudget)

toast(<ToastMessage message="We started preparing tutorial for you" />, {
type: 'success',
})

await postInstagraph({
term: searchTerm,
transcripts,
})
await postInstagraph(
{
term: searchTerm,
transcripts,
},
setBudget,
)

await updateBudget(setBudget)

Expand Down
2 changes: 1 addition & 1 deletion src/components/App/SecondarySidebar/Sentiment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const Sentiment = () => {
sphinx.enable(),
)

getSentimentData()
getSentimentData(setBudget)
.then(async (r) => {
setSentimentData(
r?.data
Expand Down
4 changes: 2 additions & 2 deletions src/components/App/SideBar/Latest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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 () => {
if (nodeCount < 1) {
return
}

await fetchData()
await fetchData(setBudget)
setNodeCount('CLEAR')
}

Expand Down
8 changes: 5 additions & 3 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ export const App = () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await sphinx.enable()

await updateBudget(setBudget)
}

setSphinxModalOpen(false)
}

await fetchData(searchTerm)
await fetchData(setBudget, searchTerm)
setSidebarOpen(true)

if (searchTerm) {
await updateBudget(setBudget)
}
}, [fetchData, searchTerm, setSphinxModalOpen, setSidebarOpen, setBudget])

useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/DataRetriever/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 <Splash handleLoading={setLoading} />
Expand Down
55 changes: 33 additions & 22 deletions src/network/fetchGraphData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ type TopicMap = Record<string, TopicMapItem>
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<FetchDataResponse> => {
const fetchNodes = async (search: string, setBudget: (value: number | null) => void): Promise<FetchDataResponse> => {
if (!search) {
try {
const response = await api.get<FetchDataResponse>(`/prediction/content/latest`)
Expand Down Expand Up @@ -97,9 +101,9 @@ const fetchNodes = async (search: string): Promise<FetchDataResponse> => {
// 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
Expand All @@ -122,10 +126,13 @@ export const getTrends = async () => {
* }
*/

export const getSentimentData = async (args?: {
topic: string
cutoff_date: string
}): Promise<FetchSentimentResponse> => {
export const getSentimentData = async (
setBudget: (value: number | null) => void,
args?: {
topic: string
cutoff_date: string
},
): Promise<FetchSentimentResponse> => {
const search = args && new URLSearchParams(args)

const endpoint = search ? `/sentiments?${search.toString()}` : '/sentiments'
Expand All @@ -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<void> => {
export const postInstagraph = async (data: TeachData, setBudget: (value: number | null) => void): Promise<void> => {
const lsatToken = await getLSat()

try {
Expand All @@ -167,9 +174,9 @@ export const postInstagraph = async (data: TeachData): Promise<void> => {
// 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
}
Expand All @@ -178,7 +185,7 @@ export const postInstagraph = async (data: TeachData): Promise<void> => {
}
}

export const postTeachMe = async (data: TeachData): Promise<void> => {
export const postTeachMe = async (data: TeachData, setBudget: (value: number | null) => void): Promise<void> => {
const lsatToken = await getLSat()

try {
Expand All @@ -187,9 +194,9 @@ export const postTeachMe = async (data: TeachData): Promise<void> => {
// 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
}
Expand All @@ -198,7 +205,7 @@ export const postTeachMe = async (data: TeachData): Promise<void> => {
}
}

export const postAskQuestion = async (data: QuestionData): Promise<void> => {
export const postAskQuestion = async (data: QuestionData, setBudget: (value: number | null) => void): Promise<void> => {
const lsatToken = await getLSat()

try {
Expand All @@ -207,9 +214,9 @@ export const postAskQuestion = async (data: QuestionData): Promise<void> => {
// 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
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/stores/useDataStore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,14 +111,14 @@ const defaultData: Omit<

export const useDataStore = create<DataStore>((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()
Expand Down
46 changes: 28 additions & 18 deletions src/utils/payLsat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { requestProvider } from 'webln'
import { buyLsat } from '~/network/buyLsat'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function payLsat(): Promise<void> {
export async function payLsat(setBudget: (value: number | null) => void): Promise<void> {
let lsat: Lsat

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -52,14 +52,18 @@ export async function payLsat(): Promise<void> {
// @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)
}
}

return
Expand All @@ -71,8 +75,10 @@ export async function payLsat(): Promise<void> {

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) {
Expand All @@ -81,13 +87,17 @@ export async function payLsat(): Promise<void> {
// 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 setBudget(budgetAmount)
}
}

0 comments on commit 504f861

Please sign in to comment.