'use client' import Link from 'next/link' import { ArrowLeft } from 'lucide-react' import { useState } from 'react' import { checkSymbol, addUserInterest } from '@/lib/userInterestsApi' type ResultState = { type: 'success' | 'error' | 'loading' | null; message: string } export default function AddItem() { const [activeTab, setActiveTab] = useState<'symbol' | 'topic'>('symbol') const [symbolInput, setSymbolInput] = useState('') const [topicInput, setTopicInput] = useState('') const [symbolResult, setSymbolResult] = useState({ type: null, message: '' }) const [topicResult, setTopicResult] = useState({ type: null, message: '' }) const handleAddSymbol = async () => { const trimmed = symbolInput.trim() if (!trimmed) return setSymbolResult({ type: 'loading', message: 'Verifying symbol...' }) try { // Step 1: Check if symbol is valid const checkResult = await checkSymbol(trimmed) if (!checkResult.found) { setSymbolResult({ type: 'error', message: checkResult.message, }) return } // Step 2: Add the symbol const addResult = await addUserInterest('symbol', trimmed) setSymbolResult({ type: 'success', message: `${trimmed.toUpperCase()} has been successfully added!`, }) setSymbolInput('') } catch (err) { setSymbolResult({ type: 'error', message: err instanceof Error ? err.message : 'Failed to add symbol', }) } } const handleAddTopic = async () => { const trimmed = topicInput.trim() if (!trimmed) return setTopicResult({ type: 'loading', message: 'Adding topic...' }) try { await addUserInterest('topic', trimmed) setTopicResult({ type: 'success', message: `${trimmed} has been added to your interests list!`, }) setTopicInput('') } catch (err) { setTopicResult({ type: 'error', message: err instanceof Error ? err.message : 'Failed to add topic', }) } } return (
{/* Header */}

Add Item

{/* Content */}
{/* Tabs */}
{/* Add Symbol Tab */} {activeTab === 'symbol' && (
setSymbolInput(e.target.value)} disabled={symbolResult.type === 'loading'} className="w-full border border-border bg-input text-foreground p-4 text-base disabled:opacity-60" />
{symbolResult.type && (
{symbolResult.message}
)}
)} {/* Add Topic Tab */} {activeTab === 'topic' && (
setTopicInput(e.target.value)} disabled={topicResult.type === 'loading'} className="w-full border border-border bg-input text-foreground p-4 text-base disabled:opacity-60" />
{topicResult.type && (
{topicResult.message}
)}
)}
) }