mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:35:24 +02:00
Sets up the project with Vite, React, Tailwind CSS, Gemini AI integration, and necessary dependencies for data analysis. Includes initial configuration for TypeScript, Tailwind, and project metadata.
84 lines
2.6 KiB
TypeScript
84 lines
2.6 KiB
TypeScript
|
|
import { SalesRecord } from '../types';
|
|
|
|
const DB_NAME = 'CrazeAnalytixDB';
|
|
const STORE_NAME = 'salesData';
|
|
const DB_VERSION = 1;
|
|
|
|
const initDB = (): Promise<IDBDatabase> => {
|
|
return new Promise((resolve, reject) => {
|
|
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
|
|
request.onupgradeneeded = (event) => {
|
|
const db = (event.target as IDBOpenDBRequest).result;
|
|
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
|
db.createObjectStore(STORE_NAME);
|
|
}
|
|
};
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
request.onerror = () => reject(request.error);
|
|
});
|
|
};
|
|
|
|
export const saveSalesData = async (data: SalesRecord[]): Promise<void> => {
|
|
try {
|
|
const db = await initDB();
|
|
return new Promise((resolve, reject) => {
|
|
const transaction = db.transaction(STORE_NAME, 'readwrite');
|
|
const store = transaction.objectStore(STORE_NAME);
|
|
|
|
// Store the data array
|
|
store.put(data, 'currentData');
|
|
// Store the timestamp
|
|
store.put(new Date().toISOString(), 'lastUpdated');
|
|
|
|
transaction.oncomplete = () => resolve();
|
|
transaction.onerror = () => reject(transaction.error);
|
|
});
|
|
} catch (error) {
|
|
console.error("Error saving to IndexedDB:", error);
|
|
// Fallback or silence error (data just won't be cached)
|
|
}
|
|
};
|
|
|
|
export const loadSalesData = async (): Promise<{ data: SalesRecord[]; lastUpdated: string | null }> => {
|
|
try {
|
|
const db = await initDB();
|
|
return new Promise((resolve, reject) => {
|
|
const transaction = db.transaction(STORE_NAME, 'readonly');
|
|
const store = transaction.objectStore(STORE_NAME);
|
|
|
|
const dataReq = store.get('currentData');
|
|
const dateReq = store.get('lastUpdated');
|
|
|
|
transaction.oncomplete = () => {
|
|
resolve({
|
|
data: dataReq.result || [],
|
|
lastUpdated: dateReq.result || null
|
|
});
|
|
};
|
|
|
|
transaction.onerror = () => reject(transaction.error);
|
|
});
|
|
} catch (error) {
|
|
console.error("Error loading from IndexedDB:", error);
|
|
return { data: [], lastUpdated: null };
|
|
}
|
|
};
|
|
|
|
export const clearSalesData = async (): Promise<void> => {
|
|
try {
|
|
const db = await initDB();
|
|
return new Promise((resolve, reject) => {
|
|
const transaction = db.transaction(STORE_NAME, 'readwrite');
|
|
const store = transaction.objectStore(STORE_NAME);
|
|
store.clear();
|
|
transaction.oncomplete = () => resolve();
|
|
transaction.onerror = () => reject(transaction.error);
|
|
});
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|