7c749299bb
* Enforces explicit type imports * Upgrades typescript-eslint * Upgrades eslint related dependencies * Update config * Sync packages mismatches * Syncs prettier version * Linting * Relocks node version * Fixes * Locks @vitejs/plugin-react to 1.3.2 * Linting
29 lines
1.0 KiB
TypeScript
29 lines
1.0 KiB
TypeScript
import React from "react";
|
|
import type { z, ZodType } from "zod";
|
|
|
|
export type GetAppData = (key: string) => unknown;
|
|
export type SetAppData = (key: string, value: unknown) => void;
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
const EventTypeAppContext = React.createContext<[GetAppData, SetAppData]>([() => ({}), () => {}]);
|
|
|
|
export type SetAppDataGeneric<TAppData extends ZodType> = <
|
|
TKey extends keyof z.infer<TAppData>,
|
|
TValue extends z.infer<TAppData>[TKey]
|
|
>(
|
|
key: TKey,
|
|
value: TValue
|
|
) => void;
|
|
|
|
export type GetAppDataGeneric<TAppData extends ZodType> = <TKey extends keyof z.infer<TAppData>>(
|
|
key: TKey
|
|
) => z.infer<TAppData>[TKey];
|
|
|
|
export const useAppContextWithSchema = <TAppData extends ZodType>() => {
|
|
type GetAppData = GetAppDataGeneric<TAppData>;
|
|
type SetAppData = SetAppDataGeneric<TAppData>;
|
|
// TODO: Not able to do it without type assertion here
|
|
const context = React.useContext(EventTypeAppContext) as [GetAppData, SetAppData];
|
|
return context;
|
|
};
|
|
export default EventTypeAppContext;
|