1
0
Fork 0
zoomaccel/src/utils.ts

82 lines
No EOL
2.3 KiB
TypeScript

import { Round, Stats, AccelerationSettings } from './types';
interface AppState {
rounds: Round[];
stats: Stats;
settings: AccelerationSettings;
}
// Persist app state to localStorage
export const saveState = (state: AppState) => {
try {
localStorage.setItem('zoomaccel_state', JSON.stringify(state));
} catch (error) {
console.error('Failed to save state:', error);
}
};
export const loadState = (): AppState | null => {
try {
const savedState = localStorage.getItem('zoomaccel_state');
if (savedState) {
return JSON.parse(savedState);
}
} catch (error) {
console.error('Failed to load state:', error);
}
return null;
};
export const exportToCSV = (rounds: Round[]) => {
const baseHeaders = ['Round Number', 'Time (ms)', 'Misclicks', 'Acceleration Enabled', 'Timestamp'];
// Figure out how many acceleration points we need
const maxCurvePoints = rounds.reduce((max, round) => {
return Math.max(max, round.accelerationCurve?.length || 0);
}, 0);
const curveHeaders = maxCurvePoints > 0
? Array.from({ length: maxCurvePoints }, (_, i) => `Accel ${i + 1}`)
: [];
const headers = [...baseHeaders, ...curveHeaders];
const rows = rounds.map((round, index) => {
const baseData = [
index + 1,
round.timeSpent,
round.misclicks,
round.accelerationEnabled ? 'true' : 'false',
new Date(round.timestamp).toISOString()
];
const curveData = maxCurvePoints > 0
? Array.from({ length: maxCurvePoints }, (_, i) =>
round.accelerationCurve?.[i]?.toFixed(2) || '')
: [];
return [...baseData, ...curveData];
});
const csvContent = [
headers.join(','),
...rows.map(row => row.join(','))
].join('\n');
// Download the file
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
const now = new Date();
const dateStr = now.toISOString().split('T')[0];
const timeStr = now.toTimeString().split(' ')[0].replace(/:/g, '-');
link.setAttribute('href', url);
link.setAttribute('download', `zoom_trials_${dateStr}_${timeStr}.csv`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};