xref: /expo/home/utils/InitialDataContext.tsx (revision 4dde4257)
1import React, { useState, createContext, useContext } from 'react';
2
3import { HomeScreenDataQuery, Home_CurrentUserActorQuery } from '../graphql/types';
4
5type ContextValue = {
6  homeScreenData?: HomeScreenDataQuery;
7  setHomeScreenData: (d?: HomeScreenDataQuery) => void;
8  currentUserData?: Home_CurrentUserActorQuery;
9  setCurrentUserData: (d?: Home_CurrentUserActorQuery) => void;
10};
11
12const InitialDataContext = createContext<ContextValue | null>(null);
13
14export function useInitialData() {
15  const context = useContext(InitialDataContext);
16
17  if (context === null) {
18    throw new Error('useInitialData must be used within a InitialDataProvider');
19  }
20
21  return context;
22}
23
24export function InitialDataProvider({ children }: { children: React.ReactNode }) {
25  const [homeScreenData, setHomeScreenData] = useState<HomeScreenDataQuery | undefined>();
26  const [currentUserData, setCurrentUserData] = useState<Home_CurrentUserActorQuery | undefined>();
27
28  return (
29    <InitialDataContext.Provider
30      value={{ homeScreenData, setHomeScreenData, currentUserData, setCurrentUserData }}>
31      {children}
32    </InitialDataContext.Provider>
33  );
34}
35