import { create } from 'zustand'; import { APP_CONFIG } from '@/config/app'; import { fetchAppConfig } from '@/services/shareApi'; interface AppConfigState { name: string; maxNodes: number; maxMapSizeMb: number; maxShareSizeMb: number; /** * Le partage serveur est-il ouvert sur cette instance ? * Faux tant que le backend n'a pas repondu : mieux vaut ne pas proposer une * action qui echouerait que l'inverse. */ sharingEnabled: boolean; allowPermanentShares: boolean; defaultShareExpiryDays: number; shareExpiryOptions: Array; /** Faux tant que le backend n'a pas repondu : l'app reste utilisable hors ligne. */ serverReachable: boolean; load: () => Promise; } const DEFAULT_EXPIRY_OPTIONS: Array = [1, 7, 30, 90, null]; /** * Le nom du produit vient de la configuration serveur (APP_NAME), avec repli * sur `APP_CONFIG.name`. Aucun composant ne doit ecrire le nom en dur. */ export const useAppConfigStore = create((set) => ({ name: APP_CONFIG.name, maxNodes: APP_CONFIG.maxNodes, maxMapSizeMb: APP_CONFIG.maxMapSizeMb, maxShareSizeMb: APP_CONFIG.maxMapSizeMb, sharingEnabled: false, allowPermanentShares: true, defaultShareExpiryDays: 30, shareExpiryOptions: DEFAULT_EXPIRY_OPTIONS, serverReachable: false, load: async () => { try { const config = await fetchAppConfig(); set({ name: config.appName || APP_CONFIG.name, maxNodes: config.maxNodes || APP_CONFIG.maxNodes, maxMapSizeMb: config.maxMapSizeMb || APP_CONFIG.maxMapSizeMb, maxShareSizeMb: config.maxShareSizeMb || config.maxMapSizeMb || APP_CONFIG.maxMapSizeMb, sharingEnabled: config.sharingEnabled !== false, allowPermanentShares: config.allowPermanentShares !== false, defaultShareExpiryDays: config.defaultShareExpiryDays ?? 30, shareExpiryOptions: Array.isArray(config.shareExpiryOptions) && config.shareExpiryOptions.length > 0 ? config.shareExpiryOptions : DEFAULT_EXPIRY_OPTIONS, serverReachable: true, }); if (typeof document !== 'undefined') { document.title = config.appName || APP_CONFIG.name; } } catch { // Hors ligne : on garde les valeurs par defaut, sans bloquer l'interface. // Le partage reste presente comme indisponible plutot que d'echouer au clic. set({ serverReachable: false, sharingEnabled: false }); } }, }));