diff --git a/src/services/clean-html.ts b/src/services/clean-html.ts new file mode 100644 index 0000000..2a3faa3 --- /dev/null +++ b/src/services/clean-html.ts @@ -0,0 +1,24 @@ +export function stripHtml(html: string): string { + if (!html) return '' + return html + .replace(/<[^>]*>/g, '') + .replace(/ /g, ' ') + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(/\s+/g, ' ') + .trim() +} + +export function extractFirstSentence(text: string, maxLen = 200): string { + const cleaned = stripHtml(text) + const match = cleaned.match(/^[^.!?]+[.!?]/) + if (match) return match[0].slice(0, maxLen) + return cleaned.slice(0, maxLen) +} + +export function stripHtmlTags(html: string): string { + return stripHtml(html) +} diff --git a/src/services/tauri-db.ts b/src/services/tauri-db.ts index 9620692..881119d 100644 --- a/src/services/tauri-db.ts +++ b/src/services/tauri-db.ts @@ -1,5 +1,15 @@ import { invoke } from '@tauri-apps/api/core' +export interface ProjectRecord { + id: number + name: string + key: string | null + description: string | null + status: string + start_date: string | null + end_date: string | null +} + export interface AlphaUserRecord { id: number email: string @@ -70,6 +80,17 @@ export interface PerformanceRecord { } export const tauriDb = { + // Projects + getProjects(): Promise { + return invoke('get_projects') + }, + saveProject(project: ProjectRecord): Promise { + return invoke('save_project', { project }) + }, + deleteProject(id: number): Promise { + return invoke('delete_project', { id }) + }, + // Users getUsers(): Promise { return invoke('get_users') diff --git a/src/stores/projects.ts b/src/stores/projects.ts index b57c216..285608a 100644 --- a/src/stores/projects.ts +++ b/src/stores/projects.ts @@ -1,6 +1,8 @@ import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { kappa } from '@/services/kappa-api' +import { tauriDb } from '@/services/tauri-db' +import { stripHtml } from '@/services/clean-html' import type { KappaInitiative } from '@/types/kappa' export const useProjectsStore = defineStore('projects', () => { @@ -20,8 +22,16 @@ export const useProjectsStore = defineStore('projects', () => { error.value = null try { const data = await kappa.getInitiatives() as KappaInitiative[] | { results?: KappaInitiative[] } - console.log('[KAPPA] initiatives:', data) - projects.value = Array.isArray(data) ? data : (data.results ?? []) + const raw = Array.isArray(data) ? data : (data.results ?? []) + + projects.value = raw.map(p => ({ + ...p, + description: stripHtml(p.description || ''), + name: p.name || p.initiative_name || `Proyecto ${p.id}`, + initiative_name: p.initiative_name || p.name, + })) + + syncToTurso(projects.value) } catch (e: any) { error.value = e.message } finally { @@ -29,6 +39,20 @@ export const useProjectsStore = defineStore('projects', () => { } } + async function syncToTurso(list: KappaInitiative[]) { + for (const p of list) { + await tauriDb.saveProject({ + id: p.id, + name: p.initiative_name || p.name || `Proyecto ${p.id}`, + key: p.key ?? null, + description: p.description ?? null, + status: p.status || 'active', + start_date: p.start_date ?? null, + end_date: p.end_date ?? null, + }).catch(() => {}) + } + } + function select(id: number) { selectedId.value = id localStorage.setItem('kappa_last_project', String(id))