sanity-plugin-shopify-assets 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # sanity-plugin-shopify-assets
2
2
 
3
- > This is a **Sanity Studio v3** plugin.
4
-
5
3
  Select assets from your Shopify store in the context of your Sanity Studio. This plugin allows you to serve assets from the Shopify CDN in your frontends.
6
4
 
7
5
  ## Installation
package/dist/index.esm.js CHANGED
@@ -1,6 +1,4 @@
1
- var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6;
2
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
- import { useProjectId, useDataset, PatchEvent, set, unset, DiffFromTo, defineType, defineField, definePlugin } from 'sanity';
1
+ import { useProjectId, useDataset, useClient, PatchEvent, set, unset, DiffFromTo, defineType, defineField, definePlugin } from 'sanity';
4
2
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
3
  import { LaunchIcon, ErrorOutlineIcon } from '@sanity/icons';
6
4
  import { Card, Box, Text, Flex, Button, Stack, Dialog, Inline, TextInput, Spinner, Grid } from '@sanity/ui';
@@ -14,29 +12,81 @@ import PhotoAlbum from 'react-photo-album';
14
12
  import InfiniteScroll from 'react-infinite-scroll-component';
15
13
  import { withLatestFrom, debounceTime, distinctUntilChanged, switchMap, map } from 'rxjs/operators';
16
14
  import axios from 'axios';
17
- const Root = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n overflow: hidden;\n background-origin: content-box;\n background-repeat: no-repeat;\n background-clip: border-box;\n background-size: cover;\n background-color: ", ";\n position: relative;\n outline: none !important;\n border: ", ";\n box-sizing: content-box;\n user-drag: none;\n\n &:hover {\n opacity: 0.85;\n }\n\n &:focus,\n &:active {\n border: 1px solid var(--input-border-color-focus);\n box-shadow: inset 0 0 0 3px var(--input-border-color-focus);\n }\n"])), _ref => {
15
+ const Root = styled.div`
16
+ overflow: hidden;
17
+ background-origin: content-box;
18
+ background-repeat: no-repeat;
19
+ background-clip: border-box;
20
+ background-size: cover;
21
+ background-color: ${_ref => {
18
22
  let {
19
23
  theme
20
24
  } = _ref;
21
25
  return theme.sanity.color.card.enabled.bg2;
22
- }, _ref2 => {
26
+ }};
27
+ position: relative;
28
+ outline: none !important;
29
+ border: ${_ref2 => {
23
30
  let {
24
31
  theme
25
32
  } = _ref2;
26
- return "1px solid ".concat(theme.sanity.color.card.enabled.border);
27
- });
28
- const InfoLine = styled(Card)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n ", ";\n user-drag: none;\n position: absolute;\n background-color: var(--infoline-bg);\n top: 0;\n left: 0;\n max-width: 65%;\n overflow-wrap: break-word;\n\n [data-ui='Text'] {\n color: var(--infoline-fg);\n }\n"])), _ref3 => {
33
+ return `1px solid ${theme.sanity.color.card.enabled.border}`;
34
+ }};
35
+ box-sizing: content-box;
36
+ user-drag: none;
37
+
38
+ &:hover {
39
+ opacity: 0.85;
40
+ }
41
+
42
+ &:focus,
43
+ &:active {
44
+ border: 1px solid var(--input-border-color-focus);
45
+ box-shadow: inset 0 0 0 3px var(--input-border-color-focus);
46
+ }
47
+ `;
48
+ const InfoLine = styled(Card)`
49
+ ${_ref3 => {
29
50
  let {
30
51
  theme
31
52
  } = _ref3;
32
- return "\n --infoline-fg: ".concat(theme.sanity.color.card.enabled.fg, ";\n --infoline-bg: ").concat(theme.sanity.color.card.enabled.bg, ";\n ");
33
- });
34
- const DurationLine = styled(Card)(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n ", ";\n user-drag: none;\n position: absolute;\n background-color: var(--durationline-bg);\n top: 0;\n right: 0;\n\n [data-ui='Text'] {\n color: var(--durationline-fg);\n }\n"])), _ref4 => {
53
+ return `
54
+ --infoline-fg: ${theme.sanity.color.card.enabled.fg};
55
+ --infoline-bg: ${theme.sanity.color.card.enabled.bg};
56
+ `;
57
+ }};
58
+ user-drag: none;
59
+ position: absolute;
60
+ background-color: var(--infoline-bg);
61
+ top: 0;
62
+ left: 0;
63
+ max-width: 65%;
64
+ overflow-wrap: break-word;
65
+
66
+ [data-ui='Text'] {
67
+ color: var(--infoline-fg);
68
+ }
69
+ `;
70
+ const DurationLine = styled(Card)`
71
+ ${_ref4 => {
35
72
  let {
36
73
  theme
37
74
  } = _ref4;
38
- return "\n --durationline-fg: ".concat(theme.sanity.color.card.enabled.bg, ";\n --durationline-bg: ").concat(theme.sanity.color.card.enabled.fg, ";\n ");
39
- });
75
+ return `
76
+ --durationline-fg: ${theme.sanity.color.card.enabled.bg};
77
+ --durationline-bg: ${theme.sanity.color.card.enabled.fg};
78
+ `;
79
+ }};
80
+ user-drag: none;
81
+ position: absolute;
82
+ background-color: var(--durationline-bg);
83
+ top: 0;
84
+ right: 0;
85
+
86
+ [data-ui='Text'] {
87
+ color: var(--durationline-fg);
88
+ }
89
+ `;
40
90
  const VideoPlayer = _ref5 => {
41
91
  let {
42
92
  src,
@@ -45,8 +95,7 @@ const VideoPlayer = _ref5 => {
45
95
  const videoNode = React.useRef(null);
46
96
  const player = React.useRef();
47
97
  useEffect(() => {
48
- var _a;
49
- player.current = videojs((_a = videoNode.current) != null ? _a : "", {
98
+ player.current = videojs(videoNode.current ?? "", {
50
99
  sources: [{
51
100
  src
52
101
  }],
@@ -81,30 +130,34 @@ const VideoPlayer = _ref5 => {
81
130
  })]
82
131
  });
83
132
  };
84
- const StyledBox = styled(Box)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n background-color: ", ";\n border: ", ";\n display: flex;\n justify-content: center;\n margin-bottom: ", ";\n position: relative;\n"])), _ref6 => {
133
+ const StyledBox = styled(Box)`
134
+ background-color: ${_ref6 => {
85
135
  let {
86
136
  theme
87
137
  } = _ref6;
88
- var _a, _b, _c;
89
- return (_c = (_b = (_a = theme.sanity.color) == null ? void 0 : _a.card) == null ? void 0 : _b.enabled) == null ? void 0 : _c.bg2;
90
- }, _ref7 => {
138
+ return theme.sanity.color?.card?.enabled?.bg2;
139
+ }};
140
+ border: ${_ref7 => {
91
141
  let {
92
142
  theme
93
143
  } = _ref7;
94
- var _a, _b, _c, _d;
95
- return "1px solid ".concat((_d = (_c = (_b = (_a = theme.sanity) == null ? void 0 : _a.color) == null ? void 0 : _b.card) == null ? void 0 : _c.enabled) == null ? void 0 : _d.border);
96
- }, _ref8 => {
144
+ return `1px solid ${theme.sanity?.color?.card?.enabled?.border}`;
145
+ }};
146
+ display: flex;
147
+ justify-content: center;
148
+ margin-bottom: ${_ref8 => {
97
149
  let {
98
150
  theme
99
151
  } = _ref8;
100
152
  return theme.sanity.space[4];
101
- });
153
+ }};
154
+ position: relative;
155
+ `;
102
156
  const RenderAsset = _ref9 => {
103
157
  let {
104
158
  value,
105
159
  url
106
160
  } = _ref9;
107
- var _a;
108
161
  switch (value.type) {
109
162
  case "video":
110
163
  return /* @__PURE__ */jsx(VideoPlayer, {
@@ -116,7 +169,7 @@ const RenderAsset = _ref9 => {
116
169
  justify: "center",
117
170
  children: /* @__PURE__ */jsx("img", {
118
171
  alt: "preview",
119
- src: (_a = value == null ? void 0 : value.preview) == null ? void 0 : _a.url,
172
+ src: value?.preview?.url,
120
173
  style: {
121
174
  maxWidth: "100%",
122
175
  height: "auto",
@@ -127,10 +180,10 @@ const RenderAsset = _ref9 => {
127
180
  });
128
181
  }
129
182
  };
130
- const AssetPreview = _ref10 => {
183
+ const AssetPreview = _ref0 => {
131
184
  let {
132
185
  value
133
- } = _ref10;
186
+ } = _ref0;
134
187
  const url = value && value.url;
135
188
  if (!value || !url) {
136
189
  return null;
@@ -154,8 +207,8 @@ const AssetPreview = _ref10 => {
154
207
  margin: 2,
155
208
  children: /* @__PURE__ */jsxs(Text, {
156
209
  size: 1,
157
- title: "Select ".concat(filename),
158
- children: [filename, " ", fileSize && "(".concat(prettyBytes(fileSize), ")")]
210
+ title: `Select ${filename}`,
211
+ children: [filename, " ", fileSize && `(${prettyBytes(fileSize)})`]
159
212
  })
160
213
  }), duration && /* @__PURE__ */jsx(DurationLine, {
161
214
  padding: 2,
@@ -163,7 +216,7 @@ const AssetPreview = _ref10 => {
163
216
  margin: 2,
164
217
  children: /* @__PURE__ */jsx(Text, {
165
218
  size: 1,
166
- title: "Video duration: ".concat(filename),
219
+ title: `Video duration: ${filename}`,
167
220
  children: prettyMilliseconds(duration, {
168
221
  colonNotation: true,
169
222
  secondsDecimalDigits: 0
@@ -179,12 +232,16 @@ const fetchSearch = props => {
179
232
  shop,
180
233
  query,
181
234
  cursor,
182
- resultsPerPage
235
+ resultsPerPage,
236
+ token
183
237
  } = props;
184
238
  return defer(() => {
185
- return axios.get("https://".concat(projectId, ".api.sanity.io/v1/shopify/assets/").concat(dataset, "?shop=").concat(shop, "&query=").concat(encodeURIComponent(query)).concat(cursor && "&cursor=".concat(cursor), "&limit=").concat(resultsPerPage), {
239
+ return axios.get(`https://${projectId}.api.sanity.io/v1/shopify/assets/${dataset}?shop=${shop}&query=${encodeURIComponent(query)}${cursor && `&cursor=${cursor}`}&limit=${resultsPerPage}`, {
186
240
  withCredentials: true,
187
- method: "GET"
241
+ method: "GET",
242
+ headers: token ? {
243
+ Authorization: `Bearer ${token}`
244
+ } : {}
188
245
  });
189
246
  }).pipe(map(result => result.data));
190
247
  };
@@ -194,11 +251,15 @@ const fetchList = props => {
194
251
  dataset,
195
252
  shop,
196
253
  cursor,
197
- resultsPerPage
254
+ resultsPerPage,
255
+ token
198
256
  } = props;
199
- return defer(() => axios.get("https://".concat(projectId, ".api.sanity.io/v1/shopify/assets/").concat(dataset, "?shop=").concat(shop).concat(cursor && "&cursor=".concat(cursor), "&limit=").concat(resultsPerPage), {
257
+ return defer(() => axios.get(`https://${projectId}.api.sanity.io/v1/shopify/assets/${dataset}?shop=${shop}${cursor && `&cursor=${cursor}`}&limit=${resultsPerPage}`, {
200
258
  withCredentials: true,
201
- method: "GET"
259
+ method: "GET",
260
+ headers: token ? {
261
+ Authorization: `Bearer ${token}`
262
+ } : {}
202
263
  })).pipe(map(result => result.data));
203
264
  };
204
265
  const search = props => {
@@ -208,10 +269,11 @@ const search = props => {
208
269
  shop,
209
270
  query,
210
271
  cursor,
211
- resultsPerPage
272
+ resultsPerPage,
273
+ token
212
274
  } = props;
213
- return concat(query.pipe(withLatestFrom(cursor), debounceTime(500), distinctUntilChanged(), switchMap(_ref11 => {
214
- let [q, c] = _ref11;
275
+ return concat(query.pipe(withLatestFrom(cursor), debounceTime(500), distinctUntilChanged(), switchMap(_ref1 => {
276
+ let [q, c] = _ref1;
215
277
  if (q) {
216
278
  return fetchSearch({
217
279
  projectId,
@@ -219,7 +281,8 @@ const search = props => {
219
281
  shop,
220
282
  query: q,
221
283
  cursor: c,
222
- resultsPerPage
284
+ resultsPerPage,
285
+ token
223
286
  }).pipe(distinctUntilChanged());
224
287
  }
225
288
  return fetchList({
@@ -227,7 +290,8 @@ const search = props => {
227
290
  dataset,
228
291
  shop,
229
292
  cursor: c,
230
- resultsPerPage
293
+ resultsPerPage,
294
+ token
231
295
  });
232
296
  })));
233
297
  };
@@ -237,7 +301,7 @@ const DialogHeader = props => {
237
301
  shopifyDomain
238
302
  } = props;
239
303
  const handleOpenInNewTab = useCallback(() => {
240
- window.open("https://".concat(shopifyDomain, "/admin/settings/files"), "_blank");
304
+ window.open(`https://${shopifyDomain}/admin/settings/files`, "_blank");
241
305
  }, [shopifyDomain]);
242
306
  return /* @__PURE__ */jsxs(Flex, {
243
307
  align: "center",
@@ -265,10 +329,7 @@ const DialogHeader = props => {
265
329
  })]
266
330
  });
267
331
  };
268
- const extractName = name => {
269
- var _a, _b, _c;
270
- return (_c = (_b = (_a = name == null ? void 0 : name.split("/")) == null ? void 0 : _a.pop()) == null ? void 0 : _b.split("?")[0]) != null ? _c : "";
271
- };
332
+ const extractName = name => name?.split("/")?.pop()?.split("?")[0] ?? "";
272
333
  function File(props) {
273
334
  const {
274
335
  onClick,
@@ -290,12 +351,12 @@ function File(props) {
290
351
  }, [onClick, data, filename]);
291
352
  return /* @__PURE__ */jsxs(Root, {
292
353
  ref: rootElm,
293
- title: "".concat(filename),
354
+ title: `${filename}`,
294
355
  tabIndex: 0,
295
356
  style: {
296
- width: "".concat(width, "px"),
297
- height: "".concat(height, "px"),
298
- backgroundImage: "url(\"".concat(preview == null ? void 0 : preview.url, "\")")
357
+ width: `${width}px`,
358
+ height: `${height}px`,
359
+ backgroundImage: `url("${preview?.url}")`
299
360
  },
300
361
  onClick: handleClick,
301
362
  children: [/* @__PURE__ */jsx(InfoLine, {
@@ -304,8 +365,8 @@ function File(props) {
304
365
  margin: 2,
305
366
  children: /* @__PURE__ */jsxs(Text, {
306
367
  size: 1,
307
- title: "Select ".concat(filename),
308
- children: [filename, " ", meta.fileSize && "(".concat(prettyBytes(meta.fileSize), ")")]
368
+ title: `Select ${filename}`,
369
+ children: [filename, " ", meta.fileSize && `(${prettyBytes(meta.fileSize)})`]
309
370
  })
310
371
  }), meta.duration && /* @__PURE__ */jsx(DurationLine, {
311
372
  padding: 2,
@@ -313,7 +374,7 @@ function File(props) {
313
374
  margin: 2,
314
375
  children: /* @__PURE__ */jsx(Text, {
315
376
  size: 1,
316
- title: "Video duration: ".concat(filename),
377
+ title: `Video duration: ${filename}`,
317
378
  children: prettyMilliseconds(meta.duration, {
318
379
  colonNotation: true,
319
380
  secondsDecimalDigits: 0
@@ -322,8 +383,15 @@ function File(props) {
322
383
  })]
323
384
  });
324
385
  }
325
- const Search = styled(Stack)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n position: sticky;\n top: 0;\n z-index: 1;\n"])));
326
- styled.div(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n overflow-y: auto;\n max-height: 80vh;\n"])));
386
+ const Search = styled(Stack)`
387
+ position: sticky;
388
+ top: 0;
389
+ z-index: 1;
390
+ `;
391
+ styled.div`
392
+ overflow-y: auto;
393
+ max-height: 80vh;
394
+ `;
327
395
  const RESULTS_PER_PAGE = 42;
328
396
  const PHOTO_SPACING = 2;
329
397
  const PHOTO_PADDING = 1;
@@ -338,6 +406,10 @@ function ShopifyAssetPicker(props) {
338
406
  } = props;
339
407
  const projectId = useProjectId();
340
408
  const dataset = useDataset();
409
+ const client = useClient({
410
+ apiVersion: "2021-06-07"
411
+ });
412
+ const token = client.config().token;
341
413
  const [error, setError] = useState("");
342
414
  const [query, setQuery] = useState("");
343
415
  const [searchResults, setSearchResults] = useState([]);
@@ -355,7 +427,8 @@ function ShopifyAssetPicker(props) {
355
427
  shop: shopifyDomain,
356
428
  query: searchSubject$,
357
429
  cursor: cursorSubject$,
358
- resultsPerPage: RESULTS_PER_PAGE
430
+ resultsPerPage: RESULTS_PER_PAGE,
431
+ token
359
432
  }).subscribe({
360
433
  next: results => {
361
434
  setSearchResults(prevResults => [...prevResults, ...results.assets]);
@@ -363,11 +436,11 @@ function ShopifyAssetPicker(props) {
363
436
  setIsLoading(false);
364
437
  },
365
438
  error: err => {
366
- setError("".concat(err.response.data.message || err.message || "An error occurred", " - check plugin configuration"));
439
+ setError(`${err.response?.data?.message || err.message || "An error occurred"} - check plugin configuration`);
367
440
  }
368
441
  });
369
442
  return () => searchSubscription.unsubscribe();
370
- }, [searchSubject$, cursorSubject$, shopifyDomain, projectId, dataset]);
443
+ }, [searchSubject$, cursorSubject$, shopifyDomain, projectId, dataset, token]);
371
444
  const handleSearchTermChanged = useCallback(event => {
372
445
  const newQuery = event.currentTarget.value;
373
446
  setQuery(newQuery);
@@ -383,11 +456,11 @@ function ShopifyAssetPicker(props) {
383
456
  searchSubject$.next(query);
384
457
  }, [cursorSubject$, pageInfo, searchSubject$, query]);
385
458
  const handleSelect = useCallback(file => {
386
- file._key = value == null ? void 0 : value._key;
459
+ file._key = value?._key;
387
460
  file._type = schemaType.name;
388
461
  onChange(PatchEvent.from([set(file)]));
389
462
  onClose();
390
- }, [onChange, onClose, schemaType.name, value == null ? void 0 : value._key]);
463
+ }, [onChange, onClose, schemaType.name, value?._key]);
391
464
  const renderFile = useCallback(fileProps => {
392
465
  const {
393
466
  photo,
@@ -458,7 +531,7 @@ function ShopifyAssetPicker(props) {
458
531
  }), /* @__PURE__ */jsx(InfiniteScroll, {
459
532
  dataLength: searchResults.length,
460
533
  next: handleScollerLoadMore,
461
- hasMore: pageInfo ? pageInfo == null ? void 0 : pageInfo.hasNextPage : true,
534
+ hasMore: pageInfo ? pageInfo?.hasNextPage : true,
462
535
  scrollThreshold: 0.99,
463
536
  height: "60vh",
464
537
  loader: /* @__PURE__ */jsx(Flex, {
@@ -484,21 +557,18 @@ function ShopifyAssetPicker(props) {
484
557
  spacing: PHOTO_SPACING,
485
558
  padding: PHOTO_PADDING,
486
559
  targetRowHeight: handleWidth,
487
- photos: searchResults.map(file => {
488
- var _a, _b, _c;
489
- return {
490
- src: (_a = file == null ? void 0 : file.preview) == null ? void 0 : _a.url,
491
- width: ((_b = file == null ? void 0 : file.preview) == null ? void 0 : _b.width) || 2048,
492
- height: ((_c = file == null ? void 0 : file.preview) == null ? void 0 : _c.height) || 2048,
493
- key: file.id,
494
- data: file
495
- };
496
- }),
560
+ photos: searchResults.map(file => ({
561
+ src: file?.preview?.url,
562
+ width: file?.preview?.width || 2048,
563
+ height: file?.preview?.height || 2048,
564
+ key: file.id,
565
+ data: file
566
+ })),
497
567
  renderPhoto: renderFile,
498
568
  componentsProps: {
499
569
  containerProps: {
500
570
  style: {
501
- marginBottom: "".concat(PHOTO_SPACING, "px")
571
+ marginBottom: `${PHOTO_SPACING}px`
502
572
  }
503
573
  }
504
574
  }
@@ -607,15 +677,14 @@ function ShopifyAssetInput(props) {
607
677
  })]
608
678
  });
609
679
  }
610
- const CloudinaryDiffPreview = _ref12 => {
680
+ const CloudinaryDiffPreview = _ref10 => {
611
681
  let {
612
682
  value
613
- } = _ref12;
614
- var _a, _b;
683
+ } = _ref10;
615
684
  if (!value) {
616
685
  return null;
617
686
  }
618
- if ((_a = value == null ? void 0 : value.preview) == null ? void 0 : _a.url) {
687
+ if (value?.preview?.url) {
619
688
  return /* @__PURE__ */jsx(Flex, {
620
689
  justify: "center",
621
690
  align: "center",
@@ -625,7 +694,7 @@ const CloudinaryDiffPreview = _ref12 => {
625
694
  space: 2,
626
695
  children: [/* @__PURE__ */jsx("img", {
627
696
  alt: "preview",
628
- src: (_b = value == null ? void 0 : value.preview) == null ? void 0 : _b.url,
697
+ src: value?.preview?.url,
629
698
  style: {
630
699
  objectFit: "contain",
631
700
  margin: "auto",
@@ -649,11 +718,11 @@ const CloudinaryDiffPreview = _ref12 => {
649
718
  })
650
719
  });
651
720
  };
652
- const AssetDiff = _ref13 => {
721
+ const AssetDiff = _ref11 => {
653
722
  let {
654
723
  diff,
655
724
  schemaType
656
- } = _ref13;
725
+ } = _ref11;
657
726
  return /* @__PURE__ */jsx(DiffFromTo, {
658
727
  diff,
659
728
  schemaType,
@@ -712,14 +781,14 @@ const shopifyAssetSchema = config => {
712
781
  filename: "filename",
713
782
  type: "type"
714
783
  },
715
- prepare(_ref14) {
784
+ prepare(_ref12) {
716
785
  let {
717
786
  url,
718
787
  meta,
719
788
  preview,
720
789
  filename,
721
790
  type
722
- } = _ref14;
791
+ } = _ref12;
723
792
  return {
724
793
  title: filename,
725
794
  subtitle: type,