-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add popup and perf improvements
- Loading branch information
Showing
17 changed files
with
304 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useContext, useRef } from "react"; | ||
|
||
import { CoreContext } from "./state"; | ||
import type { CoreContextType } from "./state"; | ||
|
||
export const useCore = () => { | ||
const coreRef = useRef<CoreContextType>({} as CoreContextType); | ||
const core = useContext(CoreContext); | ||
|
||
// It is important to not override the `current` object reference so it | ||
// doesn't trigger more hooks than it should if it is added as a hook | ||
// dependency | ||
coreRef.current.state = core.state; | ||
coreRef.current.dispatch = core.dispatch; | ||
|
||
return { | ||
core: coreRef.current, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use client"; | ||
|
||
import type { PropsWithChildren } from "react"; | ||
import { useReducer } from "react"; | ||
|
||
import { reducer } from "./reducer"; | ||
import { CoreContext, defaultState } from "./state"; | ||
|
||
export const CoreProvider = ({ children }: PropsWithChildren) => { | ||
const [state, dispatch] = useReducer(reducer, defaultState); | ||
|
||
return ( | ||
<CoreContext.Provider value={{ dispatch, state }}> | ||
{children} | ||
</CoreContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { CoreState } from "./state"; | ||
|
||
export type CoreAction = | ||
| { | ||
content: CoreState["isLoadingBlocking"]; | ||
type: "SET_IS_LOADING_BLOCKING"; | ||
} | ||
| { | ||
content: CoreState["popupOpenId"]; | ||
type: "SET_POPUP_OPEN_ID"; | ||
}; | ||
|
||
type Content<T extends CoreAction["type"]> = Extract< | ||
CoreAction, | ||
{ type: T } | ||
>["content"]; | ||
|
||
export const setPopupOpenId = ( | ||
content: Content<"SET_POPUP_OPEN_ID">, | ||
): CoreAction => ({ | ||
content, | ||
type: "SET_POPUP_OPEN_ID", | ||
}); | ||
|
||
export const setIsLoadingBlocking = ( | ||
content: Content<"SET_IS_LOADING_BLOCKING">, | ||
): CoreAction => ({ | ||
content, | ||
type: "SET_IS_LOADING_BLOCKING", | ||
}); | ||
|
||
export const reducer = (state: CoreState, action: CoreAction) => { | ||
switch (action.type) { | ||
case "SET_POPUP_OPEN_ID": | ||
return { ...state, popupOpenId: action.content }; | ||
|
||
case "SET_IS_LOADING_BLOCKING": | ||
return { ...state, isLoadingBlocking: action.content }; | ||
|
||
default: | ||
action satisfies never; | ||
|
||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { Dispatch } from "react"; | ||
import { createContext } from "react"; | ||
|
||
import type { CoreAction } from "./reducer"; | ||
|
||
export type CoreState = { | ||
isLoadingBlocking: boolean; | ||
popupOpenId: null | string; | ||
}; | ||
|
||
export type CoreContextType = { | ||
dispatch: Dispatch<CoreAction>; | ||
state: CoreState; | ||
}; | ||
|
||
export const defaultState: CoreState = { | ||
isLoadingBlocking: false, | ||
popupOpenId: null, | ||
}; | ||
|
||
export const CoreContext = createContext<CoreContextType>({ | ||
dispatch: () => null, | ||
state: defaultState, | ||
}); |
Oops, something went wrong.