ollie-brandtech-strategic-i.../services/databaseService.ts
2026-02-09 07:31:16 -06:00

40 lines
No EOL
1.4 KiB
TypeScript

import { User, ChatSession } from '../types';
const STORAGE_KEYS = {
USERS: 'bt_db_users',
SESSIONS: 'bt_db_sessions',
CURRENT_USER: 'bt_session_user'
};
export const db = {
getUsers: (): User[] => JSON.parse(localStorage.getItem(STORAGE_KEYS.USERS) || '[]'),
saveUser: (user: User) => {
const users = db.getUsers();
users.push(user);
localStorage.setItem(STORAGE_KEYS.USERS, JSON.stringify(users));
},
getSessions: (userId: string): ChatSession[] => {
const allSessions: ChatSession[] = JSON.parse(localStorage.getItem(STORAGE_KEYS.SESSIONS) || '[]');
return allSessions.filter(s => s.userId === userId).sort((a, b) => b.timestamp - a.timestamp);
},
saveSession: (session: ChatSession) => {
const allSessions: ChatSession[] = JSON.parse(localStorage.getItem(STORAGE_KEYS.SESSIONS) || '[]');
const index = allSessions.findIndex(s => s.id === session.id);
if (index > -1) {
allSessions[index] = session;
} else {
allSessions.push(session);
}
localStorage.setItem(STORAGE_KEYS.SESSIONS, JSON.stringify(allSessions));
},
deleteSession: (sessionId: string) => {
const allSessions: ChatSession[] = JSON.parse(localStorage.getItem(STORAGE_KEYS.SESSIONS) || '[]');
const filtered = allSessions.filter(s => s.id !== sessionId);
localStorage.setItem(STORAGE_KEYS.SESSIONS, JSON.stringify(filtered));
}
};