Prima versione della web app BriefAI

This commit is contained in:
Diego-C-05
2026-05-05 10:24:46 +02:00
commit 2955588c13
73 changed files with 28223 additions and 0 deletions
@@ -0,0 +1,66 @@
const BASE = import.meta.env.VITE_API_URL as string
import { getAuthHeader } from './authService'
const authFetch = (path: string, options: RequestInit = {}) =>
fetch(`${BASE}${path}`, {
...options,
headers: { ...getAuthHeader(), ...options.headers },
})
// GET /api/stats/sentiment
export const fetchSentimentStats = () =>
authFetch('/api/stats/sentiment').then((r) => r.json())
// GET /api/stats/trending
export const fetchTrendingTopics = () =>
authFetch('/api/stats/trending').then((r) => r.json())
// GET /api/stats/categories
export const fetchCategoryStats = () =>
authFetch('/api/stats/categories').then((r) => r.json())
// GET /api/stats/sources
export const fetchSourceStats = () =>
authFetch('/api/stats/sources').then((r) => r.json())
// GET /api/stats/overview
export const fetchOverview = () =>
authFetch('/api/stats/overview').then((r) => r.json())
// GET /api/profile
export const fetchProfile = () =>
authFetch('/api/profile').then((r) => r.json())
// PUT /api/profile
export const updateProfile = async (data: {
userId?: string
macroTopics?: string[]
keywords?: string[]
}) => {
// Sync to backend
const backendRes = await authFetch('/api/profile', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then((r) => r.json());
// Opt-in sync to n8n if url is present and userId is known
const n8nUrl = import.meta.env.VITE_N8N_URL;
if (n8nUrl && data.userId && data.macroTopics) {
try {
await fetch(`${n8nUrl}/briefai/profile/update`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
userId: data.userId,
macroTopics: data.macroTopics,
keywords: data.keywords,
})
});
} catch (e) {
console.warn("Failed to sync profile to n8n webhook", e);
}
}
return backendRes;
}
@@ -0,0 +1,70 @@
const BASE = import.meta.env.VITE_API_URL as string;
// Salva il token dopo login o registrazione
const saveToken = (token: string) =>
localStorage.setItem('briefai_token', token);
// Restituisce l'header Authorization da aggiungere a ogni richiesta protetta
export const getAuthHeader = (): Record<string, string> => {
const token = localStorage.getItem('briefai_token');
return token ? { Authorization: `Bearer ${token}` } : {};
};
// Decodifica il payload JWT senza librerie esterne
export const decodeToken = (token: string) => {
try {
return JSON.parse(atob(token.split('.')[1]));
} catch {
return null;
}
};
// POST /api/auth/login
export const login = async (email: string, password: string) => {
const res = await fetch(`${BASE}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
if (!res.ok) throw await res.json();
const data = await res.json();
saveToken(data.token);
return data;
};
// POST /api/auth/register
export const register = async (payload: {
email: string;
password: string;
username: string;
preferences?: {
macroTopics?: string[] | undefined;
keywords?: string[] | undefined;
} | undefined;
}) => {
const res = await fetch(`${BASE}/api/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) throw await res.json();
const data = await res.json();
saveToken(data.token);
return data;
};
// GET /api/auth/me — verifica se il token è ancora valido
export const getMe = async () => {
const res = await fetch(`${BASE}/api/auth/me`, {
headers: getAuthHeader(),
});
if (!res.ok) {
localStorage.removeItem('briefai_token');
return null;
}
return res.json();
};
// Logout locale
export const logout = () =>
localStorage.removeItem('briefai_token');
@@ -0,0 +1,35 @@
import type { Article } from '../types/article'
import { getAuthHeader, decodeToken } from './authService'
const N8N = import.meta.env.VITE_N8N_URL
export const fetchPersonalizedFeed = async (): Promise<Article[]> => {
console.log("🚀 N8N URL:", N8N);
const token = localStorage.getItem('briefai_token')
if (!token) throw new Error('Non autenticato')
const payload = decodeToken(token)
const userId: string = payload?.userId
if (!userId) throw new Error('Token non valido')
const res = await fetch(`${N8N}/briefai/feed`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...getAuthHeader() },
body: JSON.stringify({ userId, limit: 20 }),
})
if (!res.ok) {
const status = res.status
const statusText = res.statusText
const errorText = await res.text().catch(() => 'non leggibile')
console.error('[FeedService] HTTP Error:', { status, statusText, errorText })
throw new Error(`Errore nel recupero del feed: ${status} ${statusText}`)
}
const data = await res.json()
console.log('[FeedService] Response:', data)
const articles = (data.articles ?? []).map((a: any) => {
const idVal = a.uniqueKey ?? a.id ?? a._id ?? ''
const uniqueKey = typeof idVal === 'object' ? String(idVal) : idVal
return { ...a, uniqueKey }
})
return articles
}
@@ -0,0 +1,21 @@
const N8N = import.meta.env.VITE_N8N_URL
export const sendFeedback = async (
articleId: string,
vote: 1 | -1
): Promise<void> => {
const token = localStorage.getItem('briefai_token')
if (!token) return
const payload = JSON.parse(atob(token.split('.')[1]))
await fetch(`${N8N}/briefai/feedback`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
userId: payload.userId,
articleId,
vote,
}),
})
}