Skip to content

Commit

Permalink
fix: render nothing on website when collection is invalid
Browse files Browse the repository at this point in the history
User got 500 on published site. Turned out collection got invalid
data (not array). We need to keep site working in such cases, missing
piece will be more intuitive than 500 on whole website.
  • Loading branch information
TrySound committed Feb 2, 2025
1 parent b9f84e7 commit 93d2933
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/react-sdk/src/component-generator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ test("generate collection component as map", () => {
).toEqual(
validateJSX(
clear(`
{data?.map((element: any, index: number) =>
{data?.map?.((element: any, index: number) =>
<Fragment key={index}>
<Label />
<Button
Expand Down Expand Up @@ -636,7 +636,7 @@ test("avoid generating collection parameter variable as state", () => {
const Page = () => {
let [data, set$data] = useVariableState<any>(["apple","orange","mango"])
return <Body>
{data?.map((element: any, index: number) =>
{data?.map?.((element: any, index: number) =>
<Fragment key={index}>
</Fragment>
)}
Expand Down Expand Up @@ -854,7 +854,7 @@ let [condition, set$condition] = useVariableState<any>(false)
return <Body>
{(condition) &&
<>
{[]?.map((collectionItem: any, index: number) =>
{[]?.map?.((collectionItem: any, index: number) =>
<Fragment key={index}>
</Fragment>
)}
Expand Down
5 changes: 3 additions & 2 deletions packages/react-sdk/src/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ export const generateJsxElement = ({
return "";
}
const indexVariable = scope.getName(`${instance.id}-index`, "index");
// fix implicit any error
generatedElement += `{${collectionDataValue}?.map((${collectionItemValue}: any, ${indexVariable}: number) =>\n`;
// collection can be nullable or invalid type
// fix implicitly on published sites
generatedElement += `{${collectionDataValue}?.map?.((${collectionItemValue}: any, ${indexVariable}: number) =>\n`;
generatedElement += `<Fragment key={${indexVariable}}>\n`;
generatedElement += children;
generatedElement += `</Fragment>\n`;
Expand Down

0 comments on commit 93d2933

Please sign in to comment.