mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add svelte
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, nextTick } from 'vue'
|
||||
import { useChat } from '@ai-sdk/vue'
|
||||
import { nextTick, ref, watch } from 'vue'
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const serverUrl = config.public.serverURL
|
||||
@@ -16,7 +16,6 @@ watch(messages, async () => {
|
||||
messagesEndRef.value?.scrollIntoView({ behavior: 'smooth' })
|
||||
})
|
||||
|
||||
// Helper: Concatenate all text parts for a message
|
||||
function getMessageText(message: any) {
|
||||
return message.parts
|
||||
.filter((part: any) => part.type === 'text')
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
<script lang="ts">
|
||||
import { PUBLIC_SERVER_URL } from '$env/static/public';
|
||||
import { Chat } from '@ai-sdk/svelte';
|
||||
|
||||
const chat = new Chat({
|
||||
api: `${PUBLIC_SERVER_URL}/ai`,
|
||||
});
|
||||
|
||||
let messagesEndElement: HTMLDivElement | null = null;
|
||||
|
||||
$effect(() => {
|
||||
const messageCount = chat.messages.length;
|
||||
if (messageCount > 0) {
|
||||
setTimeout(() => {
|
||||
messagesEndElement?.scrollIntoView({ behavior: 'smooth' });
|
||||
}, 0);
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<div class="mx-auto grid h-full w-full max-w-2xl grid-rows-[1fr_auto] overflow-hidden p-4">
|
||||
<div class="mb-4 space-y-4 overflow-y-auto pb-4">
|
||||
{#if chat.messages.length === 0}
|
||||
<div class="mt-8 text-center text-neutral-500">Ask the AI anything to get started!</div>
|
||||
{/if}
|
||||
|
||||
{#each chat.messages as message (message.id)}
|
||||
<div
|
||||
class="w-fit max-w-[85%] rounded-lg p-3 text-sm md:text-base"
|
||||
class:ml-auto={message.role === 'user'}
|
||||
class:bg-indigo-600={message.role === 'user'}
|
||||
class:text-white={message.role === 'user'}
|
||||
class:bg-neutral-700={message.role === 'assistant'}
|
||||
class:text-neutral-100={message.role === 'assistant'}
|
||||
>
|
||||
<p
|
||||
class="mb-1 text-xs font-semibold uppercase tracking-wide"
|
||||
class:text-indigo-200={message.role === 'user'}
|
||||
class:text-neutral-400={message.role === 'assistant'}
|
||||
>
|
||||
{message.role === 'user' ? 'You' : 'AI Assistant'}
|
||||
</p>
|
||||
<div class="whitespace-pre-wrap break-words">
|
||||
{#each message.parts as part, partIndex (partIndex)}
|
||||
{#if part.type === 'text'}
|
||||
{part.text}
|
||||
{:else if part.type === 'tool-invocation'}
|
||||
<pre class="mt-2 rounded bg-neutral-800 p-2 text-xs text-neutral-300"
|
||||
>{JSON.stringify(part.toolInvocation, null, 2)}</pre
|
||||
>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
<div bind:this={messagesEndElement}></div>
|
||||
</div>
|
||||
|
||||
<form
|
||||
onsubmit={chat.handleSubmit}
|
||||
class="flex w-full items-center space-x-2 border-t border-neutral-700 pt-4"
|
||||
>
|
||||
<input
|
||||
name="prompt"
|
||||
bind:value={chat.input}
|
||||
placeholder="Type your message..."
|
||||
class="flex-1 rounded border border-neutral-600 bg-neutral-800 px-3 py-2 text-neutral-100 placeholder-neutral-500 focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 disabled:opacity-50"
|
||||
autocomplete="off"
|
||||
onkeydown={(e) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
chat.handleSubmit(e);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!chat.input.trim()}
|
||||
class="inline-flex h-10 w-10 items-center justify-center rounded bg-indigo-600 text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:ring-offset-neutral-900 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
aria-label="Send message"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="m22 2-7 20-4-9-9-4Z" /><path d="M22 2 11 13" />
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,150 @@
|
||||
<script lang="ts">
|
||||
import { orpc } from '$lib/orpc';
|
||||
import { createQuery, createMutation } from '@tanstack/svelte-query';
|
||||
|
||||
let newTodoText = $state('');
|
||||
|
||||
const todosQuery = createQuery(orpc.todo.getAll.queryOptions());
|
||||
|
||||
const addMutation = createMutation(
|
||||
orpc.todo.create.mutationOptions({
|
||||
onSuccess: () => {
|
||||
$todosQuery.refetch();
|
||||
newTodoText = '';
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to create todo:', error?.message ?? error);
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const toggleMutation = createMutation(
|
||||
orpc.todo.toggle.mutationOptions({
|
||||
onSuccess: () => {
|
||||
$todosQuery.refetch();
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to toggle todo:', error?.message ?? error);
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const deleteMutation = createMutation(
|
||||
orpc.todo.delete.mutationOptions({
|
||||
onSuccess: () => {
|
||||
$todosQuery.refetch();
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to delete todo:', error?.message ?? error);
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
function handleAddTodo(event: SubmitEvent) {
|
||||
event.preventDefault();
|
||||
const text = newTodoText.trim();
|
||||
if (text) {
|
||||
$addMutation.mutate({ text });
|
||||
}
|
||||
}
|
||||
|
||||
function handleToggleTodo(id: number, completed: boolean) {
|
||||
$toggleMutation.mutate({ id, completed: !completed });
|
||||
}
|
||||
|
||||
function handleDeleteTodo(id: number) {
|
||||
$deleteMutation.mutate({ id });
|
||||
}
|
||||
|
||||
const isAdding = $derived($addMutation.isPending);
|
||||
const canAdd = $derived(!isAdding && newTodoText.trim().length > 0);
|
||||
const isLoadingTodos = $derived($todosQuery.isLoading);
|
||||
const todos = $derived($todosQuery.data ?? []);
|
||||
const hasTodos = $derived(todos.length > 0);
|
||||
|
||||
</script>
|
||||
|
||||
<div class="p-4">
|
||||
<h1 class="text-xl mb-4">Todos</h1>
|
||||
|
||||
<form onsubmit={handleAddTodo} class="flex gap-2 mb-4">
|
||||
<input
|
||||
type="text"
|
||||
bind:value={newTodoText}
|
||||
placeholder="New task..."
|
||||
disabled={isAdding}
|
||||
class=" p-1 flex-grow"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!canAdd}
|
||||
class="bg-blue-500 text-white px-3 py-1 rounded disabled:opacity-50"
|
||||
>
|
||||
{#if isAdding}Adding...{:else}Add{/if}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{#if isLoadingTodos}
|
||||
<p>Loading...</p>
|
||||
{:else if !hasTodos}
|
||||
<p>No todos yet.</p>
|
||||
{:else}
|
||||
<ul class="space-y-1">
|
||||
{#each todos as todo (todo.id)}
|
||||
{@const isToggling = $toggleMutation.isPending && $toggleMutation.variables?.id === todo.id}
|
||||
{@const isDeleting = $deleteMutation.isPending && $deleteMutation.variables?.id === todo.id}
|
||||
{@const isDisabled = isToggling || isDeleting}
|
||||
<li
|
||||
class="flex items-center justify-between p-2 "
|
||||
class:opacity-50={isDisabled}
|
||||
>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id={`todo-${todo.id}`}
|
||||
checked={todo.completed}
|
||||
onchange={() => handleToggleTodo(todo.id, todo.completed)}
|
||||
disabled={isDisabled}
|
||||
/>
|
||||
<label
|
||||
for={`todo-${todo.id}`}
|
||||
class:line-through={todo.completed}
|
||||
>
|
||||
{todo.text}
|
||||
</label>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => handleDeleteTodo(todo.id)}
|
||||
disabled={isDisabled}
|
||||
aria-label="Delete todo"
|
||||
class="text-red-500 px-1 disabled:opacity-50"
|
||||
>
|
||||
{#if isDeleting}Deleting...{:else}X{/if}
|
||||
</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
||||
{#if $todosQuery.isError}
|
||||
<p class="mt-4 text-red-500">
|
||||
Error loading: {$todosQuery.error?.message ?? 'Unknown error'}
|
||||
</p>
|
||||
{/if}
|
||||
{#if $addMutation.isError}
|
||||
<p class="mt-4 text-red-500">
|
||||
Error adding: {$addMutation.error?.message ?? 'Unknown error'}
|
||||
</p>
|
||||
{/if}
|
||||
{#if $toggleMutation.isError}
|
||||
<p class="mt-4 text-red-500">
|
||||
Error updating: {$toggleMutation.error?.message ?? 'Unknown error'}
|
||||
</p>
|
||||
{/if}
|
||||
{#if $deleteMutation.isError}
|
||||
<p class="mt-4 text-red-500">
|
||||
Error deleting: {$deleteMutation.error?.message ?? 'Unknown error'}
|
||||
</p>
|
||||
{/if}
|
||||
</div>
|
||||
Reference in New Issue
Block a user