Skip to content

Commit

Permalink
fix: pass all item data to hydrogen component loader
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-phan committed Jul 29, 2024
1 parent 0d1b542 commit a1643ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/hydrogen/src/WeaverseHydrogenRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Await, useLoaderData } from '@remix-run/react'
import { WeaverseRoot } from '@weaverse/react'
import { Suspense, memo } from 'react'
import { type ComponentType, type JSX, Suspense, memo } from 'react'
import { ErrorBoundary } from 'react-error-boundary'

import { WeaverseEffect } from '~/Effect'
Expand Down Expand Up @@ -62,8 +62,8 @@ function RenderRoot(props: {
)
}

export function withWeaverse(Component: React.ComponentType<any>) {
return (props: React.JSX.IntrinsicAttributes) => {
export function withWeaverse(Component: ComponentType<any>) {
return (props: JSX.IntrinsicAttributes) => {
let { settings } = useThemeSettingsStore()
return (
<ThemeSettingsProvider.Provider value={settings}>
Expand Down
22 changes: 11 additions & 11 deletions packages/hydrogen/src/weaverse-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ export class WeaverseClient {
throw new Error('Missing Weaverse projectId!')
}
let url = `${weaverseHost}/${API}/project_configs`
// biome-ignore lint/suspicious/noImplicitAnyLet: <explanation>
let res
let res: any
let body = JSON.stringify({ isDesignMode, projectId })
if (isDesignMode) {
res = await fetch(url, { method: 'POST', body }).then((res) =>
Expand Down Expand Up @@ -217,7 +216,7 @@ export class WeaverseClient {
let items = page.items
page.items = await Promise.all(items.map(this.execComponentLoader))
}
let data: WeaverseLoaderData = {
return {
page,
project,
pageAssignment,
Expand All @@ -232,7 +231,6 @@ export class WeaverseClient {
},
},
}
return data
} catch (e) {
// biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
console.error(`❌ Page load failed.`, e)
Expand All @@ -242,16 +240,18 @@ export class WeaverseClient {

execComponentLoader = async (item: HydrogenComponentData) => {
let { data = {}, type, id } = item
let schema = this.components.find(({ schema }) => schema?.type === type)
let loader = schema?.loader
if (loader && typeof loader === 'function') {
let component = this.components.find(({ schema }) => schema?.type === type)
let loader = component?.loader
if (typeof loader === 'function') {
try {
return {
...item,
loaderData: await loader({
data,
weaverse: this,
}),
loaderData: await Promise.resolve(
loader({
data: { ...generateDataFromSchema(component.schema), ...data },
weaverse: this,
}),
),
}
} catch (e) {
console.warn('❌ Item loader run failed.', type, id, e)
Expand Down

0 comments on commit a1643ae

Please sign in to comment.