Migrate frontend from Thymeleaf and basic JS to Vue.js application

This commit is contained in:
2025-07-19 20:30:53 +02:00
parent dc6314384d
commit 84cbf9be89
34 changed files with 1988 additions and 0 deletions

16
frontend/src/App.vue Normal file
View File

@@ -0,0 +1,16 @@
<template>
<div id="app">
<ChatLayout />
</div>
</template>
<script setup>
import ChatLayout from './components/ChatLayout.vue'
</script>
<style>
#app {
height: 100vh;
width: 100vw;
}
</style>

View File

@@ -0,0 +1,36 @@
/* Base y fuente */
html,
body {
margin: 0;
padding: 0;
height: 100%;
background: #121217;
color: #e0e4e8;
font-family: "Segoe UI Variable", "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
margin: 0;
padding: 0;
height: 100vh;
background-color: #121217;
}
/* Responsive */
@media (max-width: 768px) {
.layout-container {
flex-direction: column;
}
.sidebar {
width: 100% !important;
border-right: none;
border-bottom: 1px solid #2c2e34;
}
.main-content {
width: 100% !important;
}
}

View File

@@ -0,0 +1,116 @@
<template>
<form class="chat-form" @submit.prevent="handleSubmit">
<textarea
v-model="prompt"
aria-label="Pregunta del usuario"
placeholder="Escribe tu mensaje..."
autocomplete="off"
required
:disabled="disabled"
@keydown.enter.exact.prevent="handleSubmit"
@keydown.enter.shift.exact="addNewLine"
></textarea>
<button
type="submit"
:disabled="disabled || !prompt.trim()"
>
Enviar
</button>
</form>
</template>
<script setup>
import { ref } from 'vue'
defineProps({
disabled: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['send-message'])
const prompt = ref('')
const handleSubmit = () => {
if (!prompt.value.trim()) return
emit('send-message', prompt.value.trim())
prompt.value = ''
}
const addNewLine = () => {
prompt.value += '\n'
}
</script>
<style scoped>
.chat-form {
flex-shrink: 0;
margin-top: 1.25rem;
display: flex;
gap: 12px;
padding: 0;
box-sizing: border-box;
background: transparent;
border-radius: 0;
}
textarea {
flex-grow: 1;
min-height: 4.25rem;
border-radius: 16px;
border: 1px solid #2d2f36;
padding: 1rem 1.25rem;
font-size: 1rem;
resize: none;
outline: none;
background-color: #1c1e24;
color: #e6e9ef;
font-family: inherit;
max-width: 100%;
overflow-y: auto;
box-sizing: border-box;
transition: border-color 0.25s ease, background-color 0.25s ease;
}
textarea::placeholder {
color: #6a6e7c;
font-style: italic;
opacity: 0.9;
}
textarea:focus {
border-color: #5a90ff;
background-color: #20232a;
}
button {
background-color: #5a90ff;
border: none;
border-radius: 14px;
color: #ffffff;
padding: 0 1.75rem;
font-weight: 600;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.25s ease, transform 0.1s ease;
white-space: nowrap;
}
button:hover:not(:disabled) {
background-color: #4076e0;
}
button:active:not(:disabled) {
transform: translateY(1px);
background-color: #305dc0;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
background-color: #3a4a6a;
}
</style>

View File

@@ -0,0 +1,145 @@
<template>
<div class="layout-container">
<!-- Menú lateral izquierdo -->
<ChatSidebar
:chats="chats"
:current-chat-id="chatId"
@select-chat="selectChat"
@create-chat="createNewChat"
/>
<!-- Área principal del chat -->
<ChatMain
:messages="messages"
:is-loading="isLoading"
@send-message="sendMessage"
/>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import ChatSidebar from './ChatSidebar.vue'
import ChatMain from './ChatMain.vue'
import { chatService } from '../services/chatService.js'
// Estado reactivo
const chatId = ref('')
const chats = ref([])
const messages = ref([])
const isLoading = ref(false)
// Cargar historial de chats
const loadHistory = async () => {
try {
const data = await chatService.getChats()
chats.value = data
// Autoabrir primer chat si no hay chatId activo
if (!chatId.value && data.length > 0) {
chatId.value = data[0].id
await loadMessages(chatId.value)
}
} catch (error) {
console.error("Error cargando historial:", error)
}
}
// Cargar mensajes de un chat específico
const loadMessages = async (selectedChatId) => {
try {
const data = await chatService.getChatMessages(selectedChatId)
messages.value = data
} catch (error) {
console.error("Error al cargar mensajes:", error)
messages.value.push({
role: "bot",
text: "❌ Error cargando mensajes del chat.",
date: new Date().toISOString()
})
}
}
// Seleccionar un chat
const selectChat = async (selectedId) => {
if (selectedId !== chatId.value) {
chatId.value = selectedId
await loadMessages(chatId.value)
}
}
// Crear nuevo chat
const createNewChat = async () => {
try {
const newChatId = await chatService.createChat()
chatId.value = newChatId
await loadHistory()
await loadMessages(chatId.value)
} catch (error) {
console.error("Error al crear nuevo chat:", error)
if (messages.value) {
messages.value.push({
role: "bot",
text: "❌ Error creando nuevo chat.",
date: new Date().toISOString()
})
}
}
}
// Enviar mensaje
const sendMessage = async (prompt) => {
// Crear nuevo chat si no existe
if (!chatId.value) {
await createNewChat()
}
// Agregar mensaje del usuario inmediatamente
const userMessage = {
role: "user",
text: prompt,
date: new Date().toISOString()
}
messages.value.push(userMessage)
isLoading.value = true
try {
const data = await chatService.sendMessage(chatId.value, prompt)
chatId.value = data.chatId
// Agregar respuesta del bot
const botMessage = {
role: "bot",
text: data.text,
date: data.date
}
messages.value.push(botMessage)
await loadHistory()
} catch (error) {
console.error("Error enviando mensaje:", error)
messages.value.push({
role: "bot",
text: "❌ Error procesando la respuesta.",
date: new Date().toISOString()
})
} finally {
isLoading.value = false
}
}
// Inicializar al montar el componente
onMounted(async () => {
await loadHistory()
})
</script>
<style scoped>
.layout-container {
display: flex;
height: 100vh;
width: 100vw;
overflow: hidden;
}
</style>

View File

@@ -0,0 +1,56 @@
<template>
<main class="main-content">
<h1 class="main-title">🤖 Chat IA Offline</h1>
<ChatMessages
:messages="messages"
:is-loading="isLoading"
/>
<ChatForm @send-message="handleSendMessage" :disabled="isLoading" />
</main>
</template>
<script setup>
import ChatMessages from './ChatMessages.vue'
import ChatForm from './ChatForm.vue'
defineProps({
messages: {
type: Array,
required: true
},
isLoading: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['send-message'])
const handleSendMessage = (message) => {
emit('send-message', message)
}
</script>
<style scoped>
.main-content {
width: 75%;
box-sizing: border-box;
display: flex;
flex-direction: column;
padding: 2rem 1.25rem 1rem;
background-color: #121217;
overflow: hidden;
}
.main-title {
text-align: center;
margin-bottom: 1.25rem;
color: #5a90ff;
font-weight: 700;
font-size: 1.75rem;
letter-spacing: 0.05em;
user-select: none;
}
</style>

View File

@@ -0,0 +1,143 @@
<template>
<div class="chat-log" ref="chatLog">
<div
v-for="(msg, index) in messages"
:key="index"
:class="['bubble', msg.role]"
v-show="msg.text"
>
<span>{{ msg.text }}</span>
<em class="timestamp">{{ formatDate(msg.date) }}</em>
</div>
</div>
<div v-if="isLoading" class="spinner"></div>
<div v-if="isLoading" class="thinking-text">Pensando...</div>
</template>
<script setup>
import { ref, watch, nextTick } from 'vue'
import { dateUtils } from '../utils/dateUtils.ts'
const props = defineProps({
messages: {
type: Array,
required: true
},
isLoading: {
type: Boolean,
default: false
}
})
const chatLog = ref(null)
// Scroll al final cuando se agregan mensajes
watch(() => props.messages.length, async () => {
await nextTick()
if (chatLog.value) {
chatLog.value.scrollTop = chatLog.value.scrollHeight
}
})
const formatDate = (date) => {
return dateUtils.formatDate(date)
}
</script>
<style scoped>
.chat-log {
flex: 1 1 auto;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 12px;
padding: 1.25rem 1.5rem;
box-sizing: border-box;
background-color: #1a1c22;
border-radius: 16px;
scrollbar-width: thin;
scrollbar-color: #5a90ff33 transparent;
}
.chat-log::-webkit-scrollbar {
width: 6px;
}
.chat-log::-webkit-scrollbar-thumb {
background-color: #5a90ff55;
border-radius: 3px;
}
.bubble {
max-width: 75%;
padding: 14px 18px;
border-radius: 20px;
line-height: 1.5;
font-size: 1rem;
word-wrap: break-word;
user-select: text;
transition: background-color 0.3s ease, color 0.3s ease;
opacity: 0;
transform: translateY(10px);
animation: slideFadeIn 0.3s forwards;
}
@keyframes slideFadeIn {
to {
opacity: 1;
transform: translateY(0);
}
}
.user {
background-color: #3451d1;
align-self: flex-end;
color: #ffffff;
border-radius: 20px 20px 4px 20px;
}
.bot {
background-color: #2a2d35;
align-self: flex-start;
color: #a4c8ff;
font-style: italic;
border-radius: 20px 20px 20px 4px;
}
.timestamp {
display: block;
text-align: right;
font-size: 0.75rem;
color: #999;
margin-top: 6px;
font-style: normal;
opacity: 0.7;
user-select: none;
}
.spinner {
margin: 1rem auto;
width: 36px;
height: 36px;
border: 4px solid rgba(90, 144, 255, 0.2);
border-top-color: #5a90ff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
.thinking-text {
text-align: center;
color: #5a90ff;
font-style: italic;
margin-top: 0.5rem;
font-weight: 600;
user-select: none;
}
</style>

View File

@@ -0,0 +1,95 @@
<template>
<aside class="sidebar">
<h2>🗂 Chats</h2>
<button class="new-chat-btn" @click="$emit('create-chat')">
Nuevo chat
</button>
<ul class="chat-list">
<li
v-for="chat in chats"
:key="chat.id"
:class="{ active: chat.id === currentChatId }"
@click="$emit('select-chat', chat.id)"
>
{{ chat.name }}
</li>
</ul>
</aside>
</template>
<script setup>
defineProps({
chats: {
type: Array,
required: true
},
currentChatId: {
type: String,
default: ''
}
})
defineEmits(['select-chat', 'create-chat'])
</script>
<style scoped>
.sidebar {
width: 25%;
min-width: 200px;
background-color: #1b1d23;
color: #ffffff;
padding: 1.5rem 1rem;
box-sizing: border-box;
border-right: 1px solid #2c2e34;
display: flex;
flex-direction: column;
}
.sidebar h2 {
font-size: 1.25rem;
margin-bottom: 1rem;
color: #5a90ff;
}
.new-chat-btn {
margin-bottom: 10px;
padding: 8px;
font-size: 14px;
cursor: pointer;
background-color: #5a90ff;
color: white;
border: none;
border-radius: 8px;
transition: background-color 0.2s ease;
}
.new-chat-btn:hover {
background-color: #4076e0;
}
.chat-list {
list-style: none;
padding: 0;
margin: 0;
overflow-y: auto;
flex-grow: 1;
}
.chat-list li {
padding: 0.5rem 0.75rem;
cursor: pointer;
border-radius: 8px;
transition: background-color 0.2s ease;
font-size: 0.95rem;
}
.chat-list li:hover {
background-color: #2c2f3a;
}
.chat-list li.active {
background-color: #3451d1;
color: white;
font-weight: bold;
}
</style>

5
frontend/src/main.ts Normal file
View File

@@ -0,0 +1,5 @@
import { createApp } from "vue"
import App from "./App.vue"
import "./assets/styles.css"
createApp(App).mount("#app")

View File

@@ -0,0 +1,63 @@
class ChatService {
async getChats() {
try {
const response = await fetch("/api/v1/chats", { method: "GET" })
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error("Error fetching chats:", error)
throw error
}
}
async createChat() {
try {
const response = await fetch("/api/v1/chats", {
method: "POST",
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.text()
} catch (error) {
console.error("Error creating chat:", error)
throw error
}
}
async getChatMessages(chatId) {
try {
const response = await fetch(`/api/v1/chats/${chatId}`)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error("Error fetching messages:", error)
throw error
}
}
async sendMessage(chatId, prompt) {
try {
const response = await fetch(`/api/v1/chats/${chatId}`, {
method: "PUT",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({ prompt }),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error("Error sending message:", error)
throw error
}
}
}
export const chatService = new ChatService()

View File

@@ -0,0 +1,14 @@
export const dateUtils = {
formatDate(date) {
if (!date) return ""
const d = new Date(date)
const day = d.getDate().toString().padStart(2, "0")
const month = (d.getMonth() + 1).toString().padStart(2, "0")
const year = d.getFullYear()
const hours = d.getHours().toString().padStart(2, "0")
const minutes = d.getMinutes().toString().padStart(2, "0")
return `${day}/${month}/${year} ${hours}:${minutes}`
},
}