Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/boost/boost-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function BoostButton({ refId, pubkey, boostCount = 0, className }: BoostB

const isAdmin = useUserStore((s) => s.isAdmin)
const setBudget = useUserStore((s) => s.setBudget)
const refreshBalance = useUserStore((s) => s.refreshBalance)

const handleBoost = useCallback(async () => {
if (boosting) return
Expand Down Expand Up @@ -56,13 +57,14 @@ export function BoostButton({ refId, pubkey, boostCount = 0, className }: BoostB
setCount((c) => c + DEFAULT_BOOST_AMOUNT)
setFlash(true)
setTimeout(() => setFlash(false), 600)
refreshBalance()
} catch (err) {
console.error("Boost failed:", err)
setError("Boost failed. Please try again.")
} finally {
setBoosting(false)
}
}, [refId, pubkey, boosting, isAdmin, setBudget])
}, [refId, pubkey, boosting, isAdmin, setBudget, refreshBalance])

return (
<div className="flex flex-col items-start gap-1">
Expand Down
3 changes: 3 additions & 0 deletions src/components/layout/node-preview-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ScrollArea } from "@/components/ui/scroll-area"
import { api } from "@/lib/api"
import { useMocks, MOCK_FULL_NODES } from "@/lib/mock-data"
import { usePlayerStore } from "@/stores/player-store"
import { useUserStore } from "@/stores/user-store"
import type { GraphNode } from "@/lib/graph-api"
import type { SchemaNode } from "@/app/ontology/page"

Expand Down Expand Up @@ -206,6 +207,7 @@ function PersonCard({ props }: { props: Record<string, unknown> }) {
export function NodePreviewPanel({ node, onBack, schemas }: NodePreviewPanelProps) {
const [unlockState, setUnlockState] = useState<UnlockState>("preview")
const [fullNode, setFullNode] = useState<GraphNode | null>(null)
const refreshBalance = useUserStore((s) => s.refreshBalance)

const nodeType = node.node_type ?? "Unknown"
const schema = schemas.find((s) => s.type === nodeType)
Expand Down Expand Up @@ -242,6 +244,7 @@ export function NodePreviewPanel({ node, onBack, schemas }: NodePreviewPanelProp
setFullNode(result)
}
setUnlockState("unlocked")
refreshBalance()
} catch {
setUnlockState("error")
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/modals/add-content-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
export function AddContentModal() {
const { activeModal, close, open: openModal } = useModalStore()
const { budget, setBudget, pubKey } = useUserStore()
const refreshBalance = useUserStore((s) => s.refreshBalance)
const [sourceUrl, setSourceUrl] = useState("")
const [detectedType, setDetectedType] = useState<SourceType | null>(null)
const [detecting, setDetecting] = useState(false)
Expand Down Expand Up @@ -109,6 +110,7 @@ export function AddContentModal() {
try {
await submitWithAuth(trimmed, detectedType)
setSuccess(true)
refreshBalance()
setTimeout(() => {
setSourceUrl("")
setDetectedType(null)
Expand All @@ -125,6 +127,7 @@ export function AddContentModal() {
// Retry after payment
await submitWithAuth(trimmed, detectedType)
setSuccess(true)
refreshBalance()
setTimeout(() => {
setSourceUrl("")
setDetectedType(null)
Expand All @@ -148,7 +151,7 @@ export function AddContentModal() {
} finally {
setSubmitting(false)
}
}, [sourceUrl, detectedType, close, setBudget, submitWithAuth])
}, [sourceUrl, detectedType, close, setBudget, submitWithAuth, refreshBalance])

const handleOpenChange = useCallback(
(open: boolean) => {
Expand Down
28 changes: 5 additions & 23 deletions src/components/modals/budget-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { Button } from "@/components/ui/button"
import { Separator } from "@/components/ui/separator"
import { useModalStore } from "@/stores/modal-store"
import { useUserStore } from "@/stores/user-store"
import { isSphinx, getL402, hasWebLN, payInvoice, payL402, topUpLsat, topUpConfirm } from "@/lib/sphinx"
import { api } from "@/lib/api"
import { isSphinx, hasWebLN, payInvoice, payL402, topUpLsat, topUpConfirm } from "@/lib/sphinx"

type Step = "balance" | "amount" | "invoice" | "success"

Expand All @@ -24,6 +23,7 @@ const PRESET_AMOUNTS = [50, 100, 500, 1000]
export function BudgetModal() {
const { activeModal, close } = useModalStore()
const { budget, setBudget } = useUserStore()
const refreshBalance = useUserStore((s) => s.refreshBalance)
const [loading, setLoading] = useState(false)
const [error, setError] = useState("")
const [step, setStep] = useState<Step>("balance")
Expand Down Expand Up @@ -66,15 +66,7 @@ export function BudgetModal() {
}
}, [])

const refreshBalance = useCallback(async () => {
const l402 = await getL402()
if (l402) {
const bal = await api.get<{ balance: number }>("/balance", {
Authorization: l402,
})
setBudget(bal.balance)
}
}, [setBudget])


// Route "Top Up" to the right flow
const handleTopUp = useCallback(async () => {
Expand Down Expand Up @@ -198,21 +190,11 @@ export function BudgetModal() {
const handleRefreshBalance = useCallback(async () => {
setLoading(true)
try {
const l402 = await getL402()
if (!l402) {
setBudget(0)
return
}
const balance = await api.get<{ balance: number }>("/balance", {
Authorization: l402,
})
setBudget(balance.balance)
} catch {
// keep existing budget
await refreshBalance()
} finally {
setLoading(false)
}
}, [setBudget])
}, [refreshBalance])

const canTopUp = sphinxConnected || weblnAvailable || hasExistingL402

Expand Down
8 changes: 5 additions & 3 deletions src/components/search/search-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useMocks, MOCK_NODES, MOCK_EDGES } from "@/lib/mock-data"
export function SearchBar() {
const setSearchTerm = useAppStore((s) => s.setSearchTerm)
const { setGraphData, setLoading } = useGraphStore()
const setBudget = useUserStore((s) => s.setBudget)
const refreshBalance = useUserStore((s) => s.refreshBalance)
const openModal = useModalStore((s) => s.open)
const [value, setValue] = useState("")
const [focused, setFocused] = useState(false)
Expand Down Expand Up @@ -46,17 +46,19 @@ export function SearchBar() {
} else {
const result = await searchNodes(trimmed, { limit: 100 }, controller.signal)
setGraphData(result.nodes ?? [], result.edges ?? [])
refreshBalance()
}
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") return

// Handle 402 — need payment to search
if (err instanceof Response && err.status === 402) {
try {
await payL402(setBudget)
await payL402(() => {})
// Retry search after payment
const result = await searchNodes(trimmed, { limit: 100 }, controller.signal)
setGraphData(result.nodes ?? [], result.edges ?? [])
refreshBalance()
} catch {
// Payment failed or cancelled — open budget modal
openModal("budget")
Expand All @@ -69,7 +71,7 @@ export function SearchBar() {
setLoading(false)
}
},
[value, setSearchTerm, setGraphData, setLoading]
[value, setSearchTerm, setGraphData, setLoading, refreshBalance]
)

const handleClear = useCallback(() => {
Expand Down
14 changes: 14 additions & 0 deletions src/stores/user-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client"

import { create } from "zustand"
import { getL402 } from "@/lib/sphinx"
import { api } from "@/lib/api"

interface UserState {
isAdmin: boolean
Expand All @@ -17,6 +19,7 @@ interface UserActions {
setBudget: (val: number | null) => void
incrementNodeCount: () => void
resetNodeCount: () => void
refreshBalance: () => Promise<void>
}

export type UserStore = UserState & UserActions
Expand All @@ -34,4 +37,15 @@ export const useUserStore = create<UserStore>((set) => ({
incrementNodeCount: () =>
set((s) => ({ nodeCount: s.nodeCount + 1 })),
resetNodeCount: () => set({ nodeCount: 0 }),
refreshBalance: async () => {
const l402 = await getL402()
if (!l402) { set({ budget: 0 }); return }
try {
const bal = await api.get<{ balance: number }>("/balance", { Authorization: l402 })
set({ budget: bal.balance })
} catch {
localStorage.removeItem("l402")
set({ budget: 0 })
}
},
}))
Loading