Spaces:
Running

anycoder-cf48624d / components /JsonUploadModal.js
powerllm's picture
Upload components/JsonUploadModal.js with huggingface_hub
c363f68 verified
Raw History Blame Contribute Delete
5.41 kB
import { X, FileJson, CheckCircle, AlertCircle } from 'lucide-react'
import { useState } from 'react'
export default function JsonUploadModal({ isOpen, onClose, onUpload }) {
const [jsonText, setJsonText] = useState('')
const [error, setError] = useState(null)
const [success, setSuccess] = useState(false)
const validateJson = (text) => {
try {
const parsed = JSON.parse(text)
if (!Array.isArray(parsed)) {
return { valid: false, error: 'JSON must be an array of rules' }
}
for (let i = 0; i < parsed.length; i++) {
const rule = parsed[i]
if (!rule.rule || !rule.condition || !rule.action) {
return {
valid: false,
error: `Rule ${i + 1} missing required fields (rule, condition, action)`
}
}
if (!['LONG', 'SHORT', 'FOLLOW_TREND'].includes(rule.action)) {
return {
valid: false,
error: `Rule ${i + 1}: action must be LONG, SHORT, or FOLLOW_TREND`
}
}
}
return { valid: true, data: parsed }
} catch (e) {
return { valid: false, error: 'Invalid JSON format' }
}
}
const handleSubmit = () => {
setError(null)
setSuccess(false)
const validation = validateJson(jsonText)
if (!validation.valid) {
setError(validation.error)
return
}
onUpload(validation.data)
setSuccess(true)
setTimeout(() => {
onClose()
setJsonText('')
setSuccess(false)
}, 1500)
}
const loadExample = () => {
const example = [
{
"rule": "MACD Bullish Crossover",
"condition": "MACD > Signal",
"action": "LONG",
"confidence": "Medium",
"description": "When MACD line crosses above the signal line, indicating bullish momentum."
},
{
"rule": "Bollinger Breakout",
"condition": "Price > Upper_Band",
"action": "SHORT",
"confidence": "Low",
"description": "Price breaking above upper Bollinger Band may indicate overextension."
}
]
setJsonText(JSON.stringify(example, null, 2))
}
if (!isOpen) return null
return (
<div className="fixed inset-0 bg-black/80 backdrop-blur-sm z-50 flex items-center justify-center p-4">
<div className="card w-full max-w-2xl max-h-[90vh] overflow-hidden flex flex-col animate-in fade-in zoom-in duration-200">
<div className="p-6 border-b border-white/5 flex items-center justify-between bg-dark-900/50">
<div className="flex items-center space-x-2">
<FileJson className="w-5 h-5 text-primary-400" />
<h2 className="text-lg font-semibold text-white">Add Custom Rules</h2>
</div>
<button
onClick={onClose}
className="text-dark-400 hover:text-white transition-colors p-2 hover:bg-white/5 rounded-lg"
>
<X className="w-5 h-5" />
</button>
</div>
<div className="flex-1 overflow-y-auto p-6 space-y-4">
<div className="bg-dark-900/50 border border-white/5 rounded-xl p-4">
<h3 className="text-sm font-medium text-white mb-2">JSON Format</h3>
<p className="text-xs text-dark-400 mb-3">
Paste your JSON array of trading rules. Each rule must include: rule, condition, action, confidence, and description.
</p>
<button
onClick={loadExample}
className="text-xs text-primary-400 hover:text-primary-300 transition-colors font-medium"
>
Load Example →
</button>
</div>
<textarea
value={jsonText}
onChange={(e) => setJsonText(e.target.value)}
className="w-full h-64 bg-dark-900 border border-white/10 rounded-xl text-white font-mono text-xs p-4 focus:outline-none focus:ring-2 focus:ring-primary-500/50 focus:border-transparent resize-none"
placeholder='[
{
"rule": "Your Rule Name",
"condition": "INDICATOR < VALUE",
"action": "LONG",
"confidence": "High",
"description": "Description of when this rule triggers"
}
]'
/>
{error && (
<div className="flex items-center space-x-2 text-danger bg-danger/10 border border-danger/20 rounded-lg p-3">
<AlertCircle className="w-4 h-4 flex-shrink-0" />
<span className="text-sm">{error}</span>
</div>
)}
{success && (
<div className="flex items-center space-x-2 text-success bg-success/10 border border-success/20 rounded-lg p-3">
<CheckCircle className="w-4 h-4 flex-shrink-0" />
<span className="text-sm">Rules added successfully!</span>
</div>
)}
</div>
<div className="p-6 border-t border-white/5 bg-dark-900/50 flex justify-end space-x-3">
<button
onClick={onClose}
className="btn-secondary"
>
Cancel
</button>
<button
onClick={handleSubmit}
disabled={!jsonText.trim()}
className="btn-primary disabled:opacity-50 disabled:cursor-not-allowed"
>
Add Rules
</button>
</div>
</div>
</div>
)
}