-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta-demo.tsx
164 lines (156 loc) · 3.88 KB
/
meta-demo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {Image} from '@shopify/hydrogen';
import type {
ComponentLoaderArgs,
HydrogenComponentProps,
HydrogenComponentSchema,
} from '@weaverse/hydrogen';
import clsx from 'clsx';
import {forwardRef} from 'react';
import {METAOBJECTS_QUERY} from '~/data/queries';
type MetaObjectField = {
key: string;
type: string;
value: any;
reference?: any;
};
type MetaobjectData = {
handle: string;
id: string;
type: string;
fields: MetaObjectField[];
};
type MetaDemoProps = HydrogenComponentProps & {
metaDemo: {
id: string;
type: string;
};
title: string;
itemsPerRow: number;
gap: number;
};
let MetaDemo = forwardRef<HTMLDivElement, MetaDemoProps>((props, ref) => {
let {loaderData, metaDemo, title, itemsPerRow, gap, className, ...rest} =
props;
if (!metaDemo) {
return (
<section
className={clsx(
'w-full px-6 py-12 md:py-24 lg:py-32 bg-amber-50 mx-auto',
className,
)}
ref={ref}
{...rest}
>
<p className="text-center">Please select a metaobject definition</p>
</section>
);
}
let items = loaderData?.metaobjects.map(
(metaObject: MetaobjectData, ind: number) => {
let {fields} = metaObject;
let image = fields.find((field) => field.key === 'avatar');
let imageData = image?.reference?.image;
let name = fields.find((field) => field.key === 'name')?.value;
let title = fields.find((field) => field.key === 'title')?.value;
return (
<div key={ind} className="flex flex-col gap-2 items-center">
<div className="rounded-md overflow-hidden w-44">
<Image
data={imageData}
sizes="auto"
className="h-auto"
aspectRatio="1/1"
/>
</div>
<h3 className="font-semibold text-xl">{name}</h3>
<p>{title}</p>
</div>
);
},
);
return (
<section
className={clsx(
'max-w-7xl px-6 py-12 md:py-24 lg:py-32 w-fit mx-auto',
className,
)}
ref={ref}
{...rest}
>
<h2 className="text-center font-semibold mb-8">{title}</h2>
<div
className="grid w-fit mx-auto"
style={{
gridTemplateColumns: `repeat(${itemsPerRow}, minmax(0, 1fr))`,
gap: `${gap}px`,
}}
>
{items}
</div>
</section>
);
});
export let loader = async (args: ComponentLoaderArgs<MetaDemoProps>) => {
let {weaverse, data} = args;
let {storefront} = weaverse;
if (!data?.metaDemo) {
return null;
}
let {metaobjects} = await storefront.query(METAOBJECTS_QUERY, {
variables: {
type: data.metaDemo.type,
first: 10,
},
});
return {
metaobjects: metaobjects.nodes,
};
};
export let schema: HydrogenComponentSchema = {
type: 'meta-demo',
title: 'Metaobject Demo',
toolbar: ['general-settings', ['duplicate', 'delete']],
inspector: [
{
group: 'Metaobject Demo',
inputs: [
{
label: 'Select metaobject definition',
type: 'metaobject',
helpText:
'<a href="https://weaverse.io/docs/marketplace/the-pilot-theme#metaobjects" target="_blank">How to display this demo section</a>',
name: 'metaDemo',
shouldRevalidate: true,
},
{
label: 'Title',
type: 'text',
name: 'title',
defaultValue: 'Title',
},
{
label: 'Items per row',
name: 'itemsPerRow',
type: 'range',
configs: {
min: 1,
max: 10,
},
defaultValue: 3,
},
{
label: 'Gap between items',
name: 'gap',
type: 'range',
configs: {
min: 0,
step: 2,
max: 100,
},
defaultValue: 10,
},
],
},
],
};
export default MetaDemo;