'use client' import { useId, useState } from 'react' import { useActions, useAIState, useUIState } from '@aigxion/isdk/rsc' import { formatNumber } from '@/lib/utils' import type { AI } from '@/lib/chat/actions' interface Purchase { numberOfShares?: number symbol: string price: number status: 'requires_action' | 'completed' | 'expired' } export function Purchase({ props: { numberOfShares, symbol, price, status = 'requires_action' } }: { props: Purchase }) { const [value, setValue] = useState(numberOfShares || 100) const [purchasingUI, setPurchasingUI] = useState(null) const [aiState, setAIState] = useAIState() const [, setMessages] = useUIState() const { confirmPurchase } = useActions() // Unique identifier for this UI component. const id = useId() // Whenever the slider changes, we need to update the local value state and the history // so LLM also knows what's going on. function onSliderChange(e: React.ChangeEvent) { const newValue = Number(e.target.value) setValue(newValue) // Insert a hidden history info to the list. const message = { role: 'system' as const, content: `[User has changed to purchase ${newValue} shares of ${name}. Total cost: $${( newValue * price ).toFixed(2)}]`, // Identifier of this UI component, so we don't insert it many times. id } // If last history state is already this info, update it. This is to avoid // adding every slider change to the history. if (aiState.messages[aiState.messages.length - 1]?.id === id) { setAIState({ ...aiState, messages: [...aiState.messages.slice(0, -1), message] }) return } // If it doesn't exist, append it to history. setAIState({ ...aiState, messages: [...aiState.messages, message] }) } return (
+1.23% ↑
{symbol}
${price}
{purchasingUI ? (
{purchasingUI}
) : status === 'requires_action' ? ( <>

Shares to purchase

10 100 500 1000

Total cost

{value} shares
×
${price} per share
= {formatNumber(value * price)}
) : status === 'completed' ? (

You have successfully purchased {value} ${symbol}. Total cost:{' '} {formatNumber(value * price)}

) : status === 'expired' ? (

Your checkout session has expired!

) : null}
) }