import { useState, useEffect } from 'react' import FeedSidebar from '../components/FeedSidebar' import FeedTopbar from '../components/FeedTopbar' import AccountSettings, { type SubscriptionState } from '../components/AccountSettings' import InterestPreferences from '../components/InterestPreferences' import SettingsTabs, { type SettingsTab } from '../components/SettingsTabs' import TrackedKeywords from '../components/TrackedKeywords' import './FeedPage.css' import './SettingsPage.css' import { fetchProfile, updateProfile } from '../services/apiService' type SettingsSnapshot = { selectedMacroTopics: string[] keywords: string[] subscriptionState: SubscriptionState } type ProfileIdentity = { username: string email: string } const STORAGE_KEY = 'briefai-settings' const ONBOARDING_KEY = 'briefai-onboarding' const DEFAULT_MACRO_TOPICS = ['Scienza & Ricerca'] function readInitialSettings(): SettingsSnapshot { if (typeof window === 'undefined') { return { selectedMacroTopics: DEFAULT_MACRO_TOPICS, keywords: [], subscriptionState: 'free', } } const onboardingSnapshot = readJSON<{ selectedTopics?: string[]; keywords?: string[] }>( ONBOARDING_KEY, ) const storedSnapshot = readJSON>(STORAGE_KEY) return { selectedMacroTopics: storedSnapshot?.selectedMacroTopics ?? onboardingSnapshot?.selectedTopics ?? DEFAULT_MACRO_TOPICS, keywords: storedSnapshot?.keywords ?? onboardingSnapshot?.keywords ?? [], subscriptionState: storedSnapshot?.subscriptionState ?? 'free', } } function readJSON(storageKey: string): T | undefined { try { const rawValue = window.localStorage.getItem(storageKey) if (!rawValue) { return undefined } return JSON.parse(rawValue) as T } catch { return undefined } } function persistSettings(snapshot: SettingsSnapshot) { window.localStorage.setItem(STORAGE_KEY, JSON.stringify(snapshot)) } // Pagina Impostazioni: gestisce preferenze feed e account con una struttura a tab. function SettingsPage() { const [activeTab, setActiveTab] = useState('interests') const [profileIdentity, setProfileIdentity] = useState({ username: '', email: '', }) const [selectedMacroTopics, setSelectedMacroTopics] = useState(() => readInitialSettings().selectedMacroTopics, ) const [keywords, setKeywords] = useState(() => readInitialSettings().keywords) const [keywordInput, setKeywordInput] = useState('') const [subscriptionState, setSubscriptionState] = useState( () => readInitialSettings().subscriptionState, ) useEffect(() => { fetchProfile() .then((res) => { if (res && res.profile) { setProfileIdentity({ username: res.profile.username || '', email: res.profile.email || '', }) setSelectedMacroTopics(res.profile.macroTopics || []) setKeywords(res.profile.keywords || []) } }) .catch(() => {}) }, []) const handleToggleMacroTopic = (topic: string) => { // Toggle semplice: se la categoria esiste la rimuove, altrimenti la aggiunge. setSelectedMacroTopics((currentTopics) => currentTopics.includes(topic) ? currentTopics.filter((savedTopic) => savedTopic !== topic) : [...currentTopics, topic], ) } const handleSaveMacroTopics = () => { updateProfile({ macroTopics: selectedMacroTopics }) .then((res) => { if (res && res.profile) { setSelectedMacroTopics(res.profile.macroTopics || selectedMacroTopics) } persistSettings({ selectedMacroTopics, keywords, subscriptionState }) }) .catch(() => { // fallback local persist persistSettings({ selectedMacroTopics, keywords, subscriptionState }) }) } const handleAddKeyword = (rawKeyword: string) => { const cleanedKeyword = rawKeyword.trim() // Evita duplicati e valori vuoti per mantenere la lista leggibile. if (!cleanedKeyword || keywords.includes(cleanedKeyword)) { return } setKeywords((currentKeywords) => [...currentKeywords, cleanedKeyword]) setKeywordInput('') } const handleRemoveKeyword = (keywordToRemove: string) => { setKeywords((currentKeywords) => currentKeywords.filter((savedKeyword) => savedKeyword !== keywordToRemove), ) } const handleSaveKeywords = () => { updateProfile({ keywords }) .then((res) => { if (res && res.profile) { setKeywords(res.profile.keywords || keywords) } persistSettings({ selectedMacroTopics, keywords, subscriptionState }) }) .catch(() => { persistSettings({ selectedMacroTopics, keywords, subscriptionState }) }) } const handleUpgrade = () => { const nextSubscriptionState: SubscriptionState = 'pro' setSubscriptionState(nextSubscriptionState) persistSettings({ selectedMacroTopics, keywords, subscriptionState: nextSubscriptionState }) } const handleCancelSubscription = () => { const nextSubscriptionState: SubscriptionState = 'free' setSubscriptionState(nextSubscriptionState) persistSettings({ selectedMacroTopics, keywords, subscriptionState: nextSubscriptionState }) } return (

Impostazioni

Gestisci preferenze e profilo

{activeTab === 'interests' ? (
) : ( )}
) } export default SettingsPage