2025-12-07 19:32:53 +02:00
|
|
|
import { useUIStore } from '@/stores/useUIStore';
|
|
|
|
|
|
|
|
|
|
export interface AppearancePreferences {
|
|
|
|
|
showReasoningTraces?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RawAppearancePayload = {
|
|
|
|
|
showReasoningTraces?: unknown;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sanitizePreferences = (payload?: RawAppearancePayload | null): AppearancePreferences | null => {
|
|
|
|
|
if (!payload || typeof payload !== 'object') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result: AppearancePreferences = {};
|
|
|
|
|
|
|
|
|
|
if (typeof payload.showReasoningTraces === 'boolean') {
|
|
|
|
|
result.showReasoningTraces = payload.showReasoningTraces;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.keys(result).length > 0 ? result : null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const extractRawAppearance = (data: unknown): RawAppearancePayload | null => {
|
|
|
|
|
if (!data || typeof data !== 'object') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const candidate = data as Record<string, unknown>;
|
|
|
|
|
const payload: RawAppearancePayload = {
|
|
|
|
|
showReasoningTraces: candidate.showReasoningTraces,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const saveAppearancePreferences = (preferences: AppearancePreferences): boolean => {
|
2026-02-05 01:59:49 +02:00
|
|
|
if (typeof window === 'undefined') {
|
2025-12-07 19:32:53 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-02-05 01:59:49 +02:00
|
|
|
localStorage.setItem('appearance-preferences', JSON.stringify(preferences));
|
2025-12-07 19:32:53 +02:00
|
|
|
return true;
|
2026-02-05 01:59:49 +02:00
|
|
|
} catch {
|
2025-12-07 19:32:53 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const applyAppearancePreferences = (preferences: AppearancePreferences): void => {
|
|
|
|
|
const store = useUIStore.getState();
|
|
|
|
|
|
|
|
|
|
if (typeof preferences.showReasoningTraces === 'boolean') {
|
|
|
|
|
store.setShowReasoningTraces(preferences.showReasoningTraces);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const loadAppearancePreferences = async (): Promise<AppearancePreferences | null> => {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stored = localStorage.getItem('appearance-preferences');
|
|
|
|
|
if (!stored) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(stored) as unknown;
|
|
|
|
|
const payload = extractRawAppearance(data);
|
|
|
|
|
return sanitizePreferences(payload);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to parse stored appearance preferences:', error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|