@shopify/cli-hydrogen 8.2.0 → 8.4.0

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.
Files changed (75) hide show
  1. package/dist/assets/hydrogen/i18n/domains.ts +4 -11
  2. package/dist/assets/hydrogen/i18n/mock-i18n-types.ts +4 -2
  3. package/dist/assets/hydrogen/i18n/subdomains.ts +4 -11
  4. package/dist/assets/hydrogen/i18n/subfolders.ts +4 -11
  5. package/dist/assets/hydrogen/starter/.graphqlrc.ts +27 -0
  6. package/dist/assets/hydrogen/starter/CHANGELOG.md +174 -0
  7. package/dist/assets/hydrogen/starter/app/components/CartLineItem.tsx +5 -2
  8. package/dist/assets/hydrogen/starter/app/components/CartMain.tsx +2 -2
  9. package/dist/assets/hydrogen/starter/app/components/PageLayout.tsx +65 -19
  10. package/dist/assets/hydrogen/starter/app/components/PaginatedResourceSection.tsx +42 -0
  11. package/dist/assets/hydrogen/starter/app/components/SearchForm.tsx +68 -0
  12. package/dist/assets/hydrogen/starter/app/components/SearchFormPredictive.tsx +76 -0
  13. package/dist/assets/hydrogen/starter/app/components/SearchResults.tsx +164 -0
  14. package/dist/assets/hydrogen/starter/app/components/SearchResultsPredictive.tsx +322 -0
  15. package/dist/assets/hydrogen/starter/app/entry.client.tsx +10 -8
  16. package/dist/assets/hydrogen/starter/app/entry.server.tsx +1 -1
  17. package/dist/assets/hydrogen/starter/app/lib/context.ts +43 -0
  18. package/dist/assets/hydrogen/starter/app/lib/fragments.ts +53 -0
  19. package/dist/assets/hydrogen/starter/app/lib/search.ts +74 -24
  20. package/dist/assets/hydrogen/starter/app/root.tsx +4 -7
  21. package/dist/assets/hydrogen/starter/app/routes/account.addresses.tsx +2 -3
  22. package/dist/assets/hydrogen/starter/app/routes/account.orders._index.tsx +5 -19
  23. package/dist/assets/hydrogen/starter/app/routes/blogs.$blogHandle._index.tsx +11 -24
  24. package/dist/assets/hydrogen/starter/app/routes/blogs._index.tsx +14 -27
  25. package/dist/assets/hydrogen/starter/app/routes/collections.$handle.tsx +12 -30
  26. package/dist/assets/hydrogen/starter/app/routes/collections._index.tsx +13 -27
  27. package/dist/assets/hydrogen/starter/app/routes/collections.all.tsx +9 -31
  28. package/dist/assets/hydrogen/starter/app/routes/search.tsx +312 -73
  29. package/dist/assets/hydrogen/starter/app/styles/reset.css +12 -2
  30. package/dist/assets/hydrogen/starter/env.d.ts +11 -30
  31. package/dist/assets/hydrogen/starter/guides/predictiveSearch/predictiveSearch.jpg +0 -0
  32. package/dist/assets/hydrogen/starter/guides/predictiveSearch/predictiveSearch.md +391 -0
  33. package/dist/assets/hydrogen/starter/guides/search/search.jpg +0 -0
  34. package/dist/assets/hydrogen/starter/guides/search/search.md +333 -0
  35. package/dist/assets/hydrogen/starter/package.json +4 -4
  36. package/dist/assets/hydrogen/starter/server.ts +18 -74
  37. package/dist/assets/hydrogen/starter/storefrontapi.generated.d.ts +242 -172
  38. package/dist/assets/hydrogen/virtual-routes/components/{PageLayout.jsx → Layout.jsx} +2 -2
  39. package/dist/assets/hydrogen/virtual-routes/virtual-root.jsx +7 -6
  40. package/dist/commands/hydrogen/build.js +4 -2
  41. package/dist/commands/hydrogen/codegen.js +11 -3
  42. package/dist/commands/hydrogen/debug/cpu.js +2 -3
  43. package/dist/commands/hydrogen/deploy.js +8 -6
  44. package/dist/commands/hydrogen/dev.js +13 -11
  45. package/dist/commands/hydrogen/env/pull.js +5 -3
  46. package/dist/commands/hydrogen/env/push.js +2 -5
  47. package/dist/commands/hydrogen/login.js +4 -0
  48. package/dist/commands/hydrogen/logout.js +2 -0
  49. package/dist/commands/hydrogen/preview.js +11 -7
  50. package/dist/commands/hydrogen/setup.js +2 -4
  51. package/dist/hooks/init.js +6 -3
  52. package/dist/index.d.ts +5 -1
  53. package/dist/lib/auth.js +3 -63
  54. package/dist/lib/classic-compiler/dev.js +8 -4
  55. package/dist/lib/codegen.js +68 -33
  56. package/dist/lib/dev-shared.js +17 -15
  57. package/dist/lib/environment-variables.js +5 -4
  58. package/dist/lib/flags.js +7 -0
  59. package/dist/lib/import-utils.js +8 -2
  60. package/dist/lib/log.js +7 -38
  61. package/dist/lib/onboarding/common.js +2 -0
  62. package/dist/lib/onboarding/local.js +22 -7
  63. package/dist/lib/remix-config.js +8 -3
  64. package/dist/lib/setups/css/replacers.js +2 -2
  65. package/dist/lib/setups/i18n/index.js +3 -6
  66. package/dist/lib/setups/i18n/replacers.js +62 -134
  67. package/dist/lib/template-diff.js +15 -0
  68. package/dist/lib/transpile/morph/functions.js +3 -2
  69. package/dist/lib/transpile/morph/typedefs.js +4 -1
  70. package/dist/lib/vite-config.js +13 -1
  71. package/oclif.manifest.json +39 -2
  72. package/package.json +3 -3
  73. package/dist/assets/hydrogen/starter/.graphqlrc.yml +0 -12
  74. package/dist/assets/hydrogen/starter/app/components/Search.tsx +0 -514
  75. package/dist/assets/hydrogen/starter/app/routes/api.predictive-search.tsx +0 -318
@@ -1,514 +0,0 @@
1
- import {
2
- Link,
3
- Form,
4
- useParams,
5
- useFetcher,
6
- type FormProps,
7
- } from '@remix-run/react';
8
- import {Image, Money, Pagination} from '@shopify/hydrogen';
9
- import React, {useRef, useEffect} from 'react';
10
- import {applyTrackingParams} from '~/lib/search';
11
-
12
- import type {
13
- PredictiveProductFragment,
14
- PredictiveCollectionFragment,
15
- PredictiveArticleFragment,
16
- SearchQuery,
17
- } from 'storefrontapi.generated';
18
-
19
- import type {PredictiveSearchAPILoader} from '../routes/api.predictive-search';
20
-
21
- type PredicticeSearchResultItemImage =
22
- | PredictiveCollectionFragment['image']
23
- | PredictiveArticleFragment['image']
24
- | PredictiveProductFragment['variants']['nodes'][0]['image'];
25
-
26
- type PredictiveSearchResultItemPrice =
27
- | PredictiveProductFragment['variants']['nodes'][0]['price'];
28
-
29
- export type NormalizedPredictiveSearchResultItem = {
30
- __typename: string | undefined;
31
- handle: string;
32
- id: string;
33
- image?: PredicticeSearchResultItemImage;
34
- price?: PredictiveSearchResultItemPrice;
35
- styledTitle?: string;
36
- title: string;
37
- url: string;
38
- };
39
-
40
- export type NormalizedPredictiveSearchResults = Array<
41
- | {type: 'queries'; items: Array<NormalizedPredictiveSearchResultItem>}
42
- | {type: 'products'; items: Array<NormalizedPredictiveSearchResultItem>}
43
- | {type: 'collections'; items: Array<NormalizedPredictiveSearchResultItem>}
44
- | {type: 'pages'; items: Array<NormalizedPredictiveSearchResultItem>}
45
- | {type: 'articles'; items: Array<NormalizedPredictiveSearchResultItem>}
46
- >;
47
-
48
- export type NormalizedPredictiveSearch = {
49
- results: NormalizedPredictiveSearchResults;
50
- totalResults: number;
51
- };
52
-
53
- type FetchSearchResultsReturn = {
54
- searchResults: {
55
- results: SearchQuery | null;
56
- totalResults: number;
57
- };
58
- searchTerm: string;
59
- };
60
-
61
- export const NO_PREDICTIVE_SEARCH_RESULTS: NormalizedPredictiveSearchResults = [
62
- {type: 'queries', items: []},
63
- {type: 'products', items: []},
64
- {type: 'collections', items: []},
65
- {type: 'pages', items: []},
66
- {type: 'articles', items: []},
67
- ];
68
-
69
- export function SearchForm({searchTerm}: {searchTerm: string}) {
70
- const inputRef = useRef<HTMLInputElement | null>(null);
71
-
72
- // focus the input when cmd+k is pressed
73
- useEffect(() => {
74
- function handleKeyDown(event: KeyboardEvent) {
75
- if (event.key === 'k' && event.metaKey) {
76
- event.preventDefault();
77
- inputRef.current?.focus();
78
- }
79
-
80
- if (event.key === 'Escape') {
81
- inputRef.current?.blur();
82
- }
83
- }
84
-
85
- document.addEventListener('keydown', handleKeyDown);
86
-
87
- return () => {
88
- document.removeEventListener('keydown', handleKeyDown);
89
- };
90
- }, []);
91
-
92
- return (
93
- <Form method="get">
94
- <input
95
- defaultValue={searchTerm}
96
- name="q"
97
- placeholder="Search…"
98
- ref={inputRef}
99
- type="search"
100
- />
101
- &nbsp;
102
- <button type="submit">Search</button>
103
- </Form>
104
- );
105
- }
106
-
107
- export function SearchResults({
108
- results,
109
- searchTerm,
110
- }: Pick<FetchSearchResultsReturn['searchResults'], 'results'> & {
111
- searchTerm: string;
112
- }) {
113
- if (!results) {
114
- return null;
115
- }
116
- const keys = Object.keys(results) as Array<keyof typeof results>;
117
- return (
118
- <div>
119
- {results &&
120
- keys.map((type) => {
121
- const resourceResults = results[type];
122
-
123
- if (resourceResults.nodes[0]?.__typename === 'Page') {
124
- const pageResults = resourceResults as SearchQuery['pages'];
125
- return resourceResults.nodes.length ? (
126
- <SearchResultPageGrid key="pages" pages={pageResults} />
127
- ) : null;
128
- }
129
-
130
- if (resourceResults.nodes[0]?.__typename === 'Product') {
131
- const productResults = resourceResults as SearchQuery['products'];
132
- return resourceResults.nodes.length ? (
133
- <SearchResultsProductsGrid
134
- key="products"
135
- products={productResults}
136
- searchTerm={searchTerm}
137
- />
138
- ) : null;
139
- }
140
-
141
- if (resourceResults.nodes[0]?.__typename === 'Article') {
142
- const articleResults = resourceResults as SearchQuery['articles'];
143
- return resourceResults.nodes.length ? (
144
- <SearchResultArticleGrid
145
- key="articles"
146
- articles={articleResults}
147
- />
148
- ) : null;
149
- }
150
-
151
- return null;
152
- })}
153
- </div>
154
- );
155
- }
156
-
157
- function SearchResultsProductsGrid({
158
- products,
159
- searchTerm,
160
- }: Pick<SearchQuery, 'products'> & {searchTerm: string}) {
161
- return (
162
- <div className="search-result">
163
- <h2>Products</h2>
164
- <Pagination connection={products}>
165
- {({nodes, isLoading, NextLink, PreviousLink}) => {
166
- const ItemsMarkup = nodes.map((product) => {
167
- const trackingParams = applyTrackingParams(
168
- product,
169
- `q=${encodeURIComponent(searchTerm)}`,
170
- );
171
-
172
- return (
173
- <div className="search-results-item" key={product.id}>
174
- <Link
175
- prefetch="intent"
176
- to={`/products/${product.handle}${trackingParams}`}
177
- >
178
- {product.variants.nodes[0].image && (
179
- <Image
180
- data={product.variants.nodes[0].image}
181
- alt={product.title}
182
- width={50}
183
- />
184
- )}
185
- <div>
186
- <p>{product.title}</p>
187
- <small>
188
- <Money data={product.variants.nodes[0].price} />
189
- </small>
190
- </div>
191
- </Link>
192
- </div>
193
- );
194
- });
195
- return (
196
- <div>
197
- <div>
198
- <PreviousLink>
199
- {isLoading ? 'Loading...' : <span>↑ Load previous</span>}
200
- </PreviousLink>
201
- </div>
202
- <div>
203
- {ItemsMarkup}
204
- <br />
205
- </div>
206
- <div>
207
- <NextLink>
208
- {isLoading ? 'Loading...' : <span>Load more ↓</span>}
209
- </NextLink>
210
- </div>
211
- </div>
212
- );
213
- }}
214
- </Pagination>
215
- <br />
216
- </div>
217
- );
218
- }
219
-
220
- function SearchResultPageGrid({pages}: Pick<SearchQuery, 'pages'>) {
221
- return (
222
- <div className="search-result">
223
- <h2>Pages</h2>
224
- <div>
225
- {pages?.nodes?.map((page) => (
226
- <div className="search-results-item" key={page.id}>
227
- <Link prefetch="intent" to={`/pages/${page.handle}`}>
228
- {page.title}
229
- </Link>
230
- </div>
231
- ))}
232
- </div>
233
- <br />
234
- </div>
235
- );
236
- }
237
-
238
- function SearchResultArticleGrid({articles}: Pick<SearchQuery, 'articles'>) {
239
- return (
240
- <div className="search-result">
241
- <h2>Articles</h2>
242
- <div>
243
- {articles?.nodes?.map((article) => (
244
- <div className="search-results-item" key={article.id}>
245
- <Link prefetch="intent" to={`/blogs/${article.handle}`}>
246
- {article.title}
247
- </Link>
248
- </div>
249
- ))}
250
- </div>
251
- <br />
252
- </div>
253
- );
254
- }
255
-
256
- export function NoSearchResults() {
257
- return <p>No results, try a different search.</p>;
258
- }
259
-
260
- type ChildrenRenderProps = {
261
- fetchResults: (event: React.ChangeEvent<HTMLInputElement>) => void;
262
- fetcher: ReturnType<typeof useFetcher<PredictiveSearchAPILoader>>;
263
- inputRef: React.MutableRefObject<HTMLInputElement | null>;
264
- };
265
-
266
- type SearchFromProps = {
267
- action?: FormProps['action'];
268
- className?: string;
269
- children: (passedProps: ChildrenRenderProps) => React.ReactNode;
270
- [key: string]: unknown;
271
- };
272
-
273
- /**
274
- * Search form component that sends search requests to the `/search` route
275
- **/
276
- export function PredictiveSearchForm({
277
- action,
278
- children,
279
- className = 'predictive-search-form',
280
- ...props
281
- }: SearchFromProps) {
282
- const params = useParams();
283
- const fetcher = useFetcher<PredictiveSearchAPILoader>({
284
- key: 'search',
285
- });
286
- const inputRef = useRef<HTMLInputElement | null>(null);
287
-
288
- function fetchResults(event: React.ChangeEvent<HTMLInputElement>) {
289
- const searchAction = action ?? '/api/predictive-search';
290
- const newSearchTerm = event.target.value || '';
291
- const localizedAction = params.locale
292
- ? `/${params.locale}${searchAction}`
293
- : searchAction;
294
-
295
- fetcher.submit(
296
- {q: newSearchTerm, limit: '6'},
297
- {method: 'GET', action: localizedAction},
298
- );
299
- }
300
-
301
- // ensure the passed input has a type of search, because SearchResults
302
- // will select the element based on the input
303
- useEffect(() => {
304
- inputRef?.current?.setAttribute('type', 'search');
305
- }, []);
306
-
307
- return (
308
- <fetcher.Form
309
- {...props}
310
- className={className}
311
- onSubmit={(event) => {
312
- event.preventDefault();
313
- event.stopPropagation();
314
- if (!inputRef?.current || inputRef.current.value === '') {
315
- return;
316
- }
317
- inputRef.current.blur();
318
- }}
319
- >
320
- {children({fetchResults, inputRef, fetcher})}
321
- </fetcher.Form>
322
- );
323
- }
324
-
325
- export function PredictiveSearchResults() {
326
- const {results, totalResults, searchInputRef, searchTerm, state} =
327
- usePredictiveSearch();
328
-
329
- function goToSearchResult(event: React.MouseEvent<HTMLAnchorElement>) {
330
- if (!searchInputRef.current) return;
331
- searchInputRef.current.blur();
332
- searchInputRef.current.value = '';
333
- // close the aside
334
- window.location.href = event.currentTarget.href;
335
- }
336
-
337
- if (state === 'loading') {
338
- return <div>Loading...</div>;
339
- }
340
-
341
- if (!totalResults) {
342
- return <NoPredictiveSearchResults searchTerm={searchTerm} />;
343
- }
344
-
345
- return (
346
- <div className="predictive-search-results">
347
- <div>
348
- {results.map(({type, items}) => (
349
- <PredictiveSearchResult
350
- goToSearchResult={goToSearchResult}
351
- items={items}
352
- key={type}
353
- searchTerm={searchTerm}
354
- type={type}
355
- />
356
- ))}
357
- </div>
358
- {searchTerm.current && (
359
- <Link onClick={goToSearchResult} to={`/search?q=${searchTerm.current}`}>
360
- <p>
361
- View all results for <q>{searchTerm.current}</q>
362
- &nbsp; →
363
- </p>
364
- </Link>
365
- )}
366
- </div>
367
- );
368
- }
369
-
370
- function NoPredictiveSearchResults({
371
- searchTerm,
372
- }: {
373
- searchTerm: React.MutableRefObject<string>;
374
- }) {
375
- if (!searchTerm.current) {
376
- return null;
377
- }
378
- return (
379
- <p>
380
- No results found for <q>{searchTerm.current}</q>
381
- </p>
382
- );
383
- }
384
-
385
- type SearchResultTypeProps = {
386
- goToSearchResult: (event: React.MouseEvent<HTMLAnchorElement>) => void;
387
- items: NormalizedPredictiveSearchResultItem[];
388
- searchTerm: UseSearchReturn['searchTerm'];
389
- type: NormalizedPredictiveSearchResults[number]['type'];
390
- };
391
-
392
- function PredictiveSearchResult({
393
- goToSearchResult,
394
- items,
395
- searchTerm,
396
- type,
397
- }: SearchResultTypeProps) {
398
- const isSuggestions = type === 'queries';
399
- const categoryUrl = `/search?q=${
400
- searchTerm.current
401
- }&type=${pluralToSingularSearchType(type)}`;
402
-
403
- return (
404
- <div className="predictive-search-result" key={type}>
405
- <Link prefetch="intent" to={categoryUrl} onClick={goToSearchResult}>
406
- <h5>{isSuggestions ? 'Suggestions' : type}</h5>
407
- </Link>
408
- <ul>
409
- {items.map((item: NormalizedPredictiveSearchResultItem) => (
410
- <SearchResultItem
411
- goToSearchResult={goToSearchResult}
412
- item={item}
413
- key={item.id}
414
- />
415
- ))}
416
- </ul>
417
- </div>
418
- );
419
- }
420
-
421
- type SearchResultItemProps = Pick<SearchResultTypeProps, 'goToSearchResult'> & {
422
- item: NormalizedPredictiveSearchResultItem;
423
- };
424
-
425
- function SearchResultItem({goToSearchResult, item}: SearchResultItemProps) {
426
- return (
427
- <li className="predictive-search-result-item" key={item.id}>
428
- <Link onClick={goToSearchResult} to={item.url}>
429
- {item.image?.url && (
430
- <Image
431
- alt={item.image.altText ?? ''}
432
- src={item.image.url}
433
- width={50}
434
- height={50}
435
- />
436
- )}
437
- <div>
438
- {item.styledTitle ? (
439
- <div
440
- dangerouslySetInnerHTML={{
441
- __html: item.styledTitle,
442
- }}
443
- />
444
- ) : (
445
- <span>{item.title}</span>
446
- )}
447
- {item?.price && (
448
- <small>
449
- <Money data={item.price} />
450
- </small>
451
- )}
452
- </div>
453
- </Link>
454
- </li>
455
- );
456
- }
457
-
458
- type UseSearchReturn = NormalizedPredictiveSearch & {
459
- searchInputRef: React.MutableRefObject<HTMLInputElement | null>;
460
- searchTerm: React.MutableRefObject<string>;
461
- state: ReturnType<typeof useFetcher<PredictiveSearchAPILoader>>['state'];
462
- };
463
-
464
- function usePredictiveSearch(): UseSearchReturn {
465
- const searchFetcher = useFetcher<FetchSearchResultsReturn>({key: 'search'});
466
- const searchTerm = useRef<string>('');
467
- const searchInputRef = useRef<HTMLInputElement | null>(null);
468
-
469
- if (searchFetcher?.state === 'loading') {
470
- searchTerm.current = (searchFetcher.formData?.get('q') || '') as string;
471
- }
472
-
473
- const search = (searchFetcher?.data?.searchResults || {
474
- results: NO_PREDICTIVE_SEARCH_RESULTS,
475
- totalResults: 0,
476
- }) as NormalizedPredictiveSearch;
477
-
478
- // capture the search input element as a ref
479
- useEffect(() => {
480
- if (searchInputRef.current) return;
481
- searchInputRef.current = document.querySelector('input[type="search"]');
482
- }, []);
483
-
484
- return {...search, searchInputRef, searchTerm, state: searchFetcher.state};
485
- }
486
-
487
- /**
488
- * Converts a plural search type to a singular search type
489
- *
490
- * @example
491
- * ```js
492
- * pluralToSingularSearchType('articles'); // => 'ARTICLE'
493
- * pluralToSingularSearchType(['articles', 'products']); // => 'ARTICLE,PRODUCT'
494
- * ```
495
- */
496
- function pluralToSingularSearchType(
497
- type:
498
- | NormalizedPredictiveSearchResults[number]['type']
499
- | Array<NormalizedPredictiveSearchResults[number]['type']>,
500
- ) {
501
- const plural = {
502
- articles: 'ARTICLE',
503
- collections: 'COLLECTION',
504
- pages: 'PAGE',
505
- products: 'PRODUCT',
506
- queries: 'QUERY',
507
- };
508
-
509
- if (typeof type === 'string') {
510
- return plural[type];
511
- }
512
-
513
- return type.map((t) => plural[t]).join(',');
514
- }