-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make product details visually editable
- Loading branch information
1 parent
dc96609
commit 5b1c0a6
Showing
7 changed files
with
149 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use client'; | ||
|
||
import React, { | ||
type ComponentPropsWithoutRef, | ||
createContext, | ||
forwardRef, | ||
type PropsWithChildren, | ||
type ReactNode, | ||
type Ref, | ||
useContext, | ||
useMemo, | ||
} from 'react'; | ||
|
||
import { ProductDetail } from '@/vibes/soul/sections/product-detail'; | ||
|
||
type VibesProductDetailProps = ComponentPropsWithoutRef<typeof ProductDetail>; | ||
|
||
type Props = VibesProductDetailProps & { | ||
product: VibesProductDetailProps['product'] & { | ||
description: string; | ||
plainTextDescription: string; | ||
}; | ||
}; | ||
|
||
const PropsContext = createContext<Props | null>(null); | ||
|
||
export const PropsContextProvider = ({ value, children }: PropsWithChildren<{ value: Props }>) => ( | ||
<PropsContext.Provider value={value}>{children}</PropsContext.Provider> | ||
); | ||
|
||
export const DescriptionType = { | ||
PlainText: 'PlainText', | ||
RichText: 'RichText', | ||
Custom: 'Custom', | ||
} as const; | ||
|
||
type DescriptionType = (typeof DescriptionType)[keyof typeof DescriptionType]; | ||
|
||
export const MakeswiftProductDetail = forwardRef( | ||
( | ||
{ | ||
descriptionType, | ||
slot, | ||
}: { | ||
descriptionType: DescriptionType; | ||
slot: ReactNode; | ||
}, | ||
ref: Ref<HTMLDivElement>, | ||
) => { | ||
const context = useContext(PropsContext); | ||
const description = useMemo(() => { | ||
const product = context?.product; | ||
if (product == null) return null; | ||
|
||
switch (descriptionType) { | ||
case DescriptionType.PlainText: | ||
return product.plainTextDescription; | ||
case DescriptionType.RichText: | ||
return ( | ||
<div className="prose" dangerouslySetInnerHTML={{ __html: product.description }} /> | ||
); | ||
case DescriptionType.Custom: | ||
return slot; | ||
} | ||
}, [descriptionType, context?.product, slot]); | ||
|
||
if (context == null) { | ||
console.error('No context provided for MakeswiftProductDetail'); | ||
return <p ref={ref}>There was an error rendering the product detail.</p>; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col" ref={ref}> | ||
<ProductDetail | ||
{...context} | ||
product={{ | ||
...context.product, | ||
description, | ||
}} | ||
/> | ||
</div> | ||
); | ||
}, | ||
); |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { MakeswiftComponent } from '@makeswift/runtime/next'; | ||
import { type ComponentPropsWithoutRef } from 'react'; | ||
|
||
import { ProductDetail as VibesProductDetail } from '@/vibes/soul/sections/product-detail'; | ||
import { getComponentSnapshot } from '~/lib/makeswift/client'; | ||
|
||
import { PropsContextProvider } from './client'; | ||
import { COMPONENT_TYPE } from './register'; | ||
|
||
type VibesProductDetailProps = ComponentPropsWithoutRef<typeof VibesProductDetail>; | ||
|
||
type Props = VibesProductDetailProps & { | ||
product: VibesProductDetailProps['product'] & { | ||
description: string; | ||
plainTextDescription: string; | ||
}; | ||
}; | ||
|
||
export const ProductDetail = async (props: Props) => { | ||
const snapshot = await getComponentSnapshot(`product-detail-${props.product.id}`); | ||
|
||
return ( | ||
<PropsContextProvider value={props}> | ||
<MakeswiftComponent | ||
label={`Detail for ${props.product.title}`} | ||
snapshot={snapshot} | ||
type={COMPONENT_TYPE} | ||
/> | ||
</PropsContextProvider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Select, Slot } from '@makeswift/runtime/controls'; | ||
|
||
import { runtime } from '~/lib/makeswift/runtime'; | ||
|
||
import { DescriptionType, MakeswiftProductDetail } from './client'; | ||
|
||
export const COMPONENT_TYPE = 'catalyst-makeswift-product-detail-description'; | ||
|
||
runtime.registerComponent(MakeswiftProductDetail, { | ||
type: COMPONENT_TYPE, | ||
label: 'MakeswiftProductDetail (private)', | ||
hidden: true, | ||
props: { | ||
descriptionType: Select({ | ||
label: 'Description', | ||
options: [ | ||
{ label: 'Catalog (plain text)', value: DescriptionType.PlainText }, | ||
{ label: 'Catalog (rich text)', value: DescriptionType.RichText }, | ||
{ label: 'Custom', value: DescriptionType.Custom }, | ||
], | ||
defaultValue: DescriptionType.PlainText, | ||
}), | ||
slot: Slot(), | ||
}, | ||
}); |
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