Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generator multichoice in callout and visual container #46

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions components/shared/richText/Callout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
calloutTypeColor,
mainColorBorderClass,
} from "../../../lib/constants/colors";
import { isCalloutType } from "../../../lib/types/calloutType";
import { siteCodename } from "../../../lib/utils/env";
import { Component_Callout } from "../../../models";
import { Component_Callout, contentTypes } from "../../../models";
import { RichTextElement } from "./RichTextElement";

type Props = Readonly<{
Expand All @@ -32,33 +33,28 @@ export const CalloutComponent: FC<Props> = (props) => (
);

const renderTypeIcon = (callout: Component_Callout) => {
const { warning, info, lightbulb } = contentTypes.callout.elements.type.options;

switch (callout.elements.type.value[0]?.codename) {
case OptionCodename.Warning:
case warning.codename:
return <ExclamationTriangleIcon />;
case OptionCodename.Info:
case info.codename:
return <InformationCircleIcon />;
case OptionCodename.Lightbulb:
case lightbulb.codename:
return <LightBulbIcon />;
default:
throwUnknownType(callout);
}
};

const createIconColor = (callout: Component_Callout) => {
const calloutType = callout.elements.type.value[0]?.codename as CalloutType;
const calloutType = callout.elements.type.value[0]?.codename;

if (calloutType) return calloutTypeColor[calloutType];
if (isCalloutType(calloutType)) return calloutTypeColor[calloutType];

throwUnknownType(callout);
};

enum OptionCodename {
Warning = "warning",
Info = "info",
Lightbulb = "lightbulb",
}

type CalloutType = "warning" | "info" | "lightbulb" | undefined;

const throwUnknownType = (callout: Component_Callout) => {
throw new Error(
Expand Down
16 changes: 5 additions & 11 deletions components/shared/visualContainer/VisualContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC } from "react";

import { sanitizeAnchor } from "../../../lib/anchors";
import { Block_VisualContainer } from "../../../models";
import { Block_VisualContainer, contentTypes } from "../../../models";
import { BuildError } from "../ui/BuildError";
import { CarouselComponent } from "./Carousel";
import { GridComponent } from "./Grid";
Expand All @@ -13,8 +13,9 @@ type Props = Readonly<{
}>;

const VisualRepresentation: FC<Props> = (props) => {
const { grid, hero_unit, stack } = contentTypes.visual_container.elements.visual_representation.options;
switch (props.item.elements.visualRepresentation.value[0]?.codename) {
case visualRepresentation.grid:
case grid.codename:
return (
<GridComponent
items={props.item.elements.items.linkedItems}
Expand All @@ -23,7 +24,7 @@ const VisualRepresentation: FC<Props> = (props) => {
codename={props.item.system.codename}
/>
);
case visualRepresentation.stack:
case stack.codename:
return (
<StackComponent
items={props.item.elements.items.linkedItems}
Expand All @@ -33,7 +34,7 @@ const VisualRepresentation: FC<Props> = (props) => {
codename={props.item.system.codename}
/>
);
case visualRepresentation.hero_unit:
case hero_unit.codename:
if (props.item.elements.items.linkedItems.length === 1) {
const fact = props.item.elements.items.linkedItems[0];
return !fact ? (
Expand Down Expand Up @@ -67,10 +68,3 @@ export const VisualContainer: FC<Props> = (props) => (
<VisualRepresentation item={props.item} />
</div>
);

// https://kontent-ai.atlassian.net/browse/DEVREL-955
const visualRepresentation = {
grid: "grid",
stack: "stack",
hero_unit: "hero_unit",
} as const;
5 changes: 3 additions & 2 deletions lib/constants/colors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CalloutType } from "../types/calloutType";
import { PerCollection } from "../types/perCollection";

export const mainColorHoverClass: PerCollection<string> = {
Expand Down Expand Up @@ -54,7 +55,7 @@ export const mainColorAfterBgClass: PerCollection<string> = {
ficto_surgical: "after:bg-rose-900",
}

export const calloutTypeColor = {
export const calloutTypeColor: Readonly<Record<CalloutType, string>> = {
warning: "text-orange-400",
info: "text-blue-400",
lightbulb: "text-green-400",
Expand All @@ -64,4 +65,4 @@ export const mainColorAnchor: PerCollection<string> = {
ficto_healthtech: "border-emerald-800",
ficto_imaging: "border-indigo-800",
ficto_surgical: "border-rose-800",
}
}
6 changes: 6 additions & 0 deletions lib/types/calloutType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { contentTypes } from "../../models/project/contentTypes";

export type CalloutType = keyof typeof contentTypes.callout.elements.type.options;

export const isCalloutType = (codename: string | undefined): codename is CalloutType =>
!!codename && Object.keys(contentTypes.callout.elements.type.options).includes(codename);
1 change: 0 additions & 1 deletion lib/types/perCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ const emptyCodenames: PerCollection<null> = {
ficto_surgical: null,
ficto_healthtech: null,
};

Loading