Spaces:
Running

File size: 5,407 Bytes
ae6f1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c363f68
 
 
ae6f1cf
 
c363f68
ae6f1cf
 
 
c363f68
ae6f1cf
 
 
 
 
c363f68
 
ae6f1cf
 
 
 
 
 
c363f68
ae6f1cf
 
 
 
 
 
 
 
c363f68
ae6f1cf
 
 
 
 
 
 
 
 
 
 
 
c363f68
ae6f1cf
 
 
 
 
 
c363f68
ae6f1cf
 
 
 
 
 
c363f68
ae6f1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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>
  )
}