{
  "id": "price-alert",
  "name": "Price Alert",
  "category": "automation",
  "tags": ["alert", "price", "automation", "stock", "crypto", "watch", "threshold"],
  "description": "Watch a ticker (stock or crypto) and fire a TITAN notification when the price crosses a threshold. Polls every 30s. Works with Yahoo Finance for stocks and Coingecko for crypto.",
  "triggers": ["price alert", "alert when", "notify me when", "watch price", "BTC alert", "stock alert", "threshold alert"],
  "defaultSize": { "w": 4, "h": 4 },
  "source": "function PriceAlert() {\n  const [symbol, setSymbol] = React.useState('REPLACE_WITH_SYMBOL');\n  const [threshold, setThreshold] = React.useState('REPLACE_WITH_THRESHOLD');\n  const [direction, setDirection] = React.useState('below');\n  const [type, setType] = React.useState('REPLACE_WITH_TYPE');\n  const [price, setPrice] = React.useState(null);\n  const [armed, setArmed] = React.useState(false);\n  const [fired, setFired] = React.useState(false);\n  const [error, setError] = React.useState('');\n  React.useEffect(() => {\n    if (!armed) return;\n    let alive = true;\n    const tick = async () => {\n      try {\n        let p;\n        if (type === 'crypto') {\n          const r = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=${encodeURIComponent(symbol.toLowerCase())}&vs_currencies=usd`).then(r=>r.json());\n          p = r[symbol.toLowerCase()]?.usd;\n        } else {\n          const r = await fetch(`https://query1.finance.yahoo.com/v8/finance/chart/${symbol.toUpperCase()}`).then(r=>r.json());\n          p = r.chart?.result?.[0]?.meta?.regularMarketPrice;\n        }\n        if (!alive || typeof p !== 'number') return;\n        setPrice(p); setError('');\n        const t = parseFloat(threshold);\n        const hit = direction === 'below' ? p < t : p > t;\n        if (hit && !fired) {\n          setFired(true);\n          try { await titan.api.call('/api/notify', { method: 'POST', body: { title: 'Price Alert', message: `${symbol} ${direction} ${t}: now ${p}` } }); } catch {}\n        }\n      } catch (e) { setError(String(e?.message||e)); }\n    };\n    tick();\n    const id = setInterval(tick, 30000);\n    return () => { alive = false; clearInterval(id); };\n  }, [armed, symbol, threshold, direction, type, fired]);\n  return (\n    <div style={{padding:14,fontFamily:'system-ui',height:'100%',display:'flex',flexDirection:'column',gap:10}}>\n      <div style={{display:'grid',gridTemplateColumns:'1fr 1fr',gap:8}}>\n        <input value={symbol} onChange={e=>setSymbol(e.target.value)} placeholder=\"Symbol\" style={{padding:'8px',border:'1px solid #ccc',borderRadius:6}}/>\n        <select value={type} onChange={e=>setType(e.target.value)} style={{padding:'8px',border:'1px solid #ccc',borderRadius:6}}><option value=\"stock\">Stock</option><option value=\"crypto\">Crypto</option></select>\n        <select value={direction} onChange={e=>setDirection(e.target.value)} style={{padding:'8px',border:'1px solid #ccc',borderRadius:6}}><option value=\"below\">Below</option><option value=\"above\">Above</option></select>\n        <input value={threshold} onChange={e=>setThreshold(e.target.value)} placeholder=\"Price\" type=\"number\" style={{padding:'8px',border:'1px solid #ccc',borderRadius:6}}/>\n      </div>\n      <button onClick={()=>{setArmed(!armed);setFired(false);}} style={{padding:'10px',border:'none',borderRadius:8,background:armed?'#FF3B30':'#34C759',color:'#fff',fontWeight:600,cursor:'pointer'}}>{armed?'Disarm':'Arm Alert'}</button>\n      <div style={{textAlign:'center'}}>\n        <div style={{fontSize:32,fontWeight:300}}>{price !== null ? `$${Number(price).toLocaleString(undefined,{maximumFractionDigits:4})}` : '—'}</div>\n        <div style={{fontSize:12,color:armed?'#34C759':'#888'}}>{armed?(fired?'🔔 Fired!':'Watching…'):'Idle'}</div>\n      </div>\n      {error && <div style={{color:'#FF3B30',fontSize:12,textAlign:'center'}}>{error}</div>}\n    </div>\n  );\n}\nrender(<PriceAlert/>);",
  "placeholders": [
    { "name": "REPLACE_WITH_SYMBOL", "description": "Ticker (AAPL) or coingecko id (bitcoin)", "default": "bitcoin" },
    { "name": "REPLACE_WITH_THRESHOLD", "description": "Price threshold", "default": "50000" },
    { "name": "REPLACE_WITH_TYPE", "description": "stock or crypto", "default": "crypto" }
  ]
}
