File size: 11,414 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
 * Widget Schema Viewer
 * 
 * Komponent der visualiserer en widgets metadata/schema.
 * Viser hvad widgeten kræver, leverer og kan konfigureres med.
 */

import { useState } from 'react';
import { 
  Info, 
  ArrowDownToLine, 
  ArrowUpFromLine, 
  Settings, 
  Link2, 
  Tag,
  Zap,
  Database,
  RefreshCw
} from 'lucide-react';
import { WidgetMetadata } from '@/types/WidgetSchema';
import { widgetRegistry } from '@/lib/widgetRegistry';
import { cn } from '@/lib/utils';

interface WidgetSchemaViewerProps {
  widgetId: string;
  compact?: boolean;
  className?: string;
}

export function WidgetSchemaViewer({ widgetId, compact = false, className }: WidgetSchemaViewerProps) {
  const [activeTab, setActiveTab] = useState<'requires' | 'provides' | 'config' | 'relations'>('requires');
  const metadata = widgetRegistry.get(widgetId);

  if (!metadata) {
    return (
      <div className={cn("p-4 border border-border/30 rounded-lg bg-card/50", className)}>
        <p className="text-muted-foreground text-sm">Widget metadata ikke tilgængelig</p>
      </div>
    );
  }

  if (compact) {
    return <CompactView metadata={metadata} className={className} />;
  }

  return (
    <div className={cn("border border-border/30 rounded-lg bg-card/50 overflow-hidden", className)}>
      {/* Header */}
      <div className="p-4 border-b border-border/30 bg-gradient-to-r from-primary/10 to-transparent">
        <div className="flex items-start justify-between">
          <div>
            <h3 className="font-mono text-lg text-foreground">{metadata.name}</h3>
            <p className="text-sm text-muted-foreground mt-1">{metadata.description}</p>
          </div>
          <div className="flex items-center gap-2">
            <span className={cn(
              "px-2 py-0.5 rounded text-xs font-mono",
              metadata.priority === 'critical' && "bg-destructive/20 text-destructive",
              metadata.priority === 'high' && "bg-warning/20 text-warning",
              metadata.priority === 'medium' && "bg-primary/20 text-primary",
              metadata.priority === 'low' && "bg-muted text-muted-foreground"
            )}>
              {metadata.priority || 'medium'}
            </span>
            <span className="text-xs text-muted-foreground font-mono">v{metadata.version}</span>
          </div>
        </div>
        
        {/* Tags */}
        <div className="flex flex-wrap gap-1 mt-3">
          {metadata.tags.map(tag => (
            <span key={tag} className="px-2 py-0.5 bg-secondary/50 rounded text-xs text-secondary-foreground flex items-center gap-1">
              <Tag className="h-3 w-3" />
              {tag}
            </span>
          ))}
        </div>
      </div>

      {/* Tabs */}
      <div className="flex border-b border-border/30">
        <TabButton 
          active={activeTab === 'requires'} 
          onClick={() => setActiveTab('requires')}
          icon={ArrowDownToLine}
          label="Kræver"
          count={metadata.requires.length}
        />
        <TabButton 
          active={activeTab === 'provides'} 
          onClick={() => setActiveTab('provides')}
          icon={ArrowUpFromLine}
          label="Leverer"
          count={metadata.provides.length}
        />
        <TabButton 
          active={activeTab === 'config'} 
          onClick={() => setActiveTab('config')}
          icon={Settings}
          label="Konfiguration"
          count={metadata.config.length}
        />
        <TabButton 
          active={activeTab === 'relations'} 
          onClick={() => setActiveTab('relations')}
          icon={Link2}
          label="Relationer"
          count={metadata.relations.length}
        />
      </div>

      {/* Content */}
      <div className="p-4 max-h-80 overflow-y-auto">
        {activeTab === 'requires' && (
          <div className="space-y-3">
            {metadata.requires.length === 0 ? (
              <p className="text-muted-foreground text-sm">Ingen data-afhængigheder</p>
            ) : (
              metadata.requires.map(req => (
                <div key={req.id} className="p-3 bg-background/50 rounded-lg border border-border/20">
                  <div className="flex items-center justify-between">
                    <span className="font-mono text-sm text-foreground">{req.name}</span>
                    <div className="flex items-center gap-2">
                      <span className="text-xs px-2 py-0.5 bg-primary/20 text-primary rounded font-mono">
                        {req.type}
                      </span>
                      {req.required && (
                        <span className="text-xs px-2 py-0.5 bg-destructive/20 text-destructive rounded">
                          påkrævet
                        </span>
                      )}
                    </div>
                  </div>
                  <p className="text-xs text-muted-foreground mt-1">{req.description}</p>
                  {req.refreshRate && (
                    <div className="flex items-center gap-1 mt-2 text-xs text-muted-foreground">
                      <RefreshCw className="h-3 w-3" />
                      Opdateres hver {req.refreshRate / 1000}s
                    </div>
                  )}
                </div>
              ))
            )}
          </div>
        )}

        {activeTab === 'provides' && (
          <div className="space-y-3">
            {metadata.provides.length === 0 ? (
              <p className="text-muted-foreground text-sm">Ingen outputs defineret</p>
            ) : (
              metadata.provides.map(cap => (
                <div key={cap.id} className="p-3 bg-background/50 rounded-lg border border-border/20">
                  <div className="flex items-center justify-between">
                    <span className="font-mono text-sm text-foreground">{cap.name}</span>
                    <span className="text-xs px-2 py-0.5 bg-accent/20 text-accent-foreground rounded font-mono">
                      {cap.outputType}
                    </span>
                  </div>
                  <p className="text-xs text-muted-foreground mt-1">{cap.description}</p>
                  {cap.triggers && cap.triggers.length > 0 && (
                    <div className="flex items-center gap-1 mt-2 flex-wrap">
                      <Zap className="h-3 w-3 text-warning" />
                      {cap.triggers.map(trigger => (
                        <span key={trigger} className="text-xs px-1.5 py-0.5 bg-warning/10 text-warning rounded">
                          {trigger}
                        </span>
                      ))}
                    </div>
                  )}
                </div>
              ))
            )}
          </div>
        )}

        {activeTab === 'config' && (
          <div className="space-y-3">
            {metadata.config.length === 0 ? (
              <p className="text-muted-foreground text-sm">Ingen konfigurerbare indstillinger</p>
            ) : (
              metadata.config.map(cfg => (
                <div key={cfg.id} className="p-3 bg-background/50 rounded-lg border border-border/20">
                  <div className="flex items-center justify-between">
                    <span className="font-mono text-sm text-foreground">{cfg.name}</span>
                    <span className="text-xs px-2 py-0.5 bg-secondary/50 text-secondary-foreground rounded font-mono">
                      {cfg.type}
                    </span>
                  </div>
                  <p className="text-xs text-muted-foreground mt-1">{cfg.description}</p>
                  <div className="mt-2 text-xs">
                    <span className="text-muted-foreground">Default: </span>
                    <span className="font-mono text-primary">{String(cfg.default)}</span>
                  </div>
                  {cfg.options && (
                    <div className="flex flex-wrap gap-1 mt-2">
                      {cfg.options.map(opt => (
                        <span key={String(opt.value)} className="text-xs px-1.5 py-0.5 bg-muted rounded">
                          {opt.label}
                        </span>
                      ))}
                    </div>
                  )}
                </div>
              ))
            )}
          </div>
        )}

        {activeTab === 'relations' && (
          <div className="space-y-3">
            {metadata.relations.length === 0 ? (
              <p className="text-muted-foreground text-sm">Ingen definerede relationer</p>
            ) : (
              metadata.relations.map((rel, idx) => (
                <div key={idx} className="p-3 bg-background/50 rounded-lg border border-border/20">
                  <div className="flex items-center gap-2">
                    <span className={cn(
                      "text-xs px-2 py-0.5 rounded font-mono",
                      rel.type === 'depends_on' && "bg-blue-500/20 text-blue-400",
                      rel.type === 'provides_to' && "bg-green-500/20 text-green-400",
                      rel.type === 'synchronizes' && "bg-purple-500/20 text-purple-400",
                      rel.type === 'triggers' && "bg-orange-500/20 text-orange-400",
                      rel.type === 'aggregates' && "bg-cyan-500/20 text-cyan-400"
                    )}>
                      {rel.type}
                    </span>
                    <span className="font-mono text-sm text-foreground">{rel.targetWidgetId}</span>
                  </div>
                  <p className="text-xs text-muted-foreground mt-1">{rel.description}</p>
                  {rel.dataFlow && (
                    <div className="flex items-center gap-1 mt-2 text-xs text-muted-foreground">
                      <Database className="h-3 w-3" />
                      Data: {rel.dataFlow}
                    </div>
                  )}
                </div>
              ))
            )}
          </div>
        )}
      </div>
    </div>
  );
}

// Tab button component
function TabButton({ 
  active, 
  onClick, 
  icon: Icon, 
  label, 
  count 
}: { 
  active: boolean; 
  onClick: () => void; 
  icon: React.ElementType; 
  label: string; 
  count: number;
}) {
  return (
    <button
      onClick={onClick}
      className={cn(
        "flex items-center gap-2 px-4 py-2 text-sm transition-colors",
        active 
          ? "bg-primary/10 text-primary border-b-2 border-primary" 
          : "text-muted-foreground hover:text-foreground hover:bg-muted/50"
      )}
    >
      <Icon className="h-4 w-4" />
      {label}
      <span className={cn(
        "px-1.5 py-0.5 rounded-full text-xs",
        active ? "bg-primary/20" : "bg-muted"
      )}>
        {count}
      </span>
    </button>
  );
}

// Compact view for inline display
function CompactView({ metadata, className }: { metadata: WidgetMetadata; className?: string }) {
  return (
    <div className={cn("p-2 border border-border/30 rounded bg-card/50", className)}>
      <div className="flex items-center gap-2 text-xs">
        <Info className="h-3 w-3 text-primary" />
        <span className="font-mono text-foreground">{metadata.name}</span>
        <span className="text-muted-foreground"></span>
        <span className="text-muted-foreground">
          {metadata.requires.length} inputs, {metadata.provides.length} outputs
        </span>
      </div>
    </div>
  );
}