@svadmin/ui 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/ui",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Pre-built admin UI components — AdminApp, AutoTable, AutoForm, Sidebar, Layout",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -436,6 +436,8 @@
436
436
  </div>
437
437
  {:else}
438
438
  <div in:fade={{ duration: 150 }}>
439
+ <!-- Desktop Table (hidden on mobile) -->
440
+ <div class="hidden md:block">
439
441
  <Table.Root>
440
442
  <Table.Header>
441
443
  {#each tbl.getHeaderGroups() as headerGroup}
@@ -582,6 +584,87 @@
582
584
  {/each}
583
585
  </Table.Body>
584
586
  </Table.Root>
587
+ </div>
588
+
589
+ <!-- Mobile Card View (visible only on small screens) -->
590
+ <div class="md:hidden space-y-3 p-2">
591
+ {#each tbl.getRowModel().rows as row}
592
+ {@const record = row.original}
593
+ {@const id = record[primaryKey] as string | number}
594
+ <div
595
+ class="rounded-xl border bg-card p-4 shadow-sm transition-colors {row.getIsSelected() ? 'ring-2 ring-primary bg-accent/30' : ''}"
596
+ >
597
+ <!-- Card header: ID + select + actions -->
598
+ <div class="flex items-center justify-between mb-3">
599
+ <div class="flex items-center gap-2">
600
+ {#if selectable && canDelete}
601
+ <Checkbox
602
+ checked={row.getIsSelected()}
603
+ onCheckedChange={() => row.toggleSelected()}
604
+ />
605
+ {/if}
606
+ <span class="text-xs font-mono text-muted-foreground">#{id}</span>
607
+ </div>
608
+ <div class="flex items-center gap-1">
609
+ {#if rowActions}
610
+ {@render rowActions({ record, id })}
611
+ {:else}
612
+ {#if canEdit}
613
+ <TooltipButton tooltip={t('common.edit')} variant="ghost" size="icon-sm" onclick={() => navigate(`/${resourceName}/edit/${id}`)}>
614
+ <Pencil class="h-4 w-4" />
615
+ </TooltipButton>
616
+ {/if}
617
+ <TooltipButton tooltip={t('common.detail')} variant="ghost" size="icon-sm" onclick={() => navigate(`/${resourceName}/show/${id}`)}>
618
+ <Eye class="h-4 w-4" />
619
+ </TooltipButton>
620
+ {#if canDelete}
621
+ <TooltipButton tooltip={t('common.delete')} variant="ghost" size="icon-sm" onclick={() => confirmDelete(id)} class="hover:text-destructive">
622
+ <Trash2 class="h-4 w-4" />
623
+ </TooltipButton>
624
+ {/if}
625
+ {/if}
626
+ </div>
627
+ </div>
628
+ <!-- Card fields -->
629
+ <div class="space-y-2">
630
+ {#each visibleFields.slice(0, 6) as field}
631
+ {@const value = record[field.key]}
632
+ <div class="flex items-start justify-between gap-4">
633
+ <span class="text-xs font-medium text-muted-foreground shrink-0">{field.label}</span>
634
+ <span class="text-sm text-right truncate max-w-[60%]">
635
+ {#if cellRenderer}
636
+ {@render cellRenderer({ field, value, record })}
637
+ {:else if field.type === 'boolean'}
638
+ <span class="inline-block h-2 w-2 rounded-full {value ? 'bg-green-500' : 'bg-muted-foreground/30'}"></span>
639
+ {:else if field.type === 'date' && value}
640
+ {new Date(value as string).toLocaleDateString()}
641
+ {:else if field.type === 'tags' && Array.isArray(value)}
642
+ <div class="flex flex-wrap gap-1 justify-end">
643
+ {#each (value as string[]).slice(0, 2) as tag}
644
+ <Badge variant="secondary" class="text-[10px]">{tag}</Badge>
645
+ {/each}
646
+ </div>
647
+ {:else if field.type === 'select' && field.options}
648
+ {@const opt = field.options.find(o => o.value === value)}
649
+ <Badge variant="outline" class="text-[10px]">{opt?.label ?? value ?? '—'}</Badge>
650
+ {:else}
651
+ {value ?? '—'}
652
+ {/if}
653
+ </span>
654
+ </div>
655
+ {/each}
656
+ </div>
657
+ </div>
658
+ {:else}
659
+ <div class="text-center py-10 text-muted-foreground">
660
+ {#if emptyState}
661
+ {@render emptyState()}
662
+ {:else}
663
+ {t('common.noData')}
664
+ {/if}
665
+ </div>
666
+ {/each}
667
+ </div>
585
668
  </div>
586
669
  {/if}
587
670
  </div>
@@ -0,0 +1,346 @@
1
+ <script lang="ts">
2
+ import { getChatProvider } from '@svadmin/core';
3
+ import type { ChatMessage } from '@svadmin/core';
4
+ import { t } from '@svadmin/core/i18n';
5
+ import { fly, fade, scale } from 'svelte/transition';
6
+ import { Button } from './ui/button/index.js';
7
+ import TooltipButton from './TooltipButton.svelte';
8
+ import { MessageCircle, X, Minus, Send, Loader2, Bot, Trash2 } from 'lucide-svelte';
9
+
10
+ let open = $state(false);
11
+ let minimized = $state(false);
12
+ let inputValue = $state('');
13
+ let messages = $state<ChatMessage[]>([]);
14
+ let isStreaming = $state(false);
15
+ let messagesContainer: HTMLDivElement | undefined = $state();
16
+ let abortController: AbortController | null = null;
17
+ let messageIdCounter = $state(0);
18
+
19
+ const provider = $derived(getChatProvider());
20
+
21
+ function genId(): string {
22
+ messageIdCounter++;
23
+ return `msg-${Date.now()}-${messageIdCounter}`;
24
+ }
25
+
26
+ function scrollToBottom() {
27
+ requestAnimationFrame(() => {
28
+ if (messagesContainer) {
29
+ messagesContainer.scrollTop = messagesContainer.scrollHeight;
30
+ }
31
+ });
32
+ }
33
+
34
+ async function sendMessage() {
35
+ if (!inputValue.trim() || isStreaming || !provider) return;
36
+
37
+ const userMsg: ChatMessage = {
38
+ id: genId(),
39
+ role: 'user',
40
+ content: inputValue.trim(),
41
+ timestamp: Date.now(),
42
+ };
43
+
44
+ messages = [...messages, userMsg];
45
+ inputValue = '';
46
+ scrollToBottom();
47
+
48
+ // Create placeholder assistant message
49
+ const assistantMsg: ChatMessage = {
50
+ id: genId(),
51
+ role: 'assistant',
52
+ content: '',
53
+ timestamp: Date.now(),
54
+ };
55
+ messages = [...messages, assistantMsg];
56
+ isStreaming = true;
57
+ scrollToBottom();
58
+
59
+ abortController = new AbortController();
60
+
61
+ try {
62
+ const result = provider.sendMessage(
63
+ messages.filter((m) => m.content), // exclude empty placeholder
64
+ { signal: abortController.signal },
65
+ );
66
+
67
+ if (result && typeof result === 'object' && Symbol.asyncIterator in result) {
68
+ // Streaming response
69
+ for await (const chunk of result as AsyncGenerator<string>) {
70
+ assistantMsg.content += chunk;
71
+ messages = [...messages.slice(0, -1), { ...assistantMsg }];
72
+ scrollToBottom();
73
+ }
74
+ } else {
75
+ // Non-streaming response
76
+ const text = await (result as Promise<string>);
77
+ assistantMsg.content = text;
78
+ messages = [...messages.slice(0, -1), { ...assistantMsg }];
79
+ scrollToBottom();
80
+ }
81
+ } catch (err: unknown) {
82
+ if (err instanceof Error && err.name === 'AbortError') return;
83
+ assistantMsg.content = t('chat.error') || 'Sorry, something went wrong. Please try again.';
84
+ messages = [...messages.slice(0, -1), { ...assistantMsg }];
85
+ } finally {
86
+ isStreaming = false;
87
+ abortController = null;
88
+ scrollToBottom();
89
+ }
90
+ }
91
+
92
+ function stopStreaming() {
93
+ if (abortController) {
94
+ abortController.abort();
95
+ isStreaming = false;
96
+ abortController = null;
97
+ }
98
+ }
99
+
100
+ function clearChat() {
101
+ messages = [];
102
+ isStreaming = false;
103
+ if (abortController) {
104
+ abortController.abort();
105
+ abortController = null;
106
+ }
107
+ }
108
+
109
+ function handleKeydown(e: KeyboardEvent) {
110
+ if (e.key === 'Enter' && !e.shiftKey) {
111
+ e.preventDefault();
112
+ sendMessage();
113
+ }
114
+ }
115
+
116
+ function handleGlobalKeydown(e: KeyboardEvent) {
117
+ if (e.ctrlKey && e.shiftKey && e.key === 'L') {
118
+ e.preventDefault();
119
+ if (open && !minimized) {
120
+ open = false;
121
+ } else {
122
+ open = true;
123
+ minimized = false;
124
+ }
125
+ }
126
+ }
127
+
128
+ /** Simple markdown→html: bold, italic, code blocks, inline code, line breaks */
129
+ function renderMarkdown(text: string): string {
130
+ return text
131
+ .replace(/```(\w*)\n([\s\S]*?)```/g, '<pre class="chat-code-block"><code>$2</code></pre>')
132
+ .replace(/`([^`]+)`/g, '<code class="chat-inline-code">$1</code>')
133
+ .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
134
+ .replace(/\*(.+?)\*/g, '<em>$1</em>')
135
+ .replace(/\n/g, '<br>');
136
+ }
137
+
138
+ const suggestions = $derived([
139
+ t('chat.suggestion1') || 'How do I create a new resource?',
140
+ t('chat.suggestion2') || 'Explain the data model',
141
+ t('chat.suggestion3') || 'Help me write a filter query',
142
+ ]);
143
+ </script>
144
+
145
+ <svelte:window onkeydown={handleGlobalKeydown} />
146
+
147
+ {#if provider}
148
+ <!-- FAB Button -->
149
+ {#if !open}
150
+ <div transition:scale={{ duration: 200 }}>
151
+ <TooltipButton
152
+ tooltip={t('chat.title') || 'AI Assistant'}
153
+ variant="default"
154
+ size="icon"
155
+ class="fixed bottom-6 right-6 z-[9998] h-12 w-12 rounded-full shadow-xl hover:shadow-2xl hover:scale-110 transition-all bg-primary text-primary-foreground"
156
+ onclick={() => { open = true; minimized = false; }}
157
+ >
158
+ <MessageCircle class="h-5 w-5" />
159
+ {#if messages.length > 0}
160
+ <span class="absolute -top-1 -right-1 h-4 w-4 rounded-full bg-destructive text-[10px] font-bold text-destructive-foreground flex items-center justify-center">
161
+ {messages.filter(m => m.role === 'assistant').length}
162
+ </span>
163
+ {/if}
164
+ </TooltipButton>
165
+ </div>
166
+ {/if}
167
+
168
+ <!-- Chat Panel -->
169
+ {#if open}
170
+ <div
171
+ class="fixed bottom-6 right-6 z-[9998] flex flex-col rounded-2xl border bg-card shadow-2xl overflow-hidden"
172
+ class:w-[400px]={!minimized}
173
+ class:h-[560px]={!minimized}
174
+ class:w-[280px]={minimized}
175
+ class:h-auto={minimized}
176
+ transition:fly={{ y: 300, duration: 250 }}
177
+ >
178
+ <!-- Header -->
179
+ <div class="flex items-center justify-between px-4 py-3 bg-primary text-primary-foreground shrink-0">
180
+ <div class="flex items-center gap-2.5">
181
+ <div class="flex items-center justify-center h-7 w-7 rounded-full bg-primary-foreground/20">
182
+ <Bot class="h-4 w-4" />
183
+ </div>
184
+ <div>
185
+ <h3 class="text-sm font-semibold leading-none">
186
+ {t('chat.title') || 'AI Assistant'}
187
+ </h3>
188
+ {#if isStreaming}
189
+ <p class="text-[10px] opacity-80 mt-0.5">{t('chat.typing') || 'Typing...'}</p>
190
+ {/if}
191
+ </div>
192
+ </div>
193
+ <div class="flex items-center gap-0.5">
194
+ {#if messages.length > 0}
195
+ <TooltipButton
196
+ tooltip={t('chat.clear') || 'Clear'}
197
+ variant="ghost"
198
+ size="icon"
199
+ class="h-7 w-7 text-primary-foreground/70 hover:text-primary-foreground hover:bg-primary-foreground/10"
200
+ onclick={clearChat}
201
+ >
202
+ <Trash2 class="h-3.5 w-3.5" />
203
+ </TooltipButton>
204
+ {/if}
205
+ <TooltipButton
206
+ tooltip={minimized ? (t('common.expand') || 'Expand') : (t('common.collapse') || 'Minimize')}
207
+ variant="ghost"
208
+ size="icon"
209
+ class="h-7 w-7 text-primary-foreground/70 hover:text-primary-foreground hover:bg-primary-foreground/10"
210
+ onclick={() => minimized = !minimized}
211
+ >
212
+ <Minus class="h-3.5 w-3.5" />
213
+ </TooltipButton>
214
+ <TooltipButton
215
+ tooltip={t('common.close') || 'Close'}
216
+ variant="ghost"
217
+ size="icon"
218
+ class="h-7 w-7 text-primary-foreground/70 hover:text-primary-foreground hover:bg-primary-foreground/10"
219
+ onclick={() => open = false}
220
+ >
221
+ <X class="h-3.5 w-3.5" />
222
+ </TooltipButton>
223
+ </div>
224
+ </div>
225
+
226
+ {#if !minimized}
227
+ <!-- Messages -->
228
+ <div
229
+ bind:this={messagesContainer}
230
+ class="flex-1 overflow-y-auto p-4 space-y-3 scroll-smooth"
231
+ >
232
+ {#if messages.length === 0}
233
+ <!-- Welcome state -->
234
+ <div class="flex flex-col items-center justify-center h-full text-center px-4" in:fade={{ duration: 200 }}>
235
+ <div class="h-12 w-12 rounded-full bg-primary/10 flex items-center justify-center mb-4">
236
+ <Bot class="h-6 w-6 text-primary" />
237
+ </div>
238
+ <h4 class="text-sm font-semibold text-foreground mb-1">
239
+ {t('chat.welcome') || 'How can I help?'}
240
+ </h4>
241
+ <p class="text-xs text-muted-foreground mb-4">
242
+ {t('chat.welcomeDesc') || 'Ask me anything about your admin panel.'}
243
+ </p>
244
+ <div class="flex flex-col gap-2 w-full max-w-[240px]">
245
+ {#each suggestions as suggestion}
246
+ <button
247
+ class="text-left text-xs px-3 py-2 rounded-lg border border-border hover:bg-accent hover:text-accent-foreground transition-colors cursor-pointer"
248
+ onclick={() => { inputValue = suggestion; }}
249
+ >
250
+ {suggestion}
251
+ </button>
252
+ {/each}
253
+ </div>
254
+ </div>
255
+ {:else}
256
+ {#each messages as msg (msg.id)}
257
+ <div
258
+ class="flex {msg.role === 'user' ? 'justify-end' : 'justify-start'}"
259
+ in:fly={{ y: 10, duration: 150 }}
260
+ >
261
+ <div
262
+ class="max-w-[85%] rounded-2xl px-3.5 py-2.5 text-sm leading-relaxed {msg.role === 'user'
263
+ ? 'bg-primary text-primary-foreground rounded-br-md'
264
+ : 'bg-muted text-foreground rounded-bl-md'}"
265
+ >
266
+ {#if msg.role === 'assistant'}
267
+ {#if msg.content}
268
+ <div class="chat-markdown">{@html renderMarkdown(msg.content)}</div>
269
+ {:else}
270
+ <div class="flex items-center gap-1.5 text-muted-foreground">
271
+ <Loader2 class="h-3.5 w-3.5 animate-spin" />
272
+ <span class="text-xs">{t('chat.thinking') || 'Thinking...'}</span>
273
+ </div>
274
+ {/if}
275
+ {:else}
276
+ {msg.content}
277
+ {/if}
278
+ </div>
279
+ </div>
280
+ {/each}
281
+ {/if}
282
+ </div>
283
+
284
+ <!-- Input area -->
285
+ <div class="shrink-0 border-t bg-card p-3">
286
+ <div class="flex items-end gap-2">
287
+ <textarea
288
+ bind:value={inputValue}
289
+ onkeydown={handleKeydown}
290
+ placeholder={t('chat.placeholder') || 'Type a message...'}
291
+ rows={1}
292
+ disabled={isStreaming}
293
+ class="flex-1 resize-none rounded-xl border border-input bg-background px-3 py-2.5 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1 disabled:opacity-50 max-h-[100px] min-h-[40px]"
294
+ ></textarea>
295
+ {#if isStreaming}
296
+ <Button
297
+ variant="destructive"
298
+ size="icon"
299
+ class="h-10 w-10 rounded-xl shrink-0"
300
+ onclick={stopStreaming}
301
+ >
302
+ <X class="h-4 w-4" />
303
+ </Button>
304
+ {:else}
305
+ <Button
306
+ variant="default"
307
+ size="icon"
308
+ class="h-10 w-10 rounded-xl shrink-0"
309
+ onclick={sendMessage}
310
+ disabled={!inputValue.trim()}
311
+ >
312
+ <Send class="h-4 w-4" />
313
+ </Button>
314
+ {/if}
315
+ </div>
316
+ <p class="text-[10px] text-muted-foreground mt-1.5 text-center">
317
+ <kbd class="px-1 py-0.5 rounded border border-border bg-muted text-[9px] font-mono">Ctrl+Shift+L</kbd>
318
+ {t('chat.shortcutHint') || 'to toggle'}
319
+ </p>
320
+ </div>
321
+ {/if}
322
+ </div>
323
+ {/if}
324
+ {/if}
325
+
326
+ <style>
327
+ :global(.chat-markdown pre.chat-code-block) {
328
+ background: hsl(var(--muted));
329
+ border-radius: 0.5rem;
330
+ padding: 0.75rem;
331
+ margin: 0.5rem 0;
332
+ overflow-x: auto;
333
+ font-size: 0.75rem;
334
+ line-height: 1.5;
335
+ }
336
+ :global(.chat-markdown code.chat-inline-code) {
337
+ background: hsl(var(--muted));
338
+ padding: 0.125rem 0.375rem;
339
+ border-radius: 0.25rem;
340
+ font-size: 0.8125em;
341
+ font-family: ui-monospace, monospace;
342
+ }
343
+ :global(.chat-markdown strong) {
344
+ font-weight: 600;
345
+ }
346
+ </style>
@@ -5,6 +5,7 @@
5
5
  import Toast from './Toast.svelte';
6
6
  import Header from './Header.svelte';
7
7
  import CommandPalette from './CommandPalette.svelte';
8
+ import ChatDialog from './ChatDialog.svelte';
8
9
  import TooltipButton from './TooltipButton.svelte';
9
10
  import { t } from '@svadmin/core/i18n';
10
11
  import { getAuthProvider } from '@svadmin/core';
@@ -124,6 +125,7 @@
124
125
  </div>
125
126
  </div>
126
127
  <CommandPalette bind:open={commandOpen} />
128
+ <ChatDialog />
127
129
  <Toast />
128
130
  {/if}
129
131
  </div>
@@ -2,7 +2,9 @@
2
2
  import { cn, type WithElementRef } from "../../../utils.js";
3
3
  import type { HTMLSelectAttributes } from "svelte/elements";
4
4
 
5
- type Props = WithElementRef<HTMLSelectAttributes, HTMLSelectElement> & {
5
+ // Use HTMLElement instead of HTMLSelectElement to avoid TS error:
6
+ // HTMLSelectElement.remove() returns void, incompatible with HTMLElement's Element return
7
+ type Props = WithElementRef<HTMLSelectAttributes, HTMLElement> & {
6
8
  placeholder?: string;
7
9
  };
8
10
 
package/src/index.ts CHANGED
@@ -46,7 +46,7 @@ export { default as StepsForm } from './components/StepsForm.svelte';
46
46
  export { default as InfiniteList } from './components/InfiniteList.svelte';
47
47
  export { default as ComboboxField } from './components/ComboboxField.svelte';
48
48
  export { default as PasswordInput } from './components/PasswordInput.svelte';
49
-
49
+ export { default as ChatDialog } from './components/ChatDialog.svelte';
50
50
  // Field display components
51
51
  export { default as NumberField } from './components/fields/NumberField.svelte';
52
52
  export { default as DateField } from './components/fields/DateField.svelte';