store/notesClientsStore.ts

hook·mobile·1.1 KB · 43 lignes· Voir l'itinéraire
Annotation non disponible

Lance npm run annotate (nécessite ANTHROPIC_API_KEY dans .env.local) pour générer une annotation française par Claude Haiku 4.5.

1 export

useNotesClientsStore

Code source· typescript

import { create } from "zustand";
import AsyncStorage from "@react-native-async-storage/async-storage";

const KEY = "wari-mobile.notes-clients";

type Notes = Record<string, string>;

type State = {
  notes: Notes;
  hydrated: boolean;
  hydrate: () => Promise<void>;
  getNote: (clientId: string) => string;
  setNote: (clientId: string, contenu: string) => Promise<void>;
};

export const useNotesClientsStore = create<State>((set, get) => ({
  notes: {},
  hydrated: false,
  hydrate: async () => {
    if (get().hydrated) return;
    try {
      const raw = await AsyncStorage.getItem(KEY);
      const notes = raw ? (JSON.parse(raw) as Notes) : {};
      set({ notes, hydrated: true });
    } catch {
      set({ hydrated: true });
    }
  },
  getNote: (clientId: string) => get().notes[clientId] ?? "",
  setNote: async (clientId: string, contenu: string) => {
    const next: Notes = { ...get().notes };
    if (contenu.trim().length === 0) {
      delete next[clientId];
    } else {
      next[clientId] = contenu;
    }
    set({ notes: next });
    try {
      await AsyncStorage.setItem(KEY, JSON.stringify(next));
    } catch {}
  },
}));