Skip to content

Commit

Permalink
Context improvements.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: cbcbbb296688b10c0be0a840be1ad328138c19f5
  • Loading branch information
cpojer committed Feb 25, 2025
1 parent f16be3a commit a8b092d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion hera/audio/Music.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const context: MusicContext = {
const Context = createContext<MusicContext>(context);

export const MusicContext = ({ children }: { children: ReactNode }) => (
<Context.Provider value={context}>{children}</Context.Provider>
<Context value={context}>{children}</Context>
);

export default function useMusic(song: SongName) {
Expand Down
22 changes: 7 additions & 15 deletions hera/hooks/useHide.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from 'react';
import createContextHook from '@deities/ui/hooks/createContextHook.tsx';
import { useEffect, useState } from 'react';

export const Context = createContext(false);

export const HideContext = ({ children }: { children: ReactNode }) => {
const [HideContext, useHide] = createContextHook(() => {
const [hidden, setHidden] = useState(false);
useEffect(() => {
const listener = (event: KeyboardEvent) => {
Expand All @@ -26,9 +19,8 @@ export const HideContext = ({ children }: { children: ReactNode }) => {
return () => document.removeEventListener('keydown', listener);
}, []);

return <Context.Provider value={hidden}>{children}</Context.Provider>;
};
return hidden;
}, false);

export default function useHide() {
return useContext(Context);
}
export { HideContext };
export default useHide;
4 changes: 1 addition & 3 deletions ui/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export type MenuContext = RefObject<boolean>;
const Context = createContext<MenuContext>({ current: false });

export const MenuContext = ({ children }: { children: ReactNode }) => {
return (
<Context.Provider value={{ current: false }}>{children}</Context.Provider>
);
return <Context value={{ current: false }}>{children}</Context>;
};

export const useDisableOpenMenuOnCancel = () => {
Expand Down
15 changes: 15 additions & 0 deletions ui/hooks/createContextHook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createContext, FunctionComponent, ReactNode, useContext } from 'react';

export default function createContextHook<T>(
contextInitializer: () => T,
defaultValue?: T,
): [Context: FunctionComponent<{ children: ReactNode }>, useHook: () => T] {
const Context = createContext<T | undefined>(defaultValue);

return [
({ children }: { children: ReactNode }) => {
return <Context value={contextInitializer()}>{children}</Context>;
},
() => useContext(Context) as T,
];
}
4 changes: 2 additions & 2 deletions ui/hooks/useAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Context = createContext<AlertContext>({
export const AlertContext = ({ children }: { children: ReactNode }) => {
const [state, setState] = useState<Props | null>(null);
return (
<Context.Provider
<Context
value={useMemo(
() => ({
alert: setState,
Expand All @@ -48,7 +48,7 @@ export const AlertContext = ({ children }: { children: ReactNode }) => {
>
{children}
{state && <Alert {...state} />}
</Context.Provider>
</Context>
);
};

Expand Down
22 changes: 7 additions & 15 deletions ui/hooks/useScale.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { TileSize } from '@deities/athena/map/Configuration.tsx';
import {
createContext,
ReactNode,
useContext,
useEffect,
useState,
} from 'react';
import { useEffect, useState } from 'react';
import { isIOS } from '../Browser.tsx';
import cssVar, { applyVar } from '../cssVar.tsx';
import createContextHook from './createContextHook.tsx';

let div: HTMLDivElement | null = null;
const CACHE = new Map<string, number>();
Expand Down Expand Up @@ -49,9 +44,7 @@ export const getScale = (tileSize: number) => {
return maxScale;
};

const Context = createContext<number>(1);

export const ScaleContext = ({ children }: { children: ReactNode }) => {
const [ScaleContext, useScale] = createContextHook(() => {
const [scale, setScale] = useState(() => getScale(TileSize));

useEffect(() => {
Expand All @@ -66,9 +59,8 @@ export const ScaleContext = ({ children }: { children: ReactNode }) => {
};
}, [scale]);

return <Context.Provider value={scale}>{children}</Context.Provider>;
};
return scale;
});

export default function useScale() {
return useContext(Context);
}
export default useScale;
export { ScaleContext };
2 changes: 1 addition & 1 deletion ui/hooks/useScrollRestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Context = createContext<RefObject<boolean | null>>({
export const ScrollRestore = Context;

export const ScrollRestoreContext = ({ children }: { children: ReactNode }) => (
<Context.Provider value={useRef(false)}>{children}</Context.Provider>
<Context value={useRef(false)}>{children}</Context>
);

export default function useScrollRestore() {
Expand Down

0 comments on commit a8b092d

Please sign in to comment.