-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add typing to componment renderer, refactor imports and fra…
…gments
- Loading branch information
1 parent
008be46
commit e78ee8c
Showing
17 changed files
with
347 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,49 @@ | ||
/** | ||
* ComponentRenderer is a component that renders components based on the data supplied. | ||
* | ||
* A few ground principles for ComponentRenderer | ||
* | ||
* 1. It should accept data and decide which component(s) to render | ||
* 2. It should allow arrays of data to be passed and handle null/undefined | ||
* 3. As a proxy, it should let you know if it can't render the component you're asking because you didn't provide enough data. | ||
* 4. It should be able to render the component(s) with the data you provided. | ||
* 5. It should skip rendering if component is not found in the componentMap. | ||
*/ | ||
|
||
import { componentMap } from './mappings'; | ||
import { ComponentProps } from 'react'; | ||
|
||
type ComponentMapType = typeof componentMap; | ||
type Data = ComponentProps<ComponentMapType[ComponentKey]>['data']; | ||
type ComponentKey = keyof ComponentMapType; | ||
type DataWithTypename = (Data & { __typename: string }) | { __typename: string } | null; | ||
|
||
function isComponentKey(key: string): key is ComponentKey { | ||
return key in componentMap; | ||
} | ||
|
||
export default function ComponentRenderer({ | ||
data, | ||
}: { | ||
data: any; // @TODO: Fixme | ||
}) { | ||
export default function ComponentRenderer<T extends DataWithTypename | DataWithTypename[]>({ data }: { data: T }) { | ||
if (data === null) { | ||
return null; | ||
} | ||
|
||
// If we have an array, we render each item in the array | ||
if (Array.isArray(data)) { | ||
return ( | ||
<> | ||
{data.map((item) => { | ||
if (!item?.sys?.id) { | ||
return null; | ||
} | ||
|
||
return <ComponentRenderer key={item.sys.id} data={item} />; | ||
{data.map((item, index) => { | ||
return <ComponentRenderer key={index} data={item} />; | ||
})} | ||
</> | ||
); | ||
} | ||
|
||
if (!data?.__typename) { | ||
return null; | ||
} | ||
|
||
// @TODO: Fix typings for componentMap. | ||
// @ts-ignore | ||
const Component = componentMap[data.__typename]; | ||
if (Component) { | ||
return <Component data={data} />; | ||
if (isComponentKey(data.__typename)) { | ||
const Component = componentMap[data.__typename]; | ||
// At this point we know data is one of the accepted props for the component | ||
return <Component data={data as any} />; | ||
} | ||
|
||
// If we don't know the component, we don't render anything | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import dynamic from 'next/dynamic'; | ||
import { HeroBannerCtf } from '#/components/hero-banner-ctf/hero-banner-ctf'; | ||
import { DuplexCtf } from '#/components/duplex-ctf/duplex-ctf'; | ||
import { TopicBusinessInfo } from '#/components/topic-business-info/topic-business-info'; | ||
import { TopicPerson } from '#/components/topic-person/topic-person'; | ||
|
||
export const componentMap = { | ||
ComponentHeroBanner: dynamic(() => import('#/components/hero-banner-ctf').then((mod) => mod.HeroBannerCtf)), | ||
ComponentDuplex: dynamic(() => import('#/components/duplex-ctf').then((mod) => mod.DuplexCtf)), | ||
TopicBusinessInfo: dynamic(() => import('#/components/topic-business-info').then((mod) => mod.TopicBusinessInfo)), | ||
TopicPersons: dynamic(() => import('#/components/topic-person').then((mod) => mod.TopicPerson)), | ||
}; | ||
ComponentHeroBanner: HeroBannerCtf, | ||
ComponentDuplex: DuplexCtf, | ||
TopicBusinessInfo: TopicBusinessInfo, | ||
TopicPersons: TopicPerson, | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.