-
Notifications
You must be signed in to change notification settings - Fork 187
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
feat: visually editable PDP #1808
Merged
agurtovoy
merged 1 commit into
soul/integrations/makeswift
from
aleksey/vib-908-pdp-visual-editing
Jan 8, 2025
+278
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
125 changes: 125 additions & 0 deletions
125
core/lib/makeswift/components/product-detail/client.tsx
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,125 @@ | ||
'use client'; | ||
|
||
import React, { | ||
type ComponentPropsWithoutRef, | ||
createContext, | ||
forwardRef, | ||
type PropsWithChildren, | ||
type ReactNode, | ||
type Ref, | ||
useCallback, | ||
useContext, | ||
} from 'react'; | ||
|
||
import { Stream, type Streamable } from '@/vibes/soul/lib/streamable'; | ||
import { ProductDetail, ProductDetailSkeleton } from '@/vibes/soul/sections/product-detail'; | ||
import { mergeSections } from '~/lib/makeswift/utils/merge-sections'; | ||
|
||
type VibesProductDetailProps = ComponentPropsWithoutRef<typeof ProductDetail>; | ||
type VibesProductDetail = Exclude<Awaited<VibesProductDetailProps['product']>, null>; | ||
|
||
export type ProductDetail = VibesProductDetail & { | ||
plainTextDescription?: string; | ||
}; | ||
|
||
export type Props = Omit<VibesProductDetailProps, 'product'> & { | ||
product: Streamable<ProductDetail>; | ||
}; | ||
|
||
const PropsContext = createContext<Props | null>(null); | ||
|
||
export const PropsContextProvider = ({ value, children }: PropsWithChildren<{ value: Props }>) => ( | ||
<PropsContext.Provider value={value}>{children}</PropsContext.Provider> | ||
); | ||
|
||
export const DescriptionSource = { | ||
CatalogPlainText: 'CatalogPlainText', | ||
CatalogRichText: 'CatalogRichText', | ||
Custom: 'Custom', | ||
} as const; | ||
|
||
type DescriptionSource = (typeof DescriptionSource)[keyof typeof DescriptionSource]; | ||
|
||
interface EditableProps { | ||
summaryText: string | undefined; | ||
description: { source: DescriptionSource; slot: ReactNode }; | ||
accordions: Exclude<Awaited<VibesProductDetail['accordions']>, undefined>; | ||
} | ||
|
||
const ProductDetailImpl = ({ | ||
summaryText, | ||
description, | ||
accordions, | ||
product: streamableProduct, | ||
...props | ||
}: Props & EditableProps) => { | ||
const getProductDescription = useCallback( | ||
(product: ProductDetail): ProductDetail['description'] => { | ||
switch (description.source) { | ||
case DescriptionSource.CatalogPlainText: | ||
return product.plainTextDescription; | ||
|
||
case DescriptionSource.CatalogRichText: | ||
return product.description; | ||
|
||
case DescriptionSource.Custom: | ||
return description.slot; | ||
} | ||
}, | ||
[description.source, description.slot], | ||
); | ||
|
||
const getProductAccordions = useCallback( | ||
( | ||
productAccordions: Awaited<ProductDetail['accordions']>, | ||
): Awaited<ProductDetail['accordions']> => | ||
productAccordions != null | ||
? mergeSections(productAccordions, accordions, (left, right) => ({ | ||
...left, | ||
content: right.content, | ||
})) | ||
: undefined, | ||
[accordions], | ||
); | ||
|
||
return ( | ||
<Stream fallback={<ProductDetailSkeleton />} value={streamableProduct}> | ||
{(product) => ( | ||
<Stream fallback={<ProductDetailSkeleton />} value={product.accordions}> | ||
{(productAccordions) => ( | ||
agurtovoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<ProductDetail | ||
{...{ | ||
...props, | ||
product: { | ||
...product, | ||
summary: summaryText, | ||
description: getProductDescription(product), | ||
accordions: getProductAccordions(productAccordions), | ||
}, | ||
}} | ||
/> | ||
)} | ||
</Stream> | ||
)} | ||
</Stream> | ||
); | ||
}; | ||
|
||
export const MakeswiftProductDetail = forwardRef( | ||
(props: EditableProps, ref: Ref<HTMLDivElement>) => { | ||
const passedProps = useContext(PropsContext); | ||
|
||
if (passedProps == null) { | ||
// eslint-disable-next-line no-console | ||
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}> | ||
<ProductDetailImpl {...{ ...passedProps, ...props }} /> | ||
</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,17 @@ | ||
import { Component } from '~/lib/makeswift/component'; | ||
|
||
import { type Props as ClientProps, PropsContextProvider } from './client'; | ||
import { COMPONENT_TYPE } from './register'; | ||
|
||
type Props = ClientProps & { productId: number }; | ||
|
||
export const ProductDetail = ({ productId, ...props }: Props) => ( | ||
<PropsContextProvider value={props}> | ||
<Component | ||
label={(async () => | ||
`Detail for ${await Promise.resolve(props.product).then(({ title }) => title)}`)()} | ||
snapshotId={`product-detail-${productId}`} | ||
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,46 @@ | ||
import { List, Select, Shape, Slot, TextArea, TextInput } from '@makeswift/runtime/controls'; | ||
|
||
import { runtime } from '~/lib/makeswift/runtime'; | ||
|
||
import { DescriptionSource, MakeswiftProductDetail } from './client'; | ||
|
||
export const COMPONENT_TYPE = 'catalyst-makeswift-product-detail-description'; | ||
|
||
const description = Shape({ | ||
label: 'Description', | ||
type: { | ||
source: Select({ | ||
label: 'Source', | ||
options: [ | ||
{ label: 'Catalog (plain text)', value: DescriptionSource.CatalogPlainText }, | ||
{ label: 'Catalog (rich text)', value: DescriptionSource.CatalogRichText }, | ||
{ label: 'Custom', value: DescriptionSource.Custom }, | ||
], | ||
defaultValue: DescriptionSource.CatalogRichText, | ||
}), | ||
slot: Slot(), | ||
}, | ||
}); | ||
|
||
runtime.registerComponent(MakeswiftProductDetail, { | ||
type: COMPONENT_TYPE, | ||
label: 'MakeswiftProductDetail (private)', | ||
hidden: true, | ||
props: { | ||
summaryText: TextArea({ | ||
label: 'Summary', | ||
}), | ||
description, | ||
accordions: List({ | ||
label: 'Product info', | ||
type: Shape({ | ||
label: 'Product info section', | ||
type: { | ||
title: TextInput({ label: 'Title', defaultValue: 'Section' }), | ||
content: Slot(), | ||
}, | ||
}), | ||
getItemLabel: (section) => section?.title || 'Section', | ||
}), | ||
}, | ||
}); |
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 |
---|---|---|
|
@@ -106,7 +106,7 @@ export function ProductDetail<F extends Field>({ | |
|
||
<Stream | ||
fallback={<ProductDetailFormSkeleton />} | ||
value={Promise.all([ | ||
value={Streamable.all([ | ||
streamableFields, | ||
streamableCtaLabel, | ||
streamableCtaDisabled, | ||
|
@@ -129,8 +129,7 @@ export function ProductDetail<F extends Field>({ | |
|
||
<Stream fallback={<ProductDescriptionSkeleton />} value={product.description}> | ||
{(description) => | ||
description !== null && | ||
description !== undefined && ( | ||
description != null && ( | ||
<div className="border-t border-contrast-100 py-8 text-contrast-500"> | ||
{description} | ||
</div> | ||
|
@@ -284,7 +283,7 @@ function ProductAccordionsSkeleton() { | |
); | ||
} | ||
|
||
function ProductDetailSkeleton() { | ||
export function ProductDetailSkeleton() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: put up a PR for VIBES that exports this. |
||
return ( | ||
<div className="grid animate-pulse grid-cols-1 items-stretch gap-x-6 gap-y-8 @2xl:grid-cols-2 @5xl:gap-x-12"> | ||
<div className="hidden @2xl:block"> | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow label to be a promise so we can provide a descriptive label here.
Note that waiting for the label to resolve below feels a bit off as strictly speaking label is not required for rendering the snapshot element tree and could theoretically be resolved in parallel. Might be something we want to pursue later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
Streamable<string>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to make this dependent on Vibes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catalyst itself depends on VIBES so this shouldn't be an issue. We're also planning on creating a
react-streamable
library and Catalyst will have it as it will be required to use with VIBES. Not a big deal, thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it does, but I view the collection of "userland" Makeswift boilerplate in this dir as a prototype for our updated integration guidelines, and I don't think they should depend on VIBES.