File size: 13,237 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
/**
 * SourceDiscoveryWidget - Displays orphan sources and allows widget generation
 * 
 * This widget enables the reverse flow: Source → Widget
 * Shows data sources that don't have matching widgets and offers to generate them.
 */

import React, { useState } from 'react';
import {
    Database, Zap, Globe, AlertTriangle, Plus, RefreshCw,
    Check, Loader2, ChevronRight, Sparkles, Link2
} from 'lucide-react';
import { useSourceDiscovery, useGeneratedWidgets } from '@/services/SourceWidgetDiscovery';
import { cn } from '@/lib/utils';

// Icon mapping for source types
const sourceTypeIcons: Record<string, React.ReactNode> = {
    database: <Database className="w-4 h-4" />,
    'mcp-tool': <Zap className="w-4 h-4" />,
    file: <Database className="w-4 h-4" />,
    'email-adapter': <AlertTriangle className="w-4 h-4" />,
    external: <Globe className="w-4 h-4" />,
    osint: <Globe className="w-4 h-4" />,
    threatIntel: <AlertTriangle className="w-4 h-4" />,
};

export default function SourceDiscoveryWidget() {
    const {
        sources,
        orphanSources,
        suggestedWidgets,
        isLoading,
        refresh,
        generateWidget
    } = useSourceDiscovery();

    const { generatedWidgets } = useGeneratedWidgets();

    const [generatingFor, setGeneratingFor] = useState<string | null>(null);
    const [activeTab, setActiveTab] = useState<'orphans' | 'all' | 'generated'>('orphans');

    const handleGenerateWidget = async (sourceId: string, suggestedName: string) => {
        setGeneratingFor(sourceId);
        try {
            await generateWidget(sourceId, suggestedName);
        } finally {
            setGeneratingFor(null);
        }
    };

    return (
        <div className="h-full flex flex-col bg-card/50 backdrop-blur-sm border border-border/50 rounded-lg overflow-hidden">
            {/* Header */}
            <div className="flex items-center justify-between p-4 border-b border-border/30 bg-gradient-to-r from-primary/10 to-transparent">
                <div className="flex items-center gap-2">
                    <Link2 className="w-5 h-5 text-primary" />
                    <span className="font-display text-sm uppercase tracking-wider text-primary">
                        Source Discovery
                    </span>
                </div>
                <div className="flex items-center gap-2">
                    <span className="text-xs text-muted-foreground">
                        {sources.length} kilder • {orphanSources.length} uden widget
                    </span>
                    <button
                        onClick={refresh}
                        className="p-1.5 hover:bg-secondary/50 rounded transition-colors"
                        disabled={isLoading}
                    >
                        <RefreshCw className={cn("w-4 h-4 text-primary", isLoading && "animate-spin")} />
                    </button>
                </div>
            </div>

            {/* Tabs */}
            <div className="flex border-b border-border/30">
                {[
                    { id: 'orphans', label: 'Orphan Kilder', count: orphanSources.length },
                    { id: 'all', label: 'Alle Kilder', count: sources.length },
                    { id: 'generated', label: 'Genererede', count: generatedWidgets.length },
                ].map(tab => (
                    <button
                        key={tab.id}
                        onClick={() => setActiveTab(tab.id as any)}
                        className={cn(
                            "flex-1 px-4 py-2 text-xs font-medium transition-colors",
                            activeTab === tab.id
                                ? "text-primary border-b-2 border-primary bg-primary/5"
                                : "text-muted-foreground hover:text-foreground"
                        )}
                    >
                        {tab.label}
                        {tab.count > 0 && (
                            <span className="ml-1.5 px-1.5 py-0.5 bg-primary/20 text-primary rounded text-xs">
                                {tab.count}
                            </span>
                        )}
                    </button>
                ))}
            </div>

            {/* Content */}
            <div className="flex-1 overflow-y-auto p-4 space-y-3">
                {isLoading ? (
                    <div className="flex items-center justify-center h-32">
                        <Loader2 className="w-6 h-6 text-primary animate-spin" />
                    </div>
                ) : activeTab === 'orphans' ? (
                    orphanSources.length === 0 ? (
                        <div className="text-center py-8">
                            <Check className="w-8 h-8 text-green-400 mx-auto mb-2" />
                            <p className="text-sm text-muted-foreground">Alle kilder har widgets!</p>
                        </div>
                    ) : (
                        orphanSources.map(source => {
                            const suggested = suggestedWidgets.find(s => s.forSource === source.id);
                            return (
                                <div
                                    key={source.id}
                                    className="p-3 bg-secondary/30 border border-border/30 rounded-lg hover:border-primary/30 transition-colors"
                                >
                                    <div className="flex items-start justify-between gap-3">
                                        <div className="flex items-center gap-2">
                                            <div className="p-2 bg-primary/10 rounded">
                                                {sourceTypeIcons[source.type] || <Database className="w-4 h-4" />}
                                            </div>
                                            <div>
                                                <p className="text-sm font-medium text-foreground">{source.name}</p>
                                                <p className="text-xs text-muted-foreground">{source.type}</p>
                                            </div>
                                        </div>
                                        <button
                                            onClick={() => handleGenerateWidget(source.id, suggested?.suggestedName || `${source.name} Widget`)}
                                            disabled={generatingFor === source.id}
                                            className={cn(
                                                "flex items-center gap-1.5 px-3 py-1.5 rounded text-xs font-medium transition-all",
                                                "bg-primary/20 text-primary hover:bg-primary/30",
                                                generatingFor === source.id && "opacity-50 cursor-not-allowed"
                                            )}
                                        >
                                            {generatingFor === source.id ? (
                                                <Loader2 className="w-3 h-3 animate-spin" />
                                            ) : (
                                                <Sparkles className="w-3 h-3" />
                                            )}
                                            Generer Widget
                                        </button>
                                    </div>

                                    {/* Capabilities */}
                                    <div className="mt-2 flex flex-wrap gap-1">
                                        {source.capabilities.slice(0, 4).map((cap, i) => (
                                            <span key={i} className="px-1.5 py-0.5 bg-secondary/50 text-muted-foreground text-xs rounded">
                                                {cap}
                                            </span>
                                        ))}
                                        {source.capabilities.length > 4 && (
                                            <span className="px-1.5 py-0.5 text-muted-foreground text-xs">
                                                +{source.capabilities.length - 4} flere
                                            </span>
                                        )}
                                    </div>

                                    {/* Suggested widget info */}
                                    {suggested && (
                                        <div className="mt-2 flex items-center gap-2 text-xs text-primary/70">
                                            <ChevronRight className="w-3 h-3" />
                                            Foreslået: <span className="font-medium">{suggested.suggestedName}</span>
                                        </div>
                                    )}
                                </div>
                            );
                        })
                    )
                ) : activeTab === 'all' ? (
                    sources.map(source => (
                        <div
                            key={source.id}
                            className="p-3 bg-secondary/20 border border-border/20 rounded-lg"
                        >
                            <div className="flex items-center gap-2">
                                <div className="p-1.5 bg-secondary/50 rounded">
                                    {sourceTypeIcons[source.type] || <Database className="w-3 h-3" />}
                                </div>
                                <div className="flex-1">
                                    <p className="text-sm text-foreground">{source.name}</p>
                                    <div className="flex items-center gap-2 text-xs text-muted-foreground">
                                        <span>{source.type}</span>
                                        <span></span>
                                        <span>{source.estimatedLatency}ms</span>
                                        {source.recommendedWidgets.length > 0 && (
                                            <>
                                                <span></span>
                                                <span className="text-primary">{source.recommendedWidgets[0]}</span>
                                            </>
                                        )}
                                    </div>
                                </div>
                                {source.hasWidget ? (
                                    <Check className="w-4 h-4 text-green-400" />
                                ) : (
                                    <span className="text-xs text-orange-400">Ingen widget</span>
                                )}
                            </div>
                        </div>
                    ))
                ) : (
                    /* Generated widgets tab */
                    generatedWidgets.length === 0 ? (
                        <div className="text-center py-8">
                            <Plus className="w-8 h-8 text-muted-foreground mx-auto mb-2" />
                            <p className="text-sm text-muted-foreground">Ingen genererede widgets endnu</p>
                        </div>
                    ) : (
                        generatedWidgets.map((widget, i) => (
                            <div
                                key={i}
                                className="p-3 bg-gradient-to-r from-primary/10 to-transparent border border-primary/20 rounded-lg"
                            >
                                <div className="flex items-center gap-2">
                                    <Sparkles className="w-4 h-4 text-primary" />
                                    <div>
                                        <p className="text-sm font-medium text-foreground">{widget.name}</p>
                                        <p className="text-xs text-muted-foreground">
                                            For: {widget.dataSource} • Template: {widget.template}
                                        </p>
                                    </div>
                                </div>
                                <div className="mt-2 flex flex-wrap gap-1">
                                    {widget.capabilities.slice(0, 3).map((cap, j) => (
                                        <span key={j} className="px-1.5 py-0.5 bg-primary/10 text-primary text-xs rounded">
                                            {cap}
                                        </span>
                                    ))}
                                </div>
                            </div>
                        ))
                    )
                )}
            </div>

            {/* Footer */}
            <div className="p-3 border-t border-border/30 bg-secondary/20">
                <p className="text-xs text-muted-foreground text-center">
                    Autonomt system lærer fra kilde↔widget koblinger
                </p>
            </div>
        </div>
    );
}