e0d96c3991
## Summary Memoize shared context values so provider renders do not unnecessarily rerender every consumer. Oxlint now enforces this pattern for the rest of the dashboard. Base: [#4677](https://github.com/triggerdotdev/trigger.dev/pull/4677)
34 lines
926 B
TypeScript
34 lines
926 B
TypeScript
import type { ReactNode } from "react";
|
|
import { createContext, useContext, useMemo } from "react";
|
|
|
|
export type OperatingSystemPlatform = "mac" | "windows";
|
|
|
|
type OperatingSystemContext = {
|
|
platform: OperatingSystemPlatform;
|
|
};
|
|
|
|
type OperatingSystemContextProviderProps = {
|
|
platform: OperatingSystemPlatform;
|
|
children: ReactNode;
|
|
};
|
|
|
|
const Context = createContext<OperatingSystemContext | null>(null);
|
|
|
|
export const OperatingSystemContextProvider = ({
|
|
platform,
|
|
children,
|
|
}: OperatingSystemContextProviderProps) => {
|
|
const value = useMemo(() => ({ platform }), [platform]);
|
|
|
|
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
};
|
|
|
|
const throwIfNoProvider = () => {
|
|
throw new Error("Please wrap your application in an OperatingSystemContextProvider.");
|
|
};
|
|
|
|
export const useOperatingSystem = () => {
|
|
const { platform } = useContext(Context) ?? throwIfNoProvider();
|
|
return { platform };
|
|
};
|