@wix/himalaya-cli 0.1852.0 → 0.1853.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 (149) hide show
  1. package/dist/cli.mjs +95 -32
  2. package/guides/himi-json-output.md +8 -2
  3. package/guides/one-app-section-mappings.md +1 -1
  4. package/guides/one-app-to-himi-app.md +19 -6
  5. package/package.json +2 -2
  6. package/template-icons/bare-fashion.png +0 -0
  7. package/templates/bare-fashion/README.md +52 -0
  8. package/templates/bare-fashion/config.ts +474 -0
  9. package/templates/bare-fashion/dev/index.ts +16526 -0
  10. package/templates/bare-fashion/himalaya.content.json +70 -0
  11. package/templates/bare-fashion/himalaya.template.json +18 -0
  12. package/templates/bare-fashion/icon.png +0 -0
  13. package/templates/bare-fashion/platforms.json +3 -0
  14. package/templates/bare-fashion/tier5-src/_shared/app-locale.ts +70 -0
  15. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-address-book.ts +116 -0
  16. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-address-countries.ts +261 -0
  17. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-bag-count.ts +24 -0
  18. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-categories.ts +48 -0
  19. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-facets.ts +265 -0
  20. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-load-error.ts +38 -0
  21. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-product-card.ts +136 -0
  22. package/templates/bare-fashion/tier5-src/_shared/bare-fashion-store-options.ts +129 -0
  23. package/templates/bare-fashion/tier5-src/_shared/branded-lite-hosted.ts +282 -0
  24. package/templates/bare-fashion/tier5-src/_shared/collection-contract.ts +228 -0
  25. package/templates/bare-fashion/tier5-src/_shared/collection-query.ts +201 -0
  26. package/templates/bare-fashion/tier5-src/_shared/collection-service.ts +429 -0
  27. package/templates/bare-fashion/tier5-src/_shared/engine-intl.ts +529 -0
  28. package/templates/bare-fashion/tier5-src/_shared/himi-account.ts +209 -0
  29. package/templates/bare-fashion/tier5-src/_shared/himi-copy.ts +123 -0
  30. package/templates/bare-fashion/tier5-src/_shared/himi-layouts.ts +142 -0
  31. package/templates/bare-fashion/tier5-src/_shared/himi-store-categories.ts +108 -0
  32. package/templates/bare-fashion/tier5-src/_shared/himi-store-saved.ts +57 -0
  33. package/templates/bare-fashion/tier5-src/_shared/himi-store.ts +7 -0
  34. package/templates/bare-fashion/tier5-src/_shared/himi-text.ts +24 -0
  35. package/templates/bare-fashion/tier5-src/_shared/hosted-checkout.ts +70 -0
  36. package/templates/bare-fashion/tier5-src/_shared/live-session.ts +172 -0
  37. package/templates/bare-fashion/tier5-src/_shared/local-preferences.ts +38 -0
  38. package/templates/bare-fashion/tier5-src/_shared/member-access.ts +63 -0
  39. package/templates/bare-fashion/tier5-src/_shared/member-avatar.ts +76 -0
  40. package/templates/bare-fashion/tier5-src/_shared/member-orders.ts +8 -0
  41. package/templates/bare-fashion/tier5-src/_shared/online-actions.ts +76 -0
  42. package/templates/bare-fashion/tier5-src/_shared/owner-load-error.ts +348 -0
  43. package/templates/bare-fashion/tier5-src/himi-bare-fashion-account/index.ts +127 -0
  44. package/templates/bare-fashion/tier5-src/himi-bare-fashion-address-form/index.ts +367 -0
  45. package/templates/bare-fashion/tier5-src/himi-bare-fashion-addresses/index.ts +202 -0
  46. package/templates/bare-fashion/tier5-src/himi-bare-fashion-appearance/index.ts +73 -0
  47. package/templates/bare-fashion/tier5-src/himi-bare-fashion-cart/index.ts +540 -0
  48. package/templates/bare-fashion/tier5-src/himi-bare-fashion-collection/index.ts +111 -0
  49. package/templates/bare-fashion/tier5-src/himi-bare-fashion-home/index.ts +241 -0
  50. package/templates/bare-fashion/tier5-src/himi-bare-fashion-nav/index.ts +56 -0
  51. package/templates/bare-fashion/tier5-src/himi-bare-fashion-order/index.ts +437 -0
  52. package/templates/bare-fashion/tier5-src/himi-bare-fashion-product/index.ts +650 -0
  53. package/templates/bare-fashion/tier5-src/himi-bare-fashion-products/index.ts +142 -0
  54. package/templates/bare-fashion/tier5-src/himi-bare-fashion-saved/index.ts +45 -0
  55. package/templates/bare-fashion/tier5-src/himi-bare-fashion-search/index.ts +199 -0
  56. package/templates/bare-fashion/tier5-src/himi-bare-fashion-shop/index.ts +79 -0
  57. package/templates/bare-fashion/tier5-src/himi-bare-fashion-sign-in/index.ts +269 -0
  58. package/templates/bare-fashion/tier5-src/himi-bare-fashion-wallet/index.ts +224 -0
  59. package/templates/bare-fashion/tier5-src/himi-branded-lite-info/index.ts +209 -0
  60. package/templates/bare-fashion/tier5-src/himi-branded-lite-orders/index.ts +327 -0
  61. package/templates/bare-fashion/tier5-src/wix-spaces-collections/index.ts +48 -0
  62. package/templates/bare-fashion/tier5-src/wix-spaces-delete-account/index.ts +59 -0
  63. package/templates/bare-fashion/tier5-src/wix-spaces-view-profile/index.ts +119 -0
  64. package/templates/bare-fashion/tokens.json +348 -0
  65. package/templates/beauty-salon/tier5-src/_shared/collection-query.ts +8 -0
  66. package/templates/beauty-salon/tier5-src/_shared/collection-service.ts +7 -1
  67. package/templates/beauty-salon-owner/tier5-src/_shared/collection-query.ts +8 -0
  68. package/templates/beauty-salon-owner/tier5-src/_shared/collection-service.ts +7 -1
  69. package/templates/branded-lite/recipes/upcoming-bookings.json +118 -6
  70. package/templates/branded-lite/tier5-src/_shared/collection-query-legacy.ts +7 -0
  71. package/templates/branded-lite/tier5-src/_shared/collection-query.ts +7 -0
  72. package/templates/branded-lite/tier5-src/_shared/collection-service.ts +7 -1
  73. package/templates/branded-lite/tier5-src/_shared/events-inline-options.ts +55 -3
  74. package/templates/branded-lite/tier5-src/_shared/events-inline.ts +5 -3
  75. package/templates/branded-lite/tier5-src/_shared/upcoming-bookings-section.ts +15 -4
  76. package/templates/branded-lite/tier5-src/himi-events-list/index.ts +17 -0
  77. package/templates/brandy/tier5-src/_shared/collection-query-legacy.ts +7 -0
  78. package/templates/brandy/tier5-src/_shared/collection-query.ts +7 -0
  79. package/templates/brandy/tier5-src/_shared/collection-service.ts +7 -1
  80. package/templates/community-club/tier5-src/_shared/collection-query.ts +8 -0
  81. package/templates/community-club/tier5-src/_shared/collection-service.ts +7 -1
  82. package/templates/community-club/tier5-src/_shared/events-inline-options.ts +77 -0
  83. package/templates/community-club/tier5-src/himi-events-list/index.ts +17 -0
  84. package/templates/community-club-owner/tier5-src/_shared/collection-query.ts +8 -0
  85. package/templates/community-club-owner/tier5-src/_shared/collection-service.ts +7 -1
  86. package/templates/conference/tier5-src/_shared/collection-query.ts +8 -0
  87. package/templates/conference/tier5-src/_shared/collection-service.ts +7 -1
  88. package/templates/conference/tier5-src/_shared/events-inline-options.ts +77 -0
  89. package/templates/conference/tier5-src/himi-events-list/index.ts +17 -0
  90. package/templates/events-meetups/tier5-src/_shared/collection-query.ts +8 -0
  91. package/templates/events-meetups/tier5-src/_shared/collection-service.ts +7 -1
  92. package/templates/events-meetups/tier5-src/_shared/events-inline-options.ts +77 -0
  93. package/templates/events-meetups/tier5-src/himi-events-list/index.ts +17 -0
  94. package/templates/events-meetups-owner/tier5-src/_shared/collection-query.ts +8 -0
  95. package/templates/events-meetups-owner/tier5-src/_shared/collection-service.ts +7 -1
  96. package/templates/fitness-gym/tier5-src/_shared/collection-query.ts +8 -0
  97. package/templates/fitness-gym/tier5-src/_shared/collection-service.ts +7 -1
  98. package/templates/gym-fitness/tier5-src/_shared/collection-query.ts +8 -0
  99. package/templates/gym-fitness/tier5-src/_shared/collection-service.ts +7 -1
  100. package/templates/gym-fitness/tier5-src/_shared/events-inline-options.ts +77 -0
  101. package/templates/gym-fitness/tier5-src/himi-events-list/index.ts +17 -0
  102. package/templates/gym-fitness-owner/tier5-src/_shared/collection-query.ts +8 -0
  103. package/templates/gym-fitness-owner/tier5-src/_shared/collection-service.ts +7 -1
  104. package/templates/medical-services/tier5-src/_shared/collection-query.ts +8 -0
  105. package/templates/medical-services/tier5-src/_shared/collection-service.ts +7 -1
  106. package/templates/nonprofit-cause/tier5-src/_shared/collection-query.ts +8 -0
  107. package/templates/nonprofit-cause/tier5-src/_shared/collection-service.ts +7 -1
  108. package/templates/nonprofit-cause/tier5-src/_shared/events-inline-options.ts +77 -0
  109. package/templates/nonprofit-cause/tier5-src/himi-events-list/index.ts +17 -0
  110. package/templates/nonprofit-cause-owner/tier5-src/_shared/collection-query.ts +8 -0
  111. package/templates/nonprofit-cause-owner/tier5-src/_shared/collection-service.ts +7 -1
  112. package/templates/owner-lite/tier5-src/_shared/collection-query.ts +8 -0
  113. package/templates/owner-lite/tier5-src/_shared/collection-service.ts +7 -1
  114. package/templates/personal-fitness-trainer/tier5-src/_shared/collection-query.ts +8 -0
  115. package/templates/personal-fitness-trainer/tier5-src/_shared/collection-service.ts +7 -1
  116. package/templates/podcast-creator/tier5-src/_shared/collection-query.ts +8 -0
  117. package/templates/podcast-creator/tier5-src/_shared/collection-service.ts +7 -1
  118. package/templates/podcast-creator/tier5-src/_shared/events-inline-options.ts +77 -0
  119. package/templates/podcast-creator/tier5-src/himi-events-list/index.ts +17 -0
  120. package/templates/podcast-creator-owner/tier5-src/_shared/collection-query.ts +8 -0
  121. package/templates/podcast-creator-owner/tier5-src/_shared/collection-service.ts +7 -1
  122. package/templates/portfolio-creative/tier5-src/_shared/collection-query.ts +8 -0
  123. package/templates/portfolio-creative/tier5-src/_shared/collection-service.ts +7 -1
  124. package/templates/portfolio-creative/tier5-src/_shared/events-inline-options.ts +77 -0
  125. package/templates/portfolio-creative/tier5-src/himi-events-list/index.ts +17 -0
  126. package/templates/portfolio-creative-owner/tier5-src/_shared/collection-query.ts +8 -0
  127. package/templates/portfolio-creative-owner/tier5-src/_shared/collection-service.ts +7 -1
  128. package/templates/restaurant/tier5-src/_shared/collection-query.ts +8 -0
  129. package/templates/restaurant/tier5-src/_shared/collection-service.ts +7 -1
  130. package/templates/restaurant/tier5-src/_shared/events-inline-options.ts +77 -0
  131. package/templates/restaurant/tier5-src/himi-events-list/index.ts +17 -0
  132. package/templates/restaurant-owner/tier5-src/_shared/collection-query.ts +8 -0
  133. package/templates/restaurant-owner/tier5-src/_shared/collection-service.ts +7 -1
  134. package/templates/retail-store/tier5-src/_shared/collection-query-legacy.ts +7 -0
  135. package/templates/retail-store/tier5-src/_shared/collection-query.ts +7 -0
  136. package/templates/retail-store/tier5-src/_shared/collection-service.ts +7 -1
  137. package/templates/retail-store-owner/tier5-src/_shared/collection-query.ts +8 -0
  138. package/templates/retail-store-owner/tier5-src/_shared/collection-service.ts +7 -1
  139. package/templates/salon-spa/tier5-src/_shared/collection-query.ts +8 -0
  140. package/templates/salon-spa/tier5-src/_shared/collection-service.ts +7 -1
  141. package/templates/wholesale-store/tier5-src/_shared/collection-query-legacy.ts +7 -0
  142. package/templates/wholesale-store/tier5-src/_shared/collection-query.ts +7 -0
  143. package/templates/wholesale-store/tier5-src/_shared/collection-service.ts +7 -1
  144. package/templates/yoga-studio/tier5-src/_shared/collection-query.ts +8 -0
  145. package/templates/yoga-studio/tier5-src/_shared/collection-service.ts +7 -1
  146. package/templates/yoga-studio/tier5-src/_shared/events-inline-options.ts +77 -0
  147. package/templates/yoga-studio/tier5-src/himi-events-list/index.ts +17 -0
  148. package/templates/yoga-studio-owner/tier5-src/_shared/collection-query.ts +8 -0
  149. package/templates/yoga-studio-owner/tier5-src/_shared/collection-service.ts +7 -1
@@ -0,0 +1,142 @@
1
+ // Tier-5 bundle: BARE — full products gallery (inner Shop screen).
2
+ // Category chips on top, a sort picker and a Filter button that opens the filter
3
+ // panel (price + the store's product options, e.g. COLOR / SIZE).
4
+ //
5
+ // The filter panel and sort picker are BottomSheets on THIS screen, not navigation
6
+ // modals: a modal is a separate screen with its own worker, and state does not
7
+ // cross screens (tier-5 guide).
8
+ //
9
+ // Opened with `category` = a category id or name ("NEW IN", "ON SALE"); absent ⇒ ALL.
10
+ import { defineService } from "@himalaya/state";
11
+ import { searchStoreProducts, type StoreProduct } from "@himalaya/wix-stores";
12
+ import { queryCollections } from "@himalaya/wix-stores-collections";
13
+ import { orderCategories } from "../_shared/bare-fashion-categories.js";
14
+ import { cardId, toggleFavorite, type ProductCard } from "../_shared/bare-fashion-product-card.js";
15
+ import { FACETS_BOOT, applyFacets, countText, currencyOf, facetActions, idOf, option, toRow, type Chip, type FacetState, type Option, type Row } from "../_shared/bare-fashion-facets.js";
16
+ import { errorFlags, errorKindOf, type ErrorKind } from "../_shared/bare-fashion-load-error.js";
17
+
18
+ const ALL = "all";
19
+ const MAX_PRODUCTS = 500;
20
+
21
+ interface S extends FacetState {
22
+ status: "loading" | "ready" | "error";
23
+ isLoading: boolean;
24
+ notLoading: boolean;
25
+ isErrored: boolean;
26
+ notErrored: boolean;
27
+ /** Why the last load failed: the error state shows the offline or the server variant. */
28
+ errorKind?: ErrorKind;
29
+ hideOfflineError?: boolean;
30
+ hideServerError?: boolean;
31
+
32
+ categoryChips: Chip[];
33
+ categorySelected: string[];
34
+ /** The category row: one chip per category, with its selected state. */
35
+ categoryOptions: Option[];
36
+
37
+ results: ProductCard[];
38
+ countLabel: string;
39
+ showLabel: string;
40
+ noResults: boolean;
41
+ noEmpty: boolean;
42
+ emptyText: string;
43
+ }
44
+
45
+ // The full catalog stays out of bound state; only derived rows are rendered.
46
+ let rows: Row[] = [];
47
+ let categoryPicked = false;
48
+ let currency: string | undefined;
49
+
50
+ function input(): { category?: string } {
51
+ return (globalThis as { himalaya?: { input?: { category?: string } } }).himalaya?.input ?? {};
52
+ }
53
+
54
+ async function fetchAll(): Promise<StoreProduct[]> {
55
+ const out: StoreProduct[] = [];
56
+ let cursor: string | undefined;
57
+ do {
58
+ const res = await searchStoreProducts({ limit: 100, ...(cursor ? { cursor } : {}) });
59
+ out.push(...(res.products ?? []));
60
+ cursor = res.pagingMetadata?.cursors?.next ?? undefined;
61
+ } while (cursor && out.length < MAX_PRODUCTS);
62
+ return out;
63
+ }
64
+
65
+ function derive(s: S): S {
66
+ const category = s.categorySelected[0] ?? ALL;
67
+ const inCategory = category === ALL ? rows : rows.filter((r) => r.categoryIds.includes(category));
68
+ const { next, active } = applyFacets(s, inCategory, currency);
69
+ const n = next.results.length;
70
+ const ready = s.status === "ready";
71
+ return {
72
+ ...next,
73
+ isLoading: s.status === "loading",
74
+ notLoading: s.status !== "loading",
75
+ isErrored: s.status === "error",
76
+ notErrored: s.status !== "error",
77
+ ...errorFlags(s.status, s.errorKind),
78
+ categoryOptions: s.categoryChips.map((c) => option(c, c.id === category)),
79
+ countLabel: ready ? countText(n) : "",
80
+ showLabel: `Show ${countText(n)}`,
81
+ noResults: n === 0,
82
+ noEmpty: !ready || n > 0,
83
+ emptyText: active ? "No products match these filters." : "No products in this category yet.",
84
+ };
85
+ }
86
+ async function load(s: S): Promise<S> {
87
+ try {
88
+ const [products, collections] = await Promise.all([
89
+ fetchAll(),
90
+ queryCollections({ query: { paging: { limit: 50 } } }).then((r) => r.collections ?? [], () => []),
91
+ ]);
92
+ currency = products.map(currencyOf).find(Boolean);
93
+ rows = products.map(toRow).filter((r) => r.card.id);
94
+ const cats = orderCategories(collections).filter((c) => c.id);
95
+ const categoryChips: Chip[] = [{ id: ALL, label: "ALL" }, ...cats.map((c) => ({ id: c.id ?? "", label: (c.name ?? "").toUpperCase() }))];
96
+ // Pre-select the category the shopper came from: an id, or a name ("NEW IN").
97
+ const want = (input().category ?? "").trim();
98
+ const match = want.toLowerCase() === ALL
99
+ ? categoryChips[0]
100
+ : cats.find((c) => c.id === want || (c.name ?? "").trim().toUpperCase() === want.toUpperCase());
101
+ // A retry keeps the shopper's own pick; the first load uses the link's category.
102
+ const current = categoryPicked ? s.categorySelected[0] : undefined;
103
+ const keep = current && categoryChips.some((c) => c.id === current) ? current : undefined;
104
+ return derive({ ...s, status: "ready", categoryChips, categorySelected: [match?.id ?? keep ?? ALL] });
105
+ } catch (e) {
106
+ return derive({ ...s, status: "error", errorKind: errorKindOf(e) });
107
+ }
108
+ }
109
+
110
+ const BOOT: S = derive({
111
+ ...FACETS_BOOT,
112
+ status: "loading", isLoading: true, notLoading: false, isErrored: false, notErrored: true,
113
+ categoryChips: [], categorySelected: [ALL], categoryOptions: [],
114
+ results: [], countLabel: "", showLabel: "", noResults: true, noEmpty: true, emptyText: "",
115
+ });
116
+
117
+ type A = (s: S, arg?: unknown) => S | Promise<S>;
118
+ const svc = defineService<S, {
119
+ load: A; retry: A; selectCategory: A; sort: A; openFilters: A; openSort: A; closePanel: A;
120
+ selectPrice: A; selectOpt1: A; selectOpt2: A; selectOpt3: A; clearFilters: A; toggleSave: A;
121
+ }>("bareFashionProducts", {
122
+ state: BOOT,
123
+ actions: {
124
+ load: (s) => load(s),
125
+ retry: (s) => load(derive({ ...s, status: "loading" })),
126
+ // Single-select: one category is always chosen.
127
+ selectCategory: (s, arg) => {
128
+ const id = idOf(arg);
129
+ if (!s.categoryChips.some((c) => c.id === id)) return s;
130
+ categoryPicked = true;
131
+ return derive({ ...s, categorySelected: [id] });
132
+ },
133
+ ...facetActions(derive),
134
+ toggleSave: (s, arg) => {
135
+ try { toggleFavorite(s.results, cardId(arg)); } catch { return s; }
136
+ return derive(s);
137
+ },
138
+ },
139
+ });
140
+
141
+ svc.load();
142
+ export { svc };
@@ -0,0 +1,45 @@
1
+ import { defineService } from "@himalaya/state";
2
+ import { readSavedProducts, removeSavedProduct } from "../_shared/himi-store-saved.js";
3
+ import { cardId, savedCard, type ProductCard } from "../_shared/bare-fashion-product-card.js";
4
+ import { stashCopy, copyParam } from "../_shared/himi-copy.js";
5
+
6
+ type Row = ProductCard;
7
+ interface S {
8
+ status: "loading" | "ready" | "error";
9
+ rows: Row[];
10
+ error: string;
11
+ emptyText: string;
12
+ isLoading: boolean;
13
+ notLoading: boolean;
14
+ notErrored: boolean;
15
+ noEmpty: boolean;
16
+ }
17
+ function shape(s: Pick<S, "status" | "rows" | "error">): S {
18
+ return { ...s, isLoading: s.status === "loading", notLoading: s.status !== "loading", notErrored: s.status !== "error", noEmpty: s.status !== "ready" || s.rows.length > 0, emptyText: copyParam("savedProducts.empty", "Tap the heart on an item to add it to your wishlist.") };
19
+ }
20
+ function loadRows(): Row[] {
21
+ return readSavedProducts().map(savedCard);
22
+ }
23
+ function failed(s: S): S {
24
+ return shape({ ...s, status: "error", error: copyParam("savedProducts.error", "Could not update your wishlist. Try again.") });
25
+ }
26
+ const svc = defineService<S, {
27
+ load: (s: S, arg?: unknown) => S;
28
+ remove: (s: S, arg?: unknown) => S;
29
+ }>("bareFashionSaved", {
30
+ state: shape({ status: "loading", rows: [], error: "" }),
31
+ actions: {
32
+ load: (s, arg) => {
33
+ stashCopy(arg);
34
+ try { return shape({ status: "ready", rows: loadRows(), error: "" }); }
35
+ catch { return failed(s); }
36
+ },
37
+ remove: (s, arg) => {
38
+ try {
39
+ removeSavedProduct(cardId(arg));
40
+ return shape({ status: "ready", rows: loadRows(), error: "" });
41
+ } catch { return failed(s); }
42
+ },
43
+ },
44
+ });
45
+ export { svc };
@@ -0,0 +1,199 @@
1
+ // Tier-5 bundle: BARE — full-screen search (opened from the Shop tab's search bar).
2
+ // Opens with the query field focused. Empty query → the shopper's recent searches.
3
+ // Non-empty query → matching products under the same sort picker and filter panel
4
+ // as the Products screen (shared in ../_shared/bare-facets.ts).
5
+ //
6
+ // Recent searches live on this device only (the host's app-scoped KV). A query is
7
+ // saved when the shopper presses Search on the keyboard, taps a recent search, or
8
+ // opens a product from the results — never per keystroke.
9
+ import { defineService, navigate } from "@himalaya/state";
10
+ import { dismiss } from "@himalaya/keyboard";
11
+ import { searchStoreProducts } from "@himalaya/wix-stores";
12
+ import { cardId, toggleFavorite, type ProductCard } from "../_shared/bare-fashion-product-card.js";
13
+ import { FACETS_BOOT, applyFacets, countText, currencyOf, facetActions, idOf, toRow, type FacetState, type Row } from "../_shared/bare-fashion-facets.js";
14
+ import { errorFlags, errorKindOf, type ErrorKind } from "../_shared/bare-fashion-load-error.js";
15
+
16
+ const RECENT_KEY = "bare:recent-searches:v1";
17
+ const RECENT_MAX = 10;
18
+ const RESULT_LIMIT = 100;
19
+
20
+ interface Recent { id: string; label: string }
21
+
22
+ interface S extends FacetState {
23
+ status: "idle" | "loading" | "ready" | "error";
24
+ query: string;
25
+ /** Non-empty seed ⇒ the field takes focus (and the keyboard opens) on appear. */
26
+ focusToken: string;
27
+ recent: Recent[];
28
+ hideRecent: boolean;
29
+ hideQueryArea: boolean;
30
+ isLoading: boolean;
31
+ notLoading: boolean;
32
+ notErrored: boolean;
33
+ /** Why the last load failed: the error state shows the offline or the server variant. */
34
+ errorKind?: ErrorKind;
35
+ hideOfflineError?: boolean;
36
+ hideServerError?: boolean;
37
+ results: ProductCard[];
38
+ countLabel: string;
39
+ showLabel: string;
40
+ noResults: boolean;
41
+ noEmpty: boolean;
42
+ emptyText: string;
43
+ }
44
+
45
+ // The fetched result rows stay out of bound state; only derived cards are rendered.
46
+ let rows: Row[] = [];
47
+ let currency: string | undefined;
48
+ // Search responses can land out of order; only the latest query may write state.
49
+ let latestQuery = "";
50
+
51
+ // ---------------------------------------------------------------------------
52
+ // Recent searches (device KV — a failed read or write never breaks search)
53
+ // ---------------------------------------------------------------------------
54
+
55
+ type StorageLike = { getItem(key: string): string | null; setItem(key: string, value: string): void };
56
+ function storage(): StorageLike | undefined {
57
+ const host = globalThis as { himalaya?: { storage?: StorageLike }; localStorage?: StorageLike };
58
+ return host.himalaya?.storage ?? host.localStorage;
59
+ }
60
+
61
+ /** The recent searches kept on the device, or `ok: false` when the stored list cannot be read
62
+ * (no storage, or a value this version did not write). Recent searches are then not offered. */
63
+ function storedRecent(): { ok: true; list: Recent[] } | { ok: false } {
64
+ try {
65
+ const parsed: unknown = JSON.parse(storage()?.getItem(RECENT_KEY) ?? "[]");
66
+ if (!Array.isArray(parsed)) return { ok: false };
67
+ return { ok: true, list: parsed.filter((q): q is string => typeof q === "string" && !!q.trim())
68
+ .slice(0, RECENT_MAX).map((q) => ({ id: q, label: q })) };
69
+ } catch {
70
+ return { ok: false };
71
+ }
72
+ }
73
+ function readRecent(): Recent[] {
74
+ const stored = storedRecent();
75
+ return stored.ok ? stored.list : [];
76
+ }
77
+
78
+ function writeRecent(list: Recent[]): Recent[] {
79
+ try { storage()?.setItem(RECENT_KEY, JSON.stringify(list.map((r) => r.id))); } catch { /* keep in memory */ }
80
+ return list;
81
+ }
82
+
83
+ /** Newest first; a repeat (any letter case) moves to the top. */
84
+ function remember(list: Recent[], query: string): Recent[] {
85
+ const q = query.trim();
86
+ if (!q) return list;
87
+ const rest = list.filter((r) => r.id.toLowerCase() !== q.toLowerCase());
88
+ return writeRecent([{ id: q, label: q }, ...rest].slice(0, RECENT_MAX));
89
+ }
90
+
91
+ // ---------------------------------------------------------------------------
92
+ // Derive + search
93
+ // ---------------------------------------------------------------------------
94
+
95
+ function queryOf(arg: unknown): string {
96
+ return String((arg as { value?: unknown } | undefined)?.value ?? "").trim().slice(0, 256);
97
+ }
98
+
99
+ function derive(s: S): S {
100
+ const { next, active } = applyFacets(s, s.query ? rows : [], currency);
101
+ const n = next.results.length;
102
+ const ready = s.status === "ready";
103
+ return {
104
+ ...next,
105
+ hideRecent: !!s.query || s.recent.length === 0,
106
+ hideQueryArea: !s.query,
107
+ isLoading: s.status === "loading",
108
+ notLoading: s.status !== "loading",
109
+ notErrored: s.status !== "error",
110
+ ...errorFlags(s.status, s.errorKind),
111
+ countLabel: ready ? countText(n) : "",
112
+ showLabel: `Show ${countText(n)}`,
113
+ noResults: !ready || n === 0,
114
+ noEmpty: !ready || n > 0,
115
+ emptyText: active ? "No products match these filters." : `No products match “${s.query}”.`,
116
+ };
117
+ }
118
+
119
+ async function run(s: S, query: string): Promise<S | undefined> {
120
+ latestQuery = query;
121
+ if (!query) {
122
+ rows = [];
123
+ return derive({ ...s, query: "", status: "idle", panel: "none" });
124
+ }
125
+ try {
126
+ const res = await searchStoreProducts({ query, limit: RESULT_LIMIT });
127
+ if (query !== latestQuery) return undefined;
128
+ const products = res.products ?? [];
129
+ currency = products.map(currencyOf).find(Boolean) ?? currency;
130
+ rows = products.map(toRow).filter((r) => r.card.id);
131
+ return derive({ ...s, query, status: "ready" });
132
+ } catch (e) {
133
+ if (query !== latestQuery) return undefined;
134
+ rows = [];
135
+ return derive({ ...s, query, status: "error", errorKind: errorKindOf(e) });
136
+ }
137
+ }
138
+
139
+ const BOOT: S = derive({
140
+ ...FACETS_BOOT,
141
+ status: "idle", query: "", focusToken: "open", recent: [], hideRecent: true, hideQueryArea: true,
142
+ isLoading: false, notLoading: true, notErrored: true,
143
+ results: [], countLabel: "", showLabel: "", noResults: true, noEmpty: true, emptyText: "",
144
+ });
145
+
146
+ type A = (s: S, arg?: unknown) => S | undefined | Promise<S | undefined>;
147
+ const svc = defineService<S, {
148
+ load: A; change: A; submit: A; pickRecent: A; removeRecent: A; retry: A; openProduct: A; toggleSave: A;
149
+ sort: A; openFilters: A; openSort: A; closePanel: A;
150
+ selectPrice: A; selectOpt1: A; selectOpt2: A; selectOpt3: A; clearFilters: A;
151
+ }>("bareFashionSearch", {
152
+ state: BOOT,
153
+ actions: {
154
+ // Every open starts a fresh search: the host can keep this worker alive between
155
+ // opens, so clear the last query and re-request focus with a new token.
156
+ load: (s) => {
157
+ rows = [];
158
+ latestQuery = "";
159
+ return derive({ ...s, query: "", status: "idle", panel: "none", recent: readRecent(), focusToken: String(Date.now()) });
160
+ },
161
+ change: (s, arg) => {
162
+ const query = queryOf(arg);
163
+ return query === s.query && s.status !== "error" ? s : run(query ? { ...s, status: "loading" } : s, query);
164
+ },
165
+ // Keyboard Search key: search now and keep the query as a recent search.
166
+ submit: (s, arg) => {
167
+ const query = queryOf(arg) || s.query;
168
+ const recent = remember(s.recent, query);
169
+ return run({ ...s, recent, status: query ? "loading" : s.status }, query);
170
+ },
171
+ // A recent search fills the field and runs. Dismiss the keyboard first: the
172
+ // field adopts a worker value only while it is not focused.
173
+ pickRecent: (s, arg) => {
174
+ const query = idOf(arg);
175
+ if (!query) return s;
176
+ dismiss().catch(() => undefined);
177
+ return run({ ...s, recent: remember(s.recent, query), status: "loading" }, query);
178
+ },
179
+ removeRecent: (s, arg) => {
180
+ const id = idOf(arg);
181
+ return derive({ ...s, recent: writeRecent(s.recent.filter((r) => r.id !== id)) });
182
+ },
183
+ retry: (s) => run({ ...s, status: "loading" }, s.query),
184
+ // Opening a result counts as a completed search.
185
+ openProduct: (s, arg) => {
186
+ const id = cardId(arg);
187
+ if (!id) return s;
188
+ return navigate({ ...s, recent: remember(s.recent, s.query) }, { screenId: "himi_product", args: { id } });
189
+ },
190
+ toggleSave: (s, arg) => {
191
+ try { toggleFavorite(s.results, cardId(arg)); } catch { return s; }
192
+ return derive(s);
193
+ },
194
+ ...facetActions(derive),
195
+ },
196
+ });
197
+
198
+ svc.load();
199
+ export { svc };
@@ -0,0 +1,79 @@
1
+ // Tier-5 bundle: himi-store — Shop tab. A search bar over the category list.
2
+ // The bar opens the full-screen search (himi_search, worker himi-bare-fashion-search); this
3
+ // screen shows the site's categories as full-width image tiles.
4
+ import { defineService } from "@himalaya/state";
5
+ import { getCurrentCart } from "@himalaya/wix-ecom";
6
+ import { queryCollections } from "@himalaya/wix-stores-collections";
7
+ import { copyParam } from "../_shared/himi-copy.js";
8
+ import { publishBagCount } from "../_shared/bare-fashion-bag-count.js";
9
+ import { categoryCard, orderCategories, type CategoryCard } from "../_shared/bare-fashion-categories.js";
10
+ import { errorFlags, errorKindOf, type ErrorKind } from "../_shared/bare-fashion-load-error.js";
11
+
12
+ interface S {
13
+ status: "loading" | "ready" | "error";
14
+ categories: CategoryCard[];
15
+ cartCount: number;
16
+ emptyText: string;
17
+ isLoading: boolean;
18
+ isErrored: boolean;
19
+ notErrored: boolean;
20
+ hideCategories: boolean;
21
+ /** Why the last load failed: the error state shows the offline or the server variant. */
22
+ errorKind?: ErrorKind;
23
+ hideOfflineError?: boolean;
24
+ hideServerError?: boolean;
25
+ noEmpty: boolean;
26
+ }
27
+
28
+ /** Live cart size (sum of line-item quantities) for the Cart tab badge, or `ok: false` when the
29
+ * cart cannot be read: the screen does not fail, and the badge keeps its stored count. */
30
+ async function cartItemCount(): Promise<{ ok: true; count: number } | { ok: false }> {
31
+ try {
32
+ const cart = await getCurrentCart();
33
+ return { ok: true, count: (cart.lineItems ?? []).reduce((n, li) => n + (li.quantity ?? 1), 0) };
34
+ } catch {
35
+ return { ok: false };
36
+ }
37
+ }
38
+
39
+ /** Recompute every derived flag. State is not reactive: each action must re-derive. */
40
+ function derive(s: S): S {
41
+ const empty = s.status === "ready" && s.categories.length === 0;
42
+ return {
43
+ ...s,
44
+ isLoading: s.status === "loading",
45
+ isErrored: s.status === "error",
46
+ notErrored: s.status !== "error",
47
+ ...errorFlags(s.status, s.errorKind),
48
+ hideCategories: s.status !== "ready",
49
+ noEmpty: !empty,
50
+ emptyText: copyParam("shop.emptyText", "No categories are published yet."),
51
+ };
52
+ }
53
+
54
+ async function load(s: S): Promise<S> {
55
+ const [categoriesResult, live] = await Promise.all([
56
+ queryCollections({ query: { paging: { limit: 50 } } }).then((r) => ({ ok: true as const, r }), (e: unknown) => ({ ok: false as const, e })),
57
+ cartItemCount(),
58
+ ]);
59
+ if (live.ok) await publishBagCount(live.count);
60
+ const cartCount = live.ok ? live.count : s.cartCount;
61
+ if (!categoriesResult.ok) return derive({ ...s, status: "error", errorKind: errorKindOf(categoriesResult.e), cartCount });
62
+ const categories = orderCategories(categoriesResult.r.collections ?? []).map(categoryCard).filter((c) => c.id);
63
+ return derive({ ...s, status: "ready", categories, cartCount });
64
+ }
65
+
66
+ const svc = defineService<S, {
67
+ load: (s: S) => Promise<S>;
68
+ retry: (s: S) => Promise<S>;
69
+ }>("bareFashionShop", {
70
+ state: derive({ status: "loading", categories: [], cartCount: 0, emptyText: "",
71
+ isLoading: true, isErrored: false, notErrored: true, hideCategories: true, noEmpty: true }),
72
+ actions: {
73
+ load: (s) => load(s),
74
+ retry: (s) => load(derive({ ...s, status: "loading" })),
75
+ },
76
+ });
77
+
78
+ svc.load();
79
+ export { svc };