feat: simulation state management (React Context) and sharing/export

This commit is contained in:
2026-02-22 20:00:33 +01:00
parent 87370d8fad
commit 7defeb87b3
2 changed files with 249 additions and 0 deletions

57
src/lib/sharing.ts Normal file
View File

@@ -0,0 +1,57 @@
import type { SimulationState } from './types';
/**
* Encode simulation state into a shareable URL hash.
* Uses lz-string for compression.
*/
export async function encodeState(state: SimulationState): Promise<string> {
const { compressToEncodedURIComponent } = await import('lz-string');
const json = JSON.stringify(state);
return compressToEncodedURIComponent(json);
}
/**
* Decode simulation state from a URL hash.
*/
export async function decodeState(hash: string): Promise<SimulationState | null> {
try {
const { decompressFromEncodedURIComponent } = await import('lz-string');
const json = decompressFromEncodedURIComponent(hash);
if (!json) return null;
return JSON.parse(json) as SimulationState;
} catch {
return null;
}
}
/**
* Export yearly data as CSV.
*/
export function exportCSV(headers: string[], rows: (string | number)[][]): void {
const bom = '\uFEFF'; // UTF-8 BOM for Excel
const sep = ';';
const lines = [
headers.join(sep),
...rows.map((row) => row.map((v) => (typeof v === 'number' ? v.toFixed(2) : v)).join(sep)),
];
const csv = bom + lines.join('\n');
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `simulation-pret-immo-${new Date().toISOString().slice(0, 10)}.csv`;
a.click();
URL.revokeObjectURL(url);
}
/**
* Copy text to clipboard.
*/
export async function copyToClipboard(text: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}