|
1 | 1 | import React, { useState, useLayoutEffect, createContext } from "react"
|
2 | 2 | import { getStore, onLogAction } from "../../redux"
|
3 |
| -import { IGatsbyCLIState } from "../../redux/types" |
| 3 | +import { IGatsbyCLIState, ActionsUnion, ILog } from "../../redux/types" |
4 | 4 | import { IRenderPageArgs } from "../../types"
|
| 5 | +import { Actions } from "../../constants" |
5 | 6 |
|
6 |
| -const StoreStateContext = createContext<{ |
| 7 | +interface IStoreStateContext { |
7 | 8 | logs: IGatsbyCLIState
|
| 9 | + messages: Array<ILog> |
8 | 10 | pageTree: IRenderPageArgs | null
|
9 |
| -}>(getStore().getState()) |
| 11 | +} |
| 12 | + |
| 13 | +const StoreStateContext = createContext<IStoreStateContext>({ |
| 14 | + ...getStore().getState(), |
| 15 | + messages: [], |
| 16 | +}) |
10 | 17 |
|
11 | 18 | export const StoreStateProvider: React.FC = ({
|
12 | 19 | children,
|
13 | 20 | }): React.ReactElement => {
|
14 |
| - const [state, setState] = useState(getStore().getState()) |
| 21 | + const [state, setState] = useState<IStoreStateContext>({ |
| 22 | + ...getStore().getState(), |
| 23 | + messages: [], |
| 24 | + }) |
15 | 25 |
|
16 | 26 | useLayoutEffect(
|
17 | 27 | () =>
|
18 |
| - onLogAction(() => { |
19 |
| - setState(getStore().getState()) |
| 28 | + onLogAction((action: ActionsUnion) => { |
| 29 | + if (action.type === Actions.Log) { |
| 30 | + setState(state => { |
| 31 | + return { |
| 32 | + ...state, |
| 33 | + messages: [...state.messages, action.payload], |
| 34 | + } |
| 35 | + }) |
| 36 | + } else { |
| 37 | + setState(state => { |
| 38 | + return { ...getStore().getState(), messages: state.messages } |
| 39 | + }) |
| 40 | + } |
20 | 41 | }),
|
21 | 42 | []
|
22 | 43 | )
|
|
0 commit comments