jan/web-client/app/_models/RootStore.ts
hiento09 86f0ffc7d1
Chore/disable submodule (#56)
* Chore disable git submodule for web-client and app-backend

* Chore add newest source code of app-backend and web-client

---------

Co-authored-by: Hien To <tominhhien97@gmail.com>
2023-09-05 16:29:07 +07:00

37 lines
1020 B
TypeScript

import { createContext, useContext } from "react";
import { Instance, types } from "mobx-state-tree";
import { History } from "./History";
import { values } from "mobx";
export const RootStore = types
.model("RootStore", {
historyStore: types.optional(History, {}),
})
.views((self) => ({
get activeConversationId() {
return values(self.historyStore.activeConversationId);
},
get conversations() {
return values(self.historyStore.conversations);
},
}));
export function initializeStore(): RootInstance {
const _store: RootInstance = RootStore.create({});
return _store;
}
export type RootInstance = Instance<typeof RootStore>;
const RootStoreContext = createContext<null | RootInstance>(null);
export const Provider = RootStoreContext.Provider;
export function useStore(): Instance<typeof RootStore> {
const store = useContext(RootStoreContext);
if (store === null) {
throw new Error("Store cannot be null, please add a context provider");
}
return store;
}