Skip to content

Commit

Permalink
feat(flags): add feature flag (#1507)
Browse files Browse the repository at this point in the history
Co-authored-by: seaerchin <[email protected]>
  • Loading branch information
seaerchin and seaerchin authored Sep 20, 2023
1 parent 3221187 commit 90ae047
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/constants/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const FEATURE_FLAGS = {
STYLING_REVAMP: "styles",
HOMEPAGE_TEMPLATES: "homepage_new_templates",
} as const
2 changes: 1 addition & 1 deletion src/layouts/EditHomepage/HomepagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const HomepagePreview = ({
dropdownIsActive={dropdownIsActive}
toggleDropdown={toggleDropdown}
ref={scrollRefs[sectionIndex] as Ref<HTMLDivElement>}
variant={section.hero.variant ?? "center"}
variant={section.hero.variant}
/>
</>
)}
Expand Down
54 changes: 34 additions & 20 deletions src/layouts/components/Homepage/HeroBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HStack,
Spacer,
} from "@chakra-ui/react"
import { useFeatureIsOn } from "@growthbook/growthbook-react"
import {
FormErrorMessage,
FormLabel,
Expand All @@ -25,6 +26,7 @@ import { BiInfoCircle } from "react-icons/bi"
import { FormContext, FormError, FormTitle } from "components/Form"
import FormFieldMedia from "components/FormFieldMedia"

import { FEATURE_FLAGS } from "constants/featureFlags"
import { HERO_LAYOUTS } from "constants/homepage"

import { useEditableContext } from "contexts/EditableContext"
Expand Down Expand Up @@ -290,29 +292,34 @@ const HeroLayoutForm = ({
children,
}: HeroLayoutFormProps): JSX.Element => {
const { onChange } = useEditableContext()
const showNewLayouts = useFeatureIsOn(FEATURE_FLAGS.HOMEPAGE_TEMPLATES)

return (
<VStack spacing="1rem" align="flex-start" w="100%">
<Text textStyle="h5">Customise layout</Text>
<FormControl isRequired>
<FormLabel textStyle="subhead-1">Layout</FormLabel>
<SingleSelect
isClearable={false}
name="hero layout options"
value={variant}
items={_.values(HERO_LAYOUTS)}
// NOTE: Safe cast - the possible values are given by `HERO_LAYOUTS`
onChange={(val) => {
onChange({
target: {
// NOTE: Format is field type, index, section type, field
id: "section-0-hero-variant",
value: val as HeroBannerLayouts,
},
})
}}
/>
</FormControl>
<Text textStyle="h5">{`Customise ${
showNewLayouts ? "Layout" : "Hero"
}`}</Text>
{showNewLayouts && (
<FormControl isRequired>
<FormLabel textStyle="subhead-1">Layout</FormLabel>
<SingleSelect
isClearable={false}
name="hero layout options"
value={variant}
items={_.values(HERO_LAYOUTS)}
// NOTE: Safe cast - the possible values are given by `HERO_LAYOUTS`
onChange={(val) => {
onChange({
target: {
// NOTE: Format is field type, index, section type, field
id: "section-0-hero-variant",
value: val as HeroBannerLayouts,
},
})
}}
/>
</FormControl>
)}
<VStack spacing="1rem" w="100%">
{children({ currentSelectedOption: variant })}
</VStack>
Expand Down Expand Up @@ -352,6 +359,7 @@ export const HeroBody = ({
initialSectionType
)
const { onChange } = useEditableContext()
const showNewLayouts = useFeatureIsOn(FEATURE_FLAGS.HOMEPAGE_TEMPLATES)

return (
<>
Expand Down Expand Up @@ -381,6 +389,12 @@ export const HeroBody = ({
<Divider my="0.25rem" />
<HeroLayoutForm variant={variant}>
{({ currentSelectedOption }) => {
// NOTE: If the flag is turned off, we always show the centered layout
// as it is the current existing layout.
if (!showNewLayouts) {
return <HeroCenteredLayout {...rest} />
}

if (currentSelectedOption === HERO_LAYOUTS.CENTERED.value) {
return <HeroCenteredLayout {...rest} />
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/homepage/HeroSection/HeroSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const TemplateHeroSection = forwardRef<
hero,
dropdownIsActive,
toggleDropdown,
variant,
variant = "center",
}: TemplateHeroSectionProps,
ref
) => {
Expand Down

0 comments on commit 90ae047

Please sign in to comment.