From bf451162be4c82464ce7529f46bf43277bb1131b Mon Sep 17 00:00:00 2001 From: Chris Villa Date: Tue, 26 Mar 2024 14:32:29 +0100 Subject: [PATCH] refactor: rework types to avoid unwanted anys --- apps/demo/app/[...puckPath]/client.tsx | 5 +-- .../app/custom-ui/[...puckPath]/client.tsx | 5 +-- apps/demo/config/index.tsx | 2 +- packages/core/components/DropZone/context.tsx | 4 +-- packages/core/components/Puck/context.tsx | 8 ++--- packages/core/components/Puck/index.tsx | 4 +-- packages/core/components/Render/index.tsx | 10 ++++-- .../core/components/ServerRender/index.tsx | 10 ++++-- packages/core/lib/resolve-all-data.ts | 8 ++--- packages/core/reducer/index.ts | 4 +-- packages/core/types/Config.tsx | 33 +++++++++++-------- 11 files changed, 45 insertions(+), 48 deletions(-) diff --git a/apps/demo/app/[...puckPath]/client.tsx b/apps/demo/app/[...puckPath]/client.tsx index f9b7e643de..749f19c4d6 100644 --- a/apps/demo/app/[...puckPath]/client.tsx +++ b/apps/demo/app/[...puckPath]/client.tsx @@ -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"; diff --git a/apps/demo/app/custom-ui/[...puckPath]/client.tsx b/apps/demo/app/custom-ui/[...puckPath]/client.tsx index 023e014fe8..0e8be22e0c 100644 --- a/apps/demo/app/custom-ui/[...puckPath]/client.tsx +++ b/apps/demo/app/custom-ui/[...puckPath]/client.tsx @@ -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"; diff --git a/apps/demo/config/index.tsx b/apps/demo/config/index.tsx index 1430e93ee0..c6817c969f 100644 --- a/apps/demo/config/index.tsx +++ b/apps/demo/config/index.tsx @@ -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"; diff --git a/packages/core/components/DropZone/context.tsx b/packages/core/components/DropZone/context.tsx index fe0200465c..820d5473df 100644 --- a/packages/core/components/DropZone/context.tsx +++ b/packages/core/components/DropZone/context.tsx @@ -15,9 +15,7 @@ import { getZoneId } from "../../lib/get-zone-id"; export type PathData = Record; -export type DropZoneContext< - UserConfig extends Config = Config -> = { +export type DropZoneContext = { data: Data; config: UserConfig; componentState?: Record; diff --git a/packages/core/components/Puck/context.tsx b/packages/core/components/Puck/context.tsx index d3c0c0d605..ace11fc26d 100644 --- a/packages/core/components/Puck/context.tsx +++ b/packages/core/components/Puck/context.tsx @@ -43,9 +43,7 @@ type ZoomConfig = { zoom: number; }; -type AppContext< - UserConfig extends Config = Config -> = { +type AppContext = { state: AppState; dispatch: (action: PuckAction) => void; config: UserConfig; @@ -114,9 +112,7 @@ export const AppProvider = ({ ); }; -export function useAppContext< - UserConfig extends Config = Config ->() { +export function useAppContext() { const mainContext = useContext(appContext) as AppContext; const selectedItem = mainContext.state.ui.itemSelector diff --git a/packages/core/components/Puck/index.tsx b/packages/core/components/Puck/index.tsx index 114493de9b..9c5b1b9b7f 100644 --- a/packages/core/components/Puck/index.tsx +++ b/packages/core/components/Puck/index.tsx @@ -50,9 +50,7 @@ import { IframeConfig } from "../../types/IframeConfig"; const getClassName = getClassNameFactory("Puck", styles); -export function Puck< - UserConfig extends Config = Config ->({ +export function Puck({ children, config, data: initialData = { content: [], root: { props: { title: "" } } }, diff --git a/packages/core/components/Render/index.tsx b/packages/core/components/Render/index.tsx index 69da3471bd..9d43af76c8 100644 --- a/packages/core/components/Render/index.tsx +++ b/packages/core/components/Render/index.tsx @@ -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 = Config ->({ config, data }: { config: UserConfig; data: Data }) { +export function Render({ + config, + data, +}: { + config: UserConfig; + data: Data; +}) { // DEPRECATED const rootProps = data.root.props || data.root; const title = rootProps?.title || ""; diff --git a/packages/core/components/ServerRender/index.tsx b/packages/core/components/ServerRender/index.tsx index ec017fd7e7..18056bb9f7 100644 --- a/packages/core/components/ServerRender/index.tsx +++ b/packages/core/components/ServerRender/index.tsx @@ -59,9 +59,13 @@ function DropZoneRender({ ); } -export function Render< - UserConfig extends Config = Config ->({ config, data }: { config: UserConfig; data: Data }) { +export function Render({ + config, + data, +}: { + config: UserConfig; + data: Data; +}) { if (config.root?.render) { // DEPRECATED const rootProps = data.root.props || data.root; diff --git a/packages/core/lib/resolve-all-data.ts b/packages/core/lib/resolve-all-data.ts index 252471b7e9..6952241a62 100644 --- a/packages/core/lib/resolve-all-data.ts +++ b/packages/core/lib/resolve-all-data.ts @@ -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, + config: Config, onResolveStart?: (item: MappedItem) => void, onResolveEnd?: (item: MappedItem) => void -) => { +) { const dynamicRoot = await resolveRootData(data, config); const { zones = {} } = data; @@ -36,4 +36,4 @@ export const resolveAllData = async ( ), zones: resolvedZones, }; -}; +} diff --git a/packages/core/reducer/index.ts b/packages/core/reducer/index.ts index 1508cf6f6f..949adaf585 100644 --- a/packages/core/reducer/index.ts +++ b/packages/core/reducer/index.ts @@ -49,9 +49,7 @@ export const setAction = (state: AppState, action: SetAction) => { return { ...state, ...action.state(state) }; }; -export function createReducer< - UserConfig extends Config = Config ->({ +export function createReducer({ config, record, }: { diff --git a/packages/core/types/Config.tsx b/packages/core/types/Config.tsx index 2588741f96..dddc8e4d54 100644 --- a/packages/core/types/Config.tsx +++ b/packages/core/types/Config.tsx @@ -151,9 +151,9 @@ export type Content< Props extends { [key: string]: any } = { [key: string]: any } > = ComponentData[]; -export type PuckComponent< - Props extends DefaultComponentProps = DefaultComponentProps -> = (props: WithPuckProps) => JSX.Element; +export type PuckComponent = ( + props: WithPuckProps +) => JSX.Element; export type PuckContext = { renderDropZone: React.FC; @@ -162,7 +162,7 @@ export type PuckContext = { export type ComponentConfig< ComponentProps extends DefaultComponentProps = DefaultComponentProps, DefaultProps = ComponentProps, - DataShape = ComponentData + DataShape = Omit, "type"> > = { render: PuckComponent; label?: string; @@ -171,10 +171,15 @@ export type ComponentConfig< resolveData?: ( data: DataShape, params: { changed: Partial> } - ) => Promise<{ - props?: Partial; - readOnly?: Partial>; - }>; + ) => + | Promise<{ + props?: Partial; + readOnly?: Partial>; + }> + | { + props?: Partial; + readOnly?: Partial>; + }; }; type Category = { @@ -185,12 +190,12 @@ type Category = { }; export type Config< - Props extends { [key: string]: any } = { [key: string]: any }, + Props extends Record = Record, RootProps extends DefaultRootProps = DefaultRootProps, - CategoryName extends string = any + CategoryName extends string = string > = { categories?: Record> & { - other?: Category; + other?: Category; }; components: { [ComponentName in keyof Props]: Omit< @@ -200,9 +205,9 @@ export type Config< }; root?: Partial< ComponentConfig< - RootProps & { children: ReactNode }, - Partial, - RootDataWithProps + RootProps & { children?: ReactNode }, + Partial, + RootDataWithProps > >; };