Files
workspace/code/app/app-rn/src/store/family.ts
T
2026-06-26 17:35:29 +08:00

54 lines
1.0 KiB
TypeScript

import AsyncStorage from "@react-native-async-storage/async-storage";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
interface Family {
family_id: number;
}
interface FamilyState {
currentFamily: Family;
setCurrentFamily: (family: Family) => void;
clear: () => void;
inFamily: () => boolean;
getFamilyId: () => number;
}
const defaultFamily: Family = {
family_id: 0,
};
export const useFamilyStore = create<FamilyState>()(
persist(
(set, get) => ({
currentFamily: defaultFamily,
setCurrentFamily: (family) =>
set({
currentFamily: family,
}),
clear: () =>
set({
currentFamily: defaultFamily,
}),
inFamily: () => {
const currentFamily = get().currentFamily;
return currentFamily.family_id !== 0;
},
getFamilyId: () => get().currentFamily.family_id,
}),
{
name: "family-storage",
storage: createJSONStorage(() => AsyncStorage),
},
),
);