chore init
This commit is contained in:
1 parent
1b7406b1d3
commit
a0d312a884
60 files changed
+5046
-125
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
createDefaultCVData,
|
||||
type CVCareerData,
|
||||
} from "@/lib/types/cv";
|
||||
|
||||
export const STORAGE_KEY = "cv-maker:v1";
|
||||
|
||||
export interface CVStorageAdapter {
|
||||
load(): CVCareerData | null;
|
||||
save(data: CVCareerData): void;
|
||||
clear(): void;
|
||||
}
|
||||
|
||||
export function isCVCareerData(value: unknown): value is CVCareerData {
|
||||
if (typeof value !== "object" || value === null) return false;
|
||||
const data = value as Record<string, unknown>;
|
||||
if (data.version !== 1) return false;
|
||||
if (!Array.isArray(data.experiences)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function normalizeCVCareerData(value: unknown): CVCareerData | null {
|
||||
if (!isCVCareerData(value)) return null;
|
||||
return { ...createDefaultCVData(), ...value, experiences: value.experiences };
|
||||
}
|
||||
|
||||
export function createLocalStorageAdapter(): CVStorageAdapter {
|
||||
return {
|
||||
load() {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(STORAGE_KEY);
|
||||
if (raw === null) return null;
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
return normalizeCVCareerData(parsed);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
save(data) {
|
||||
try {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
|
||||
} catch (error) {
|
||||
console.warn("Échec de la sauvegarde localStorage", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
clear() {
|
||||
window.localStorage.removeItem(STORAGE_KEY);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const localStorageAdapter = createLocalStorageAdapter();
|
||||
Reference in new issue
Block a user