Skip to content

Commit

Permalink
refactor: rework types to avoid unwanted anys
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Mar 26, 2024
1 parent 1c6222c commit bf45116
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 48 deletions.
5 changes: 1 addition & 4 deletions apps/demo/app/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use client";

import { Data } from "@/core/types/Config";
import { Puck } from "@/core/components/Puck";
import { Render } from "@/core/components/Render";
import { Button } from "@/core/components/Button";
import { Button, Data, Puck, Render } from "@/core";
import headingAnalyzer from "@/plugin-heading-analyzer/src/HeadingAnalyzer";
import config, { UserConfig } from "../../config";
import { useDemoData } from "../../lib/use-demo-data";
Expand Down
5 changes: 1 addition & 4 deletions apps/demo/app/custom-ui/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use client";

import { Data } from "@/core/types/Config";
import { Puck } from "@/core/components/Puck";
import { Render } from "@/core/components/Render";
import { Button } from "@/core/components/Button";
import { Button, Data, Puck, Render } from "@/core";
import { HeadingAnalyzer } from "@/plugin-heading-analyzer/src/HeadingAnalyzer";
import config, { UserConfig } from "../../../config";
import { useDemoData } from "../../../lib/use-demo-data";
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/config/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, Data } from "@/core/types/Config";
import { Config, Data } from "@/core";
import { ButtonGroup, ButtonGroupProps } from "./blocks/ButtonGroup";
import { Card, CardProps } from "./blocks/Card";
import { Columns, ColumnsProps } from "./blocks/Columns";
Expand Down
4 changes: 1 addition & 3 deletions packages/core/components/DropZone/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import { getZoneId } from "../../lib/get-zone-id";

export type PathData = Record<string, { path: string[]; label: string }>;

export type DropZoneContext<
UserConfig extends Config<any, any, any> = Config<any, any, any>
> = {
export type DropZoneContext<UserConfig extends Config = Config> = {
data: Data;
config: UserConfig;
componentState?: Record<string, any>;
Expand Down
8 changes: 2 additions & 6 deletions packages/core/components/Puck/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ type ZoomConfig = {
zoom: number;
};

type AppContext<
UserConfig extends Config<any, any, any> = Config<any, any, any>
> = {
type AppContext<UserConfig extends Config = Config> = {
state: AppState;
dispatch: (action: PuckAction) => void;
config: UserConfig;
Expand Down Expand Up @@ -114,9 +112,7 @@ export const AppProvider = ({
);
};

export function useAppContext<
UserConfig extends Config<any, any, any> = Config<any, any, any>
>() {
export function useAppContext<UserConfig extends Config = Config>() {
const mainContext = useContext(appContext) as AppContext<UserConfig>;

const selectedItem = mainContext.state.ui.itemSelector
Expand Down
4 changes: 1 addition & 3 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ import { IframeConfig } from "../../types/IframeConfig";

const getClassName = getClassNameFactory("Puck", styles);

export function Puck<
UserConfig extends Config<any, any, any> = Config<any, any, any>
>({
export function Puck<UserConfig extends Config = Config>({
children,
config,
data: initialData = { content: [], root: { props: { title: "" } } },
Expand Down
10 changes: 7 additions & 3 deletions packages/core/components/Render/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { rootDroppableId } from "../../lib/root-droppable-id";
import { Config, Data } from "../../types/Config";
import { DropZone, DropZoneProvider } from "../DropZone";

export function Render<
UserConfig extends Config<any, any, any> = Config<any, any, any>
>({ config, data }: { config: UserConfig; data: Data }) {
export function Render<UserConfig extends Config = Config>({
config,
data,
}: {
config: UserConfig;
data: Data;
}) {
// DEPRECATED
const rootProps = data.root.props || data.root;
const title = rootProps?.title || "";
Expand Down
10 changes: 7 additions & 3 deletions packages/core/components/ServerRender/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ function DropZoneRender({
);
}

export function Render<
UserConfig extends Config<any, any, any> = Config<any, any, any>
>({ config, data }: { config: UserConfig; data: Data }) {
export function Render<UserConfig extends Config = Config>({
config,
data,
}: {
config: UserConfig;
data: Data;
}) {
if (config.root?.render) {
// DEPRECATED
const rootProps = data.root.props || data.root;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/lib/resolve-all-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Config, Data, MappedItem } from "../types/Config";
import { resolveAllComponentData } from "./resolve-component-data";
import { resolveRootData } from "./resolve-root-data";

export const resolveAllData = async (
export async function resolveAllData(
data: Data,
config: Config<any, any, any>,
config: Config,
onResolveStart?: (item: MappedItem) => void,
onResolveEnd?: (item: MappedItem) => void
) => {
) {
const dynamicRoot = await resolveRootData(data, config);

const { zones = {} } = data;
Expand Down Expand Up @@ -36,4 +36,4 @@ export const resolveAllData = async (
),
zones: resolvedZones,
};
};
}
4 changes: 1 addition & 3 deletions packages/core/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export const setAction = (state: AppState, action: SetAction) => {
return { ...state, ...action.state(state) };
};

export function createReducer<
UserConfig extends Config<any, any, any> = Config<any, any, any>
>({
export function createReducer<UserConfig extends Config = Config>({
config,
record,
}: {
Expand Down
33 changes: 19 additions & 14 deletions packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ export type Content<
Props extends { [key: string]: any } = { [key: string]: any }
> = ComponentData<Props>[];

export type PuckComponent<
Props extends DefaultComponentProps = DefaultComponentProps
> = (props: WithPuckProps<Props & { puck: PuckContext }>) => JSX.Element;
export type PuckComponent<Props> = (
props: WithPuckProps<Props & { puck: PuckContext }>
) => JSX.Element;

export type PuckContext = {
renderDropZone: React.FC<DropZoneProps>;
Expand All @@ -162,7 +162,7 @@ export type PuckContext = {
export type ComponentConfig<
ComponentProps extends DefaultComponentProps = DefaultComponentProps,
DefaultProps = ComponentProps,
DataShape = ComponentData<ComponentProps>
DataShape = Omit<ComponentData<ComponentProps>, "type">
> = {
render: PuckComponent<ComponentProps>;
label?: string;
Expand All @@ -171,10 +171,15 @@ export type ComponentConfig<
resolveData?: (
data: DataShape,
params: { changed: Partial<Record<keyof ComponentProps, boolean>> }
) => Promise<{
props?: Partial<ComponentProps>;
readOnly?: Partial<Record<keyof ComponentProps, boolean>>;
}>;
) =>
| Promise<{
props?: Partial<ComponentProps>;
readOnly?: Partial<Record<keyof ComponentProps, boolean>>;
}>
| {
props?: Partial<ComponentProps>;
readOnly?: Partial<Record<keyof ComponentProps, boolean>>;
};
};

type Category<ComponentName> = {
Expand All @@ -185,12 +190,12 @@ type Category<ComponentName> = {
};

export type Config<
Props extends { [key: string]: any } = { [key: string]: any },
Props extends Record<string, any> = Record<string, any>,
RootProps extends DefaultRootProps = DefaultRootProps,
CategoryName extends string = any
CategoryName extends string = string
> = {
categories?: Record<CategoryName, Category<keyof Props>> & {
other?: Category<Props>;
other?: Category<keyof Props>;
};
components: {
[ComponentName in keyof Props]: Omit<
Expand All @@ -200,9 +205,9 @@ export type Config<
};
root?: Partial<
ComponentConfig<
RootProps & { children: ReactNode },
Partial<RootProps & { children: ReactNode }>,
RootDataWithProps<RootProps>
RootProps & { children?: ReactNode },
Partial<RootProps & { children?: ReactNode }>,
RootDataWithProps
>
>;
};
Expand Down

0 comments on commit bf45116

Please sign in to comment.