Home / File/ StoreContext.tsx — react Source File

StoreContext.tsx — react Source File

Architecture documentation for StoreContext.tsx, a tsx file in the react codebase. 5 imports, 4 dependents.

File tsx PlaygroundApp Stores 5 imports 4 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  73168655_c854_d3d1_50a0_b37f865f3c7e["StoreContext.tsx"]
  49642e97_a10e_5f44_62bd_268b445bc02f["createContext.ts"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> 49642e97_a10e_5f44_62bd_268b445bc02f
  6a454731_ba9a_e18e_d991_1e1a1c3d5669["createContext"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> 6a454731_ba9a_e18e_d991_1e1a1c3d5669
  f69df4a5_0121_dc26_8c32_aacc01afafd1["defaultStore.ts"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> f69df4a5_0121_dc26_8c32_aacc01afafd1
  200e1d7d_8772_57b3_072e_509f07464987["index.ts"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> 200e1d7d_8772_57b3_072e_509f07464987
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  73168655_c854_d3d1_50a0_b37f865f3c7e --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  84f17a2e_0d58_9d14_2c35_30f086f6e436["ConfigEditor.tsx"]
  84f17a2e_0d58_9d14_2c35_30f086f6e436 --> 73168655_c854_d3d1_50a0_b37f865f3c7e
  6c4ddf93_cf1a_7b66_fd69_69c7c8032b25["EditorImpl.tsx"]
  6c4ddf93_cf1a_7b66_fd69_69c7c8032b25 --> 73168655_c854_d3d1_50a0_b37f865f3c7e
  e75ba65e_505f_abf4_2928_6f0aefb2fc72["Input.tsx"]
  e75ba65e_505f_abf4_2928_6f0aefb2fc72 --> 73168655_c854_d3d1_50a0_b37f865f3c7e
  f0d5e809_a088_0b87_300e_fa0b251099b4["Header.tsx"]
  f0d5e809_a088_0b87_300e_fa0b251099b4 --> 73168655_c854_d3d1_50a0_b37f865f3c7e
  style 73168655_c854_d3d1_50a0_b37f865f3c7e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import type {Dispatch, ReactNode} from 'react';
import {useState, useEffect, useReducer} from 'react';
import createContext from '../lib/createContext';
import {emptyStore, defaultStore} from '../lib/defaultStore';
import {
  saveStore,
  initStoreFromUrlOrLocalStorage,
  type Store,
} from '../lib/stores';

const StoreContext = createContext<Store>();

/**
 * Hook to access the store.
 */
export const useStore = StoreContext.useContext;

const StoreDispatchContext = createContext<Dispatch<ReducerAction>>();

/**
 * Hook to access the store dispatch function.
 */
export const useStoreDispatch = StoreDispatchContext.useContext;

/**
 * Make Store and dispatch function available to all sub-components in children.
 */
export function StoreProvider({children}: {children: ReactNode}): JSX.Element {
  const [store, dispatch] = useReducer(storeReducer, emptyStore);
  const [isPageReady, setIsPageReady] = useState<boolean>(false);

  useEffect(() => {
    let mountStore: Store;
    try {
      mountStore = initStoreFromUrlOrLocalStorage();
    } catch (e) {
      console.error('Failed to initialize store from URL or local storage', e);
      mountStore = defaultStore;
    }
    dispatch({type: 'setStore', payload: {store: mountStore}});
    setIsPageReady(true);
  }, []);

  useEffect(() => {
    if (store !== emptyStore) {
      saveStore(store);
    }
  }, [store]);

  return (
    <StoreContext.Provider value={store}>
      <StoreDispatchContext.Provider value={dispatch}>
        {isPageReady ? children : null}
      </StoreDispatchContext.Provider>
    </StoreContext.Provider>
  );
}

type ReducerAction =
  | {
      type: 'setStore';
      payload: {
        store: Store;
      };
    }
  | {
      type: 'updateSource';
      payload: {
        source: string;
      };
    }
  | {
      type: 'updateConfig';
      payload: {
        config: string;
      };
    }
  | {
      type: 'toggleInternals';
    };

function storeReducer(store: Store, action: ReducerAction): Store {
  switch (action.type) {
    case 'setStore': {
      const newStore = action.payload.store;
      return newStore;
    }
    case 'updateSource': {
      const source = action.payload.source;
      const newStore = {
        ...store,
        source,
      };
      return newStore;
    }
    case 'updateConfig': {
      const config = action.payload.config;
      const newStore = {
        ...store,
        config,
      };
      return newStore;
    }
    case 'toggleInternals': {
      const newStore = {
        ...store,
        showInternals: !store.showInternals,
      };
      return newStore;
    }
  }
}

Domain

Subdomains

Frequently Asked Questions

What does StoreContext.tsx do?
StoreContext.tsx is a source file in the react codebase, written in tsx. It belongs to the PlaygroundApp domain, Stores subdomain.
What functions are defined in StoreContext.tsx?
StoreContext.tsx defines 2 function(s): StoreProvider, storeReducer.
What does StoreContext.tsx depend on?
StoreContext.tsx imports 5 module(s): createContext, createContext.ts, defaultStore.ts, index.ts, react.
What files import StoreContext.tsx?
StoreContext.tsx is imported by 4 file(s): ConfigEditor.tsx, EditorImpl.tsx, Header.tsx, Input.tsx.
Where is StoreContext.tsx in the architecture?
StoreContext.tsx is located at compiler/apps/playground/components/StoreContext.tsx (domain: PlaygroundApp, subdomain: Stores, directory: compiler/apps/playground/components).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free