@wix/himalaya-cli 0.1748.0 → 0.1749.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.
- package/bin/himi +16 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/dev-server/src/builder.d.ts +3 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/schema.d.ts +9 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/l10n.mjs +1 -1
- package/dist/browser-plane/assets.json +7 -7
- package/dist/browser-plane/preview-deferred/{63821bbb8f637899037252154a5587f0596e4b537173c7f50551f9e44f7c22c0.bin → b40ea5d0d6251898ff5617139fde8be008dd11e99a70b14c1052093685401700.bin} +1 -1
- package/dist/browser-plane/sw.js +6 -6
- package/dist/browser-plane/t5-frame.html +1 -1
- package/dist/browser-plane/t5-worker-frame.html +1 -1
- package/dist/cli.mjs +118 -67
- package/dist/preview-assets.generated.bin +0 -0
- package/guides/component-reference.json +1 -1
- package/guides/component-reference.md +9 -0
- package/package.json +2 -2
- package/template-icons/medical-services.png +0 -0
- package/templates/beauty-salon/dev/index.ts +1 -0
- package/templates/beauty-salon/l10n/en.json +6 -0
- package/templates/beauty-salon/l10n/fr.json +11 -0
- package/templates/beauty-salon/l10n/he.json +9 -0
- package/templates/beauty-salon/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/beauty-salon/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/beauty-salon/tier5-src/himi-booking-bookings/index.ts +6 -2
- package/templates/beauty-salon/tier5-src/himi-booking-service/index.ts +7 -2
- package/templates/beauty-salon/tier5-src/himi-booking-time/index.ts +6 -4
- package/templates/beauty-salon-owner/dev/index.ts +54 -0
- package/templates/beauty-salon-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/beauty-salon-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/branded-lite/dev/index.ts +1 -0
- package/templates/branded-lite/l10n/fr.json +5 -0
- package/templates/branded-lite/l10n/he.json +4 -0
- package/templates/branded-lite/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/branded-lite/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/branded-lite/tier5-src/himi-booking-bookings/index.ts +6 -2
- package/templates/branded-lite/tier5-src/himi-booking-service/index.ts +7 -2
- package/templates/branded-lite/tier5-src/himi-booking-time/index.ts +6 -4
- package/templates/community-club/dev/index.ts +1 -0
- package/templates/community-club/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/community-club-owner/dev/index.ts +54 -0
- package/templates/community-club-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/community-club-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/conference/dev/index.ts +1 -0
- package/templates/events-meetups/dev/index.ts +1 -0
- package/templates/events-meetups/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/events-meetups-owner/dev/index.ts +54 -0
- package/templates/events-meetups-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/events-meetups-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/gym-fitness/dev/index.ts +1 -0
- package/templates/gym-fitness/l10n/en.json +6 -0
- package/templates/gym-fitness/l10n/fr.json +11 -0
- package/templates/gym-fitness/l10n/he.json +9 -0
- package/templates/gym-fitness/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/gym-fitness/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/gym-fitness/tier5-src/himi-booking-bookings/index.ts +6 -2
- package/templates/gym-fitness/tier5-src/himi-booking-service/index.ts +7 -2
- package/templates/gym-fitness/tier5-src/himi-booking-time/index.ts +6 -4
- package/templates/gym-fitness-owner/dev/index.ts +54 -0
- package/templates/gym-fitness-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/gym-fitness-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/medical-services/README.md +53 -0
- package/templates/medical-services/config.ts +320 -0
- package/templates/medical-services/dev/index.ts +17783 -0
- package/templates/medical-services/himalaya.content.json +46 -0
- package/templates/medical-services/himalaya.template.json +14 -0
- package/templates/medical-services/icon.png +0 -0
- package/templates/medical-services/platforms.json +1 -0
- package/templates/medical-services/tier5-src/_shared/app-locale.ts +61 -0
- package/templates/medical-services/tier5-src/_shared/booking-location.ts +147 -0
- package/templates/medical-services/tier5-src/_shared/booking-session.ts +222 -0
- package/templates/medical-services/tier5-src/_shared/branded-lite-hosted.ts +280 -0
- package/templates/medical-services/tier5-src/_shared/branded-load-state.ts +109 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-copy.ts +40 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-history.ts +51 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-message-model.ts +177 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-model.ts +56 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-presenter.ts +576 -0
- package/templates/medical-services/tier5-src/_shared/business-chat-transport.ts +148 -0
- package/templates/medical-services/tier5-src/_shared/collection-contract.ts +228 -0
- package/templates/medical-services/tier5-src/_shared/collection-query.ts +193 -0
- package/templates/medical-services/tier5-src/_shared/collection-service.ts +400 -0
- package/templates/medical-services/tier5-src/_shared/engine-intl.ts +523 -0
- package/templates/medical-services/tier5-src/_shared/faq-list.ts +405 -0
- package/templates/medical-services/tier5-src/_shared/himi-account.ts +194 -0
- package/templates/medical-services/tier5-src/_shared/himi-booking-saved.ts +88 -0
- package/templates/medical-services/tier5-src/_shared/himi-bookings.ts +318 -0
- package/templates/medical-services/tier5-src/_shared/himi-copy.ts +116 -0
- package/templates/medical-services/tier5-src/_shared/himi-store.ts +7 -0
- package/templates/medical-services/tier5-src/_shared/himi-text.ts +24 -0
- package/templates/medical-services/tier5-src/_shared/host-events.ts +66 -0
- package/templates/medical-services/tier5-src/_shared/hosted-checkout.ts +70 -0
- package/templates/medical-services/tier5-src/_shared/iap-attendance-form.ts +106 -0
- package/templates/medical-services/tier5-src/_shared/iap-attendance-purchase.ts +216 -0
- package/templates/medical-services/tier5-src/_shared/iap-attendance.ts +38 -0
- package/templates/medical-services/tier5-src/_shared/iap-worker.ts +127 -0
- package/templates/medical-services/tier5-src/_shared/inbox-rooms.ts +68 -0
- package/templates/medical-services/tier5-src/_shared/local-preferences.ts +38 -0
- package/templates/medical-services/tier5-src/_shared/member-access.ts +52 -0
- package/templates/medical-services/tier5-src/_shared/member-account-routes.ts +52 -0
- package/templates/medical-services/tier5-src/_shared/member-bookings.ts +107 -0
- package/templates/medical-services/tier5-src/_shared/member-screen.ts +99 -0
- package/templates/medical-services/tier5-src/_shared/notification-target.ts +141 -0
- package/templates/medical-services/tier5-src/_shared/offset-page.ts +10 -0
- package/templates/medical-services/tier5-src/_shared/online-actions.ts +76 -0
- package/templates/medical-services/tier5-src/_shared/owner-load-error.ts +348 -0
- package/templates/medical-services/tier5-src/_shared/payment-setup.ts +6 -0
- package/templates/medical-services/tier5-src/_shared/rich-body.ts +78 -0
- package/templates/medical-services/tier5-src/_shared/rich-content-link.ts +34 -0
- package/templates/medical-services/tier5-src/_shared/sign-in-sheet.ts +18 -0
- package/templates/medical-services/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/medical-services/tier5-src/_shared/weekly-goal.ts +107 -0
- package/templates/medical-services/tier5-src/_shared/wix-bi.ts +85 -0
- package/templates/medical-services/tier5-src/_shared/wix-member-links.ts +114 -0
- package/templates/medical-services/tier5-src/_shared/wix-service-price.ts +146 -0
- package/templates/medical-services/tier5-src/business-chat/index.ts +732 -0
- package/templates/medical-services/tier5-src/himi-booking-account/index.ts +252 -0
- package/templates/medical-services/tier5-src/himi-booking-bookings/index.ts +468 -0
- package/templates/medical-services/tier5-src/himi-booking-service/index.ts +1190 -0
- package/templates/medical-services/tier5-src/himi-booking-services/index.ts +791 -0
- package/templates/medical-services/tier5-src/himi-booking-time/index.ts +1729 -0
- package/templates/medical-services/tier5-src/himi-branded-lite-faq/index.ts +6 -0
- package/templates/medical-services/tier5-src/himi-branded-lite-notifications/index.ts +256 -0
- package/templates/medical-services/tier5-src/himi-clinic-home/index.ts +389 -0
- package/templates/medical-services/tier5-src/himi-store-account/index.ts +998 -0
- package/templates/medical-services/tokens.json +418 -0
- package/templates/nonprofit-cause/dev/index.ts +1 -0
- package/templates/nonprofit-cause/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/nonprofit-cause-owner/dev/index.ts +54 -0
- package/templates/nonprofit-cause-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/nonprofit-cause-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/owner-lite/dev/index.ts +54 -0
- package/templates/owner-lite/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/owner-lite/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/podcast-creator/dev/index.ts +1 -0
- package/templates/podcast-creator/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/podcast-creator-owner/dev/index.ts +54 -0
- package/templates/podcast-creator-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/podcast-creator-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/portfolio-creative/dev/index.ts +1 -0
- package/templates/portfolio-creative/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/portfolio-creative-owner/dev/index.ts +54 -0
- package/templates/portfolio-creative-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/portfolio-creative-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/restaurant/dev/index.ts +1 -0
- package/templates/restaurant/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/restaurant-owner/dev/index.ts +54 -0
- package/templates/restaurant-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/restaurant-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/retail-store/dev/index.ts +1 -0
- package/templates/retail-store/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/retail-store-owner/dev/index.ts +54 -0
- package/templates/retail-store-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/retail-store-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
- package/templates/salon-spa/dev/index.ts +1 -0
- package/templates/salon-spa/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/salon-spa/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/salon-spa/tier5-src/himi-booking-bookings/index.ts +6 -2
- package/templates/salon-spa/tier5-src/himi-booking-service/index.ts +7 -2
- package/templates/salon-spa/tier5-src/himi-booking-time/index.ts +6 -4
- package/templates/wholesale-store/dev/index.ts +1 -0
- package/templates/yoga-studio/dev/index.ts +1 -0
- package/templates/yoga-studio/l10n/en.json +6 -0
- package/templates/yoga-studio/l10n/fr.json +11 -0
- package/templates/yoga-studio/l10n/he.json +9 -0
- package/templates/yoga-studio/tier5-src/_shared/member-bookings.ts +17 -4
- package/templates/yoga-studio/tier5-src/_shared/staff-name.ts +18 -0
- package/templates/yoga-studio/tier5-src/himi-booking-bookings/index.ts +6 -2
- package/templates/yoga-studio/tier5-src/himi-booking-service/index.ts +7 -2
- package/templates/yoga-studio/tier5-src/himi-booking-time/index.ts +6 -4
- package/templates/yoga-studio-owner/dev/index.ts +54 -0
- package/templates/yoga-studio-owner/tier5-src/_shared/aria-chat.ts +30 -4
- package/templates/yoga-studio-owner/tier5-src/himi-owner-lite-aria/index.ts +29 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
// The FAQ as a registrable service: one worker for the himi_faq screen and for a migrated page's FAQ section.
|
|
2
|
+
// registerFaqList(name) -> the screen: every question, search, categories (the himi_faq bundle is this call);
|
|
3
|
+
// registerFaqList(name, options) -> a section: `limit` (the source's NumberOfFAQs), `showCategories`, `firstOpen`, `oneAtATime`, `showSearch`.
|
|
4
|
+
// State and flags are the screen's own (`loadingHidden`, `errorHidden`, ...), so the faq-list recipe and the himi_faq descriptor bind the same names.
|
|
5
|
+
import { defineService } from "@himalaya/state";
|
|
6
|
+
import {
|
|
7
|
+
queryCategories,
|
|
8
|
+
queryQuestionEntries,
|
|
9
|
+
type WixCategory,
|
|
10
|
+
type WixQuestionEntry,
|
|
11
|
+
} from "@himalaya/wix-faq";
|
|
12
|
+
import { openRichLink } from "../_shared/rich-content-link.js";
|
|
13
|
+
import { richBody, ROW_IMAGE_WIDTH } from "../_shared/rich-body.js";
|
|
14
|
+
|
|
15
|
+
interface FaqQuestion {
|
|
16
|
+
id: string;
|
|
17
|
+
categoryId: string;
|
|
18
|
+
question: string;
|
|
19
|
+
/** The PLAIN_TEXT answer: the RichContent fallback, and what shows with no trusted document. */
|
|
20
|
+
answer: string;
|
|
21
|
+
/** [rico] #3372 — the RICH_CONTENT answer as ONE document string, used ONLY when its text matches the plain
|
|
22
|
+
* answer (a legacy DraftJS record converts unreliably); "" otherwise. */
|
|
23
|
+
answerJson: string;
|
|
24
|
+
/** answerJson !== "" — gates the plain answer, because RichContent draws nothing for "". */
|
|
25
|
+
hasAnswerJson: boolean;
|
|
26
|
+
sortOrder?: number;
|
|
27
|
+
status: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface FaqCategory {
|
|
31
|
+
id: string;
|
|
32
|
+
title: string;
|
|
33
|
+
sortOrder?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface QuestionRow {
|
|
37
|
+
id: string;
|
|
38
|
+
title: string;
|
|
39
|
+
answer: string;
|
|
40
|
+
answerJson: string;
|
|
41
|
+
hasAnswerJson: boolean;
|
|
42
|
+
expanded: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface CategorySection {
|
|
46
|
+
id: string;
|
|
47
|
+
title: string;
|
|
48
|
+
questions: QuestionRow[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface FaqDisplayRow extends QuestionRow {
|
|
52
|
+
categoryTitle: string;
|
|
53
|
+
categoryHeaderHidden: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface FaqState {
|
|
57
|
+
status: "loading" | "ready" | "error" | "forbidden" | "notInstalled";
|
|
58
|
+
categories: FaqCategory[];
|
|
59
|
+
questions: FaqQuestion[];
|
|
60
|
+
sections: CategorySection[];
|
|
61
|
+
rows: FaqDisplayRow[];
|
|
62
|
+
query: string;
|
|
63
|
+
expandedIds: string[];
|
|
64
|
+
otherTitle: string;
|
|
65
|
+
loadingHidden: boolean;
|
|
66
|
+
contentHidden: boolean;
|
|
67
|
+
emptyHidden: boolean;
|
|
68
|
+
errorHidden: boolean;
|
|
69
|
+
forbiddenHidden: boolean;
|
|
70
|
+
notInstalledHidden: boolean;
|
|
71
|
+
/** The section's search field is off (the source's showSearchBar is false). */
|
|
72
|
+
searchHidden: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What a source FAQ section sets. Every option defaults to what the himi_faq screen does. */
|
|
76
|
+
export interface FaqListOptions {
|
|
77
|
+
/** Show at most this many questions (the source's NumberOfFAQs). */
|
|
78
|
+
limit?: number;
|
|
79
|
+
/** false: no category headers. */
|
|
80
|
+
showCategories?: boolean;
|
|
81
|
+
/** Open the first question when the list first loads. */
|
|
82
|
+
firstOpen?: boolean;
|
|
83
|
+
/** Opening a question closes the others. */
|
|
84
|
+
oneAtATime?: boolean;
|
|
85
|
+
/** false: no search field. */
|
|
86
|
+
showSearch?: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const PAGE_SIZE = 100;
|
|
90
|
+
|
|
91
|
+
/** Caps on the static pairs, matching `BRANDED_LITE_FAQ_LIMITS` in the app contract. The
|
|
92
|
+
* param is re-bounded here because a worker must not trust a descriptor it did not build. */
|
|
93
|
+
const STATIC_LIMITS = { pairs: 20, question: 200, answer: 1500 } as const;
|
|
94
|
+
const STATIC_CATEGORY: FaqCategory = { id: "himi-static-faq", title: "" };
|
|
95
|
+
|
|
96
|
+
/** The site's own FAQ pairs — questions it publishes as page sections rather than
|
|
97
|
+
* through the Wix FAQ app, carried in by `site analyze --apply` as the `staticFaq` onAppear
|
|
98
|
+
* param (a JSON string: action params are string-valued). They are the FALLBACK: shown only
|
|
99
|
+
* when the FAQ app yields no visible question, so an owner-edited Wix FAQ always wins. */
|
|
100
|
+
|
|
101
|
+
function parseStaticFaq(raw: unknown): FaqQuestion[] {
|
|
102
|
+
let pairs: unknown = raw;
|
|
103
|
+
if (typeof raw === "string") {
|
|
104
|
+
try {
|
|
105
|
+
pairs = JSON.parse(raw);
|
|
106
|
+
} catch {
|
|
107
|
+
// A malformed param is a DESCRIPTOR bug, not an empty FAQ: there is no fallback to
|
|
108
|
+
// offer, so the screen shows exactly what the Wix FAQ read says — its content, its
|
|
109
|
+
// empty card, or its error card — rather than pretending the site has no questions.
|
|
110
|
+
pairs = undefined;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!Array.isArray(pairs)) return [];
|
|
114
|
+
const out: FaqQuestion[] = [];
|
|
115
|
+
for (const pair of pairs) {
|
|
116
|
+
if (out.length >= STATIC_LIMITS.pairs) break;
|
|
117
|
+
const row = pair && typeof pair === "object" ? pair as { q?: unknown; a?: unknown } : {};
|
|
118
|
+
const question = typeof row.q === "string" ? row.q.trim().slice(0, STATIC_LIMITS.question) : "";
|
|
119
|
+
const answer = typeof row.a === "string" ? row.a.trim().slice(0, STATIC_LIMITS.answer) : "";
|
|
120
|
+
if (!question || !answer) continue;
|
|
121
|
+
out.push({
|
|
122
|
+
id: `himi-static-faq-${out.length}`,
|
|
123
|
+
categoryId: STATIC_CATEGORY.id,
|
|
124
|
+
question,
|
|
125
|
+
answer,
|
|
126
|
+
// The site's own pairs are plain strings: there is no document to render.
|
|
127
|
+
answerJson: "",
|
|
128
|
+
hasAnswerJson: false,
|
|
129
|
+
sortOrder: out.length,
|
|
130
|
+
status: "VISIBLE",
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function sortByWixOrder<T extends { id: string; sortOrder?: number }>(left: T, right: T): number {
|
|
137
|
+
const a = Number.isFinite(left.sortOrder) ? left.sortOrder! : Number.POSITIVE_INFINITY;
|
|
138
|
+
const b = Number.isFinite(right.sortOrder) ? right.sortOrder! : Number.POSITIVE_INFINITY;
|
|
139
|
+
return a - b || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function uniqueById<T extends { id: string }>(rows: T[]): T[] {
|
|
143
|
+
return [...new Map(rows.filter((row) => row.id).map((row) => [row.id, row])).values()];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function sectionsFor(state: Pick<FaqState, "categories" | "questions" | "query" | "expandedIds" | "otherTitle">, limit?: number): CategorySection[] {
|
|
147
|
+
const needle = state.query.trim().toLowerCase();
|
|
148
|
+
const visible = state.questions
|
|
149
|
+
.filter((row) => row.status.toUpperCase() !== "HIDDEN")
|
|
150
|
+
.filter((row) => !needle || `${row.question}\n${row.answer}`.toLowerCase().includes(needle))
|
|
151
|
+
.sort(sortByWixOrder)
|
|
152
|
+
.slice(0, limit ?? Number.POSITIVE_INFINITY);
|
|
153
|
+
const expanded = new Set(state.expandedIds);
|
|
154
|
+
const known = new Set(state.categories.map((category) => category.id));
|
|
155
|
+
const row = (question: FaqQuestion): QuestionRow => ({
|
|
156
|
+
id: question.id,
|
|
157
|
+
title: question.question,
|
|
158
|
+
answer: question.answer,
|
|
159
|
+
answerJson: question.answerJson,
|
|
160
|
+
hasAnswerJson: question.hasAnswerJson,
|
|
161
|
+
expanded: expanded.has(question.id),
|
|
162
|
+
});
|
|
163
|
+
const sections = state.categories
|
|
164
|
+
.slice()
|
|
165
|
+
.sort(sortByWixOrder)
|
|
166
|
+
.map((category) => ({
|
|
167
|
+
id: category.id,
|
|
168
|
+
title: category.title,
|
|
169
|
+
questions: visible.filter((question) => question.categoryId === category.id).map(row),
|
|
170
|
+
}))
|
|
171
|
+
.filter((section) => section.questions.length > 0);
|
|
172
|
+
const orphaned = visible.filter((question) => !known.has(question.categoryId));
|
|
173
|
+
if (orphaned.length) sections.push({ id: "other", title: state.otherTitle, questions: orphaned.map(row) });
|
|
174
|
+
return sections;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function ready(state: FaqState, options: FaqListOptions = {}): FaqState {
|
|
178
|
+
const sections = sectionsFor(state, options.limit);
|
|
179
|
+
const rows = sections.flatMap((section) => section.questions.map((question, index) => ({
|
|
180
|
+
...question,
|
|
181
|
+
categoryTitle: section.title,
|
|
182
|
+
// An untitled section (the static fallback's) has no header to show.
|
|
183
|
+
categoryHeaderHidden: options.showCategories === false || index !== 0 || !section.title,
|
|
184
|
+
})));
|
|
185
|
+
return {
|
|
186
|
+
...state,
|
|
187
|
+
status: "ready",
|
|
188
|
+
sections,
|
|
189
|
+
rows,
|
|
190
|
+
loadingHidden: true,
|
|
191
|
+
contentHidden: sections.length === 0,
|
|
192
|
+
emptyHidden: sections.length > 0,
|
|
193
|
+
errorHidden: true,
|
|
194
|
+
forbiddenHidden: true,
|
|
195
|
+
notInstalledHidden: true,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function failure(state: FaqState, status: FaqState["status"]): FaqState {
|
|
200
|
+
return {
|
|
201
|
+
...state,
|
|
202
|
+
status,
|
|
203
|
+
categories: [],
|
|
204
|
+
questions: [],
|
|
205
|
+
sections: [],
|
|
206
|
+
rows: [],
|
|
207
|
+
loadingHidden: true,
|
|
208
|
+
contentHidden: true,
|
|
209
|
+
emptyHidden: true,
|
|
210
|
+
errorHidden: status !== "error",
|
|
211
|
+
forbiddenHidden: status !== "forbidden",
|
|
212
|
+
notInstalledHidden: status !== "notInstalled",
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function httpStatus(error: unknown): number | undefined {
|
|
217
|
+
if (!error || typeof error !== "object") return undefined;
|
|
218
|
+
const row = error as { status?: unknown; statusCode?: unknown; response?: { status?: unknown }; message?: unknown };
|
|
219
|
+
for (const value of [row.status, row.statusCode, row.response?.status]) {
|
|
220
|
+
if (typeof value === "number") return value;
|
|
221
|
+
}
|
|
222
|
+
const match = typeof row.message === "string" ? row.message.match(/\b(403|428)\b/) : null;
|
|
223
|
+
return match ? Number(match[1]) : undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function allCategories(): Promise<FaqCategory[]> {
|
|
227
|
+
const rows: FaqCategory[] = [];
|
|
228
|
+
const seen = new Set<string>();
|
|
229
|
+
let cursor = "";
|
|
230
|
+
do {
|
|
231
|
+
const response = await queryCategories({ query: { cursorPaging: { limit: PAGE_SIZE, ...(cursor ? { cursor } : {}) } } });
|
|
232
|
+
rows.push(...(response.categories ?? []).map((category: WixCategory) => ({
|
|
233
|
+
id: category.id ?? "",
|
|
234
|
+
title: (category.title ?? "").trim() || "Other",
|
|
235
|
+
...(typeof category.sortOrder === "number" ? { sortOrder: category.sortOrder } : {}),
|
|
236
|
+
})));
|
|
237
|
+
const next = response.pagingMetadata?.cursors?.next ?? "";
|
|
238
|
+
if (!next || seen.has(next)) break;
|
|
239
|
+
seen.add(next);
|
|
240
|
+
cursor = next;
|
|
241
|
+
} while (cursor);
|
|
242
|
+
return uniqueById(rows);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The answer's text with every whitespace run collapsed to one space: PLAIN_TEXT and a document's plain text
|
|
246
|
+
* may separate blocks differently ("\n" vs "\n\n"), and that is not a garbled conversion, but a space inside
|
|
247
|
+
* the text is content ("New York" is not "NewYork"). */
|
|
248
|
+
const normalizeSpace = (text: string): string => text.replace(/\s+/g, " ").trim();
|
|
249
|
+
|
|
250
|
+
/** [rico] #3372 — the RICH_CONTENT answer as a document string, trusted ONLY when its text matches the
|
|
251
|
+
* PLAIN_TEXT answer. Wix converts a legacy DraftJS answer to Ricos unreliably, and the plain answer is
|
|
252
|
+
* the one a visitor has always been shown, so a document that disagrees with it is dropped. */
|
|
253
|
+
function richAnswer(plain: string, rich: unknown): string {
|
|
254
|
+
// richBody encodes FIRST, so a document past the viewer's limits (too deep included) is rejected without an
|
|
255
|
+
// unbounded plain-text walk, and gives "". Answers render in a list, so their media are list-sized.
|
|
256
|
+
const { bodyJson, bodyText } = richBody(rich, "", ROW_IMAGE_WIDTH);
|
|
257
|
+
return bodyJson && normalizeSpace(bodyText) === normalizeSpace(plain) ? bodyJson : "";
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Every question entry in one content format, following THAT query's own cursors: a cursor belongs to the
|
|
261
|
+
* request that returned it, so the two formats never share one. */
|
|
262
|
+
async function entriesIn(contentFormat: "PLAIN_TEXT" | "RICH_CONTENT"): Promise<WixQuestionEntry[]> {
|
|
263
|
+
const rows: WixQuestionEntry[] = [];
|
|
264
|
+
const seen = new Set<string>();
|
|
265
|
+
let cursor = "";
|
|
266
|
+
do {
|
|
267
|
+
const response = await queryQuestionEntries({
|
|
268
|
+
query: {
|
|
269
|
+
cursorPaging: { limit: PAGE_SIZE, ...(cursor ? { cursor } : {}) },
|
|
270
|
+
filter: { status: { $in: ["VISIBLE", "UNKNOWN"] } },
|
|
271
|
+
},
|
|
272
|
+
contentFormat,
|
|
273
|
+
});
|
|
274
|
+
rows.push(...(response.questionEntries ?? []));
|
|
275
|
+
const next = response.pagingMetadata?.cursors?.next ?? "";
|
|
276
|
+
if (!next || seen.has(next)) break;
|
|
277
|
+
seen.add(next);
|
|
278
|
+
cursor = next;
|
|
279
|
+
} while (cursor);
|
|
280
|
+
return rows;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function allQuestions(): Promise<FaqQuestion[]> {
|
|
284
|
+
// Both formats are read in parallel, each paged on its own. PLAIN_TEXT is the list; a RICH_CONTENT read
|
|
285
|
+
// that fails only costs the documents — every answer keeps its plain text.
|
|
286
|
+
const [plain, rich] = await Promise.all([
|
|
287
|
+
entriesIn("PLAIN_TEXT"),
|
|
288
|
+
entriesIn("RICH_CONTENT").catch(() => [] as WixQuestionEntry[]),
|
|
289
|
+
]);
|
|
290
|
+
const richById = new Map(rich.map((question: WixQuestionEntry) => [question.id ?? "", question.richContent]));
|
|
291
|
+
const rows = plain.map((question: WixQuestionEntry) => {
|
|
292
|
+
const answer = (question.plainText ?? "").trim();
|
|
293
|
+
const answerJson = richAnswer(answer, richById.get(question.id ?? ""));
|
|
294
|
+
return {
|
|
295
|
+
id: question.id ?? "",
|
|
296
|
+
categoryId: question.categoryId ?? "",
|
|
297
|
+
question: (question.question ?? "").trim(),
|
|
298
|
+
answer,
|
|
299
|
+
answerJson,
|
|
300
|
+
hasAnswerJson: answerJson !== "",
|
|
301
|
+
...(typeof question.sortOrder === "number" ? { sortOrder: question.sortOrder } : {}),
|
|
302
|
+
status: question.status ?? "UNKNOWN",
|
|
303
|
+
};
|
|
304
|
+
}).filter((question) =>
|
|
305
|
+
question.id && question.question && question.status.toUpperCase() !== "HIDDEN",
|
|
306
|
+
);
|
|
307
|
+
return uniqueById(rows);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const INITIAL: FaqState = {
|
|
311
|
+
status: "loading",
|
|
312
|
+
categories: [],
|
|
313
|
+
questions: [],
|
|
314
|
+
sections: [],
|
|
315
|
+
rows: [],
|
|
316
|
+
query: "",
|
|
317
|
+
expandedIds: [],
|
|
318
|
+
otherTitle: "Other",
|
|
319
|
+
loadingHidden: false,
|
|
320
|
+
contentHidden: true,
|
|
321
|
+
emptyHidden: true,
|
|
322
|
+
errorHidden: true,
|
|
323
|
+
forbiddenHidden: true,
|
|
324
|
+
notInstalledHidden: true,
|
|
325
|
+
searchHidden: false,
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
export function registerFaqList(serviceName: string, options: FaqListOptions = {}) {
|
|
329
|
+
/** The site's own FAQ pairs: the FALLBACK shown only when the FAQ app yields no visible question (see parseStaticFaq). */
|
|
330
|
+
let staticQuestions: FaqQuestion[] = [];
|
|
331
|
+
const initial: FaqState = { ...INITIAL, searchHidden: options.showSearch === false };
|
|
332
|
+
let latestState = initial;
|
|
333
|
+
let loadGeneration = 0;
|
|
334
|
+
/** The reader opened or closed a question: `firstOpen` then stops deciding what is open. */
|
|
335
|
+
let touched = false;
|
|
336
|
+
|
|
337
|
+
const settle = (state: FaqState): FaqState => ready(state, options);
|
|
338
|
+
|
|
339
|
+
/** The site's pairs as a ready state, or undefined when there are none to fall back to. */
|
|
340
|
+
function staticFallback(state: FaqState): FaqState | undefined {
|
|
341
|
+
if (!staticQuestions.length) return undefined;
|
|
342
|
+
return settle({ ...state, categories: [STATIC_CATEGORY], questions: staticQuestions });
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function commit(state: FaqState): FaqState {
|
|
346
|
+
latestState = state;
|
|
347
|
+
return state;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function loadFaq(state: FaqState, arg?: { otherCategory?: string; staticFaq?: unknown }): Promise<FaqState> {
|
|
351
|
+
latestState = state;
|
|
352
|
+
// Only when the caller passed the param: `retry` is dispatched with no params and must keep
|
|
353
|
+
// the pairs the screen's onAppear delivered.
|
|
354
|
+
if (arg && "staticFaq" in arg) staticQuestions = parseStaticFaq(arg.staticFaq);
|
|
355
|
+
const generation = ++loadGeneration;
|
|
356
|
+
try {
|
|
357
|
+
const [categories, questions] = await Promise.all([allCategories(), allQuestions()]);
|
|
358
|
+
if (generation !== loadGeneration) return latestState;
|
|
359
|
+
const otherTitle = String(arg?.otherCategory ?? latestState.otherTitle).trim() || latestState.otherTitle;
|
|
360
|
+
let live = settle({ ...latestState, categories, questions, otherTitle });
|
|
361
|
+
const first = live.sections[0]?.questions[0]?.id;
|
|
362
|
+
if (options.firstOpen && !touched && !live.expandedIds.length && first) live = settle({ ...live, expandedIds: [first] });
|
|
363
|
+
// The Wix FAQ app answered but holds nothing a visitor can see — the site's own pairs,
|
|
364
|
+
// when it has any, instead of an empty state over a site whose FAQ is right there.
|
|
365
|
+
if (questions.length === 0) return commit(staticFallback(live) ?? live);
|
|
366
|
+
return commit(live);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (generation !== loadGeneration) return latestState;
|
|
369
|
+
console.warn("faq_list: the questions could not be read", error);
|
|
370
|
+
const status = httpStatus(error);
|
|
371
|
+
// No FAQ app (428), no visitor access (403) or a failed read: a site whose FAQ is page
|
|
372
|
+
// content still has an FAQ to show, so its pairs replace the failure card.
|
|
373
|
+
const fallback = staticFallback(latestState);
|
|
374
|
+
if (fallback) return commit(fallback);
|
|
375
|
+
return commit(failure(latestState, status === 403 ? "forbidden" : status === 428 ? "notInstalled" : "error"));
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return defineService(serviceName, {
|
|
380
|
+
state: initial,
|
|
381
|
+
actions: {
|
|
382
|
+
load: loadFaq,
|
|
383
|
+
retry: loadFaq,
|
|
384
|
+
search: (state: FaqState, arg?: { value?: string }) => commit(settle({ ...state, query: String(arg?.value ?? "").slice(0, 256) })),
|
|
385
|
+
toggle: (state: FaqState, arg?: { id?: string }) => {
|
|
386
|
+
const id = String(arg?.id ?? "");
|
|
387
|
+
if (!id || !state.questions.some((question) => question.id === id)) return state;
|
|
388
|
+
touched = true;
|
|
389
|
+
const open = state.expandedIds.includes(id);
|
|
390
|
+
const expandedIds = open
|
|
391
|
+
? state.expandedIds.filter((candidate) => candidate !== id)
|
|
392
|
+
: options.oneAtATime ? [id] : [...state.expandedIds, id];
|
|
393
|
+
return commit(settle({ ...state, expandedIds }));
|
|
394
|
+
},
|
|
395
|
+
// [rico] #3372 — an answer RichContent's on_link: `{ url }` of the tapped link. Only http(s), mailto and
|
|
396
|
+
// tel links reach open_url; the FAQ has no message line, so a refused link simply does nothing.
|
|
397
|
+
openLink: async (state: FaqState, arg?: unknown) => {
|
|
398
|
+
await openRichLink(arg);
|
|
399
|
+
return state;
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export { sectionsFor, httpStatus, parseStaticFaq };
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
2
|
+
// Relative for node resolution (see himi-store.ts note); rewritten off-repo.
|
|
3
|
+
import { displayName, getMyData, initials, type WixMyData } from "@himalaya/wix-users";
|
|
4
|
+
import { getMyMember } from "@himalaya/wix-members";
|
|
5
|
+
import { wixMediaUrl } from "@himalaya/wix-media";
|
|
6
|
+
import { formatDateMedium, formatWeekdayDateTime } from "../_shared/engine-intl.js";
|
|
7
|
+
import { copyParam } from "./himi-copy.js";
|
|
8
|
+
|
|
9
|
+
export interface AccountProfile {
|
|
10
|
+
isMember: boolean;
|
|
11
|
+
identityLine: string;
|
|
12
|
+
detailLine: string;
|
|
13
|
+
memberName: string;
|
|
14
|
+
memberEmail: string;
|
|
15
|
+
memberImage: string;
|
|
16
|
+
memberInitials: string;
|
|
17
|
+
hasMemberImage: boolean;
|
|
18
|
+
noMemberImage: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AccountActivityRow {
|
|
22
|
+
id: string;
|
|
23
|
+
title: string;
|
|
24
|
+
subtitle: string;
|
|
25
|
+
meta: string;
|
|
26
|
+
status: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function strOf(arg: unknown): string {
|
|
30
|
+
if (typeof arg === "string") return arg;
|
|
31
|
+
const a = arg as { value?: string; text?: string } | undefined;
|
|
32
|
+
return a?.value ?? a?.text ?? "";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function titleCase(value?: string): string {
|
|
36
|
+
return (value || "")
|
|
37
|
+
.toLowerCase()
|
|
38
|
+
.split(/[_\s-]+/)
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.map((part) => part[0].toUpperCase() + part.slice(1))
|
|
41
|
+
.join(" ");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function fmtDate(value?: string): string {
|
|
45
|
+
if (!value) return "";
|
|
46
|
+
const date = new Date(value);
|
|
47
|
+
if (Number.isNaN(date.getTime())) return "";
|
|
48
|
+
return formatDateMedium(date);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function fmtDateTime(value?: string): string {
|
|
52
|
+
if (!value) return "";
|
|
53
|
+
const date = new Date(value);
|
|
54
|
+
if (Number.isNaN(date.getTime())) return "";
|
|
55
|
+
return formatWeekdayDateTime(date);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function initialsFromName(value: string): string {
|
|
59
|
+
const parts = value.trim().split(/\s+/).filter(Boolean);
|
|
60
|
+
if (!parts.length) return "";
|
|
61
|
+
const first = parts[0]?.[0] ?? "";
|
|
62
|
+
const last = parts.length > 1 ? parts[parts.length - 1]?.[0] ?? "" : "";
|
|
63
|
+
return (first + last).toUpperCase();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function profileImage(data: WixMyData): string {
|
|
67
|
+
const profile = data.profile as { profileImage?: string; photo?: { url?: string }; imageUrl?: string } | undefined;
|
|
68
|
+
return profile?.profileImage || profile?.photo?.url || profile?.imageUrl || "";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function profileFromData(data: WixMyData, fallbackEmail = ""): AccountProfile {
|
|
72
|
+
const email = fallbackEmail || data.email || data.contactEmail || "";
|
|
73
|
+
const name = displayName(data) || email || "Member";
|
|
74
|
+
const image = profileImage(data);
|
|
75
|
+
return {
|
|
76
|
+
isMember: true,
|
|
77
|
+
identityLine: copyParam("account.signedIn", "Signed in"),
|
|
78
|
+
detailLine: email || name,
|
|
79
|
+
memberName: name,
|
|
80
|
+
memberEmail: email,
|
|
81
|
+
memberImage: image,
|
|
82
|
+
memberInitials: initials(data) || initialsFromName(name) || "M",
|
|
83
|
+
hasMemberImage: !!image,
|
|
84
|
+
noMemberImage: !image,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The signed-in site member's name and photo from their member record, or undefined when it
|
|
89
|
+
* cannot be read or names nobody (the caller then shows the email). */
|
|
90
|
+
async function siteMemberProfile(email: string): Promise<AccountProfile | undefined> {
|
|
91
|
+
try {
|
|
92
|
+
const member = (await getMyMember({ fieldsets: ["FULL"] }))?.member;
|
|
93
|
+
const name = [member?.contact?.firstName, member?.contact?.lastName].map((part) => (part ?? "").trim()).filter(Boolean).join(" ")
|
|
94
|
+
|| (member?.profile?.nickname ?? "").trim();
|
|
95
|
+
if (!name) return undefined;
|
|
96
|
+
const image = wixMediaUrl(member?.profile?.photo?.url) || wixMediaUrl(member?.profile?.photo?.id) || "";
|
|
97
|
+
return {
|
|
98
|
+
isMember: true,
|
|
99
|
+
identityLine: copyParam("account.signedIn", "Signed in"),
|
|
100
|
+
detailLine: email || name,
|
|
101
|
+
memberName: name,
|
|
102
|
+
memberEmail: email || member?.loginEmail || "",
|
|
103
|
+
memberImage: image,
|
|
104
|
+
memberInitials: initialsFromName(name) || "M",
|
|
105
|
+
hasMemberImage: !!image,
|
|
106
|
+
noMemberImage: !image,
|
|
107
|
+
};
|
|
108
|
+
} catch {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function guestProfile(detailLine: string): AccountProfile {
|
|
114
|
+
return {
|
|
115
|
+
isMember: false,
|
|
116
|
+
identityLine: copyParam("account.guestIdentity", "Browsing as a guest"),
|
|
117
|
+
detailLine,
|
|
118
|
+
memberName: "",
|
|
119
|
+
memberEmail: "",
|
|
120
|
+
memberImage: "",
|
|
121
|
+
memberInitials: "M",
|
|
122
|
+
hasMemberImage: false,
|
|
123
|
+
noMemberImage: true,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** `himi.wix.memberIdentity`: the member's email ("" when none is known) and whether a member is
|
|
128
|
+
* signed in. `signedIn` is true only when the host says so explicitly — native hosts answer it
|
|
129
|
+
* for a member whose email is not known yet (an injected or hosted session before `members/my`
|
|
130
|
+
* answers); hosts that predate it answer the email alone. */
|
|
131
|
+
export async function memberIdentity(): Promise<{ email: string; signedIn: boolean }> {
|
|
132
|
+
try {
|
|
133
|
+
const identity = await dispatch("himi.wix.memberIdentity") as unknown;
|
|
134
|
+
const direct = identity as { email?: unknown; signedIn?: unknown } | null;
|
|
135
|
+
const wrapped = identity as { payload?: { email?: unknown; signedIn?: unknown } } | null;
|
|
136
|
+
const raw = direct?.email ?? wrapped?.payload?.email;
|
|
137
|
+
const email = typeof raw === "string" ? raw : "";
|
|
138
|
+
const signedIn = (direct?.signedIn ?? wrapped?.payload?.signedIn) === true;
|
|
139
|
+
return { email, signedIn: signedIn || !!email };
|
|
140
|
+
} catch {
|
|
141
|
+
return { email: "", signedIn: false };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function signedInEmail(): Promise<string> {
|
|
146
|
+
return (await memberIdentity()).email;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A signed-in member nothing names yet: signed in, with a neutral label rather than another
|
|
150
|
+
* member's name or the sign-in form. */
|
|
151
|
+
function neutralMemberProfile(): AccountProfile {
|
|
152
|
+
const name = copyParam("account.member", "Member");
|
|
153
|
+
return {
|
|
154
|
+
isMember: true,
|
|
155
|
+
identityLine: copyParam("account.signedIn", "Signed in"),
|
|
156
|
+
detailLine: name,
|
|
157
|
+
memberName: name,
|
|
158
|
+
memberEmail: "",
|
|
159
|
+
memberImage: "",
|
|
160
|
+
memberInitials: "M",
|
|
161
|
+
hasMemberImage: false,
|
|
162
|
+
noMemberImage: true,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function loadMemberProfile(guestDetailLine: string): Promise<AccountProfile> {
|
|
167
|
+
// `himi.wix.memberIdentity` is the session source of truth. Visitor-scoped
|
|
168
|
+
// getMyData can still return a site/account nickname (e.g. apptestercoowner)
|
|
169
|
+
// after sign-out or for anonymous — never treat that as a signed-in member.
|
|
170
|
+
const identity = await memberIdentity();
|
|
171
|
+
if (!identity.signedIn) return guestProfile(guestDetailLine);
|
|
172
|
+
const storedEmail = identity.email;
|
|
173
|
+
if (!storedEmail) return (await siteMemberProfile("")) ?? neutralMemberProfile();
|
|
174
|
+
try {
|
|
175
|
+
const data = await getMyData();
|
|
176
|
+
return profileFromData(data, storedEmail);
|
|
177
|
+
} catch {
|
|
178
|
+
// The account profile is an owner endpoint: the web host refuses it, and a site member need
|
|
179
|
+
// not have one. The member's own record (the Members API) still names them.
|
|
180
|
+
const member = await siteMemberProfile(storedEmail);
|
|
181
|
+
if (member) return member;
|
|
182
|
+
return {
|
|
183
|
+
isMember: true,
|
|
184
|
+
identityLine: copyParam("account.signedIn", "Signed in"),
|
|
185
|
+
detailLine: storedEmail,
|
|
186
|
+
memberName: storedEmail,
|
|
187
|
+
memberEmail: storedEmail,
|
|
188
|
+
memberImage: "",
|
|
189
|
+
memberInitials: initialsFromName(storedEmail) || "M",
|
|
190
|
+
hasMemberImage: false,
|
|
191
|
+
noMemberImage: true,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Saved (favorite) booking services use the host's durable, app-scoped KV, shared by every
|
|
2
|
+
// screen worker — the service detail toggles a row (himi-booking-service `toggleSaved`), the
|
|
3
|
+
// Favorites list reads them (himi-booking-saved). Same model as _shared/himi-store-saved.ts: a
|
|
4
|
+
// failed write must never claim that a save succeeded.
|
|
5
|
+
export interface SavedService {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
/** The price line as the detail showed it ("$250"), kept so the list paints offline. */
|
|
9
|
+
price: string;
|
|
10
|
+
/** The duration line ("1 hr"). */
|
|
11
|
+
duration: string;
|
|
12
|
+
image: string;
|
|
13
|
+
category: string;
|
|
14
|
+
/** A course: the list's Book passes `course` to the time screen, which then refuses a course
|
|
15
|
+
* whose record failed to load instead of offering its sessions one by one. */
|
|
16
|
+
course?: boolean;
|
|
17
|
+
}
|
|
18
|
+
type StorageLike = { getItem(key: string): string | null; setItem(key: string, value: string): void };
|
|
19
|
+
type Host = { himalaya?: { storage?: StorageLike; input?: Record<string, unknown> }; localStorage?: StorageLike };
|
|
20
|
+
const BASE = "himi-booking:saved-services:v1";
|
|
21
|
+
function storage(): StorageLike {
|
|
22
|
+
const host = globalThis as Host;
|
|
23
|
+
const kv = host.himalaya?.storage ?? host.localStorage;
|
|
24
|
+
if (!kv) throw new Error("Saved-service storage is unavailable");
|
|
25
|
+
return kv;
|
|
26
|
+
}
|
|
27
|
+
const validPart = (value: unknown): value is string => typeof value === "string" && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) && value !== "default";
|
|
28
|
+
/** Saves belong to whoever made them. A signed-in member's live under the host's authenticated
|
|
29
|
+
* account + business — the partition collection preferences use, so a second member on the same
|
|
30
|
+
* device never sees them; a visitor's live on the device. A member's partition, read for the
|
|
31
|
+
* first time, takes the visitor's saves on this device (signing in carries them, like a cart),
|
|
32
|
+
* once — the device list is then emptied. */
|
|
33
|
+
function storeKey(kv: StorageLike): string {
|
|
34
|
+
const input = (globalThis as Host).himalaya?.input;
|
|
35
|
+
const account = input?.__himiPreferenceAccount, business = input?.__himiPreferenceBusiness;
|
|
36
|
+
if (!validPart(account) || !validPart(business)) return BASE;
|
|
37
|
+
const key = `${BASE}.${account}.${encodeURIComponent(business)}`;
|
|
38
|
+
if (kv.getItem(key) === null) {
|
|
39
|
+
const visitor = kv.getItem(BASE);
|
|
40
|
+
if (visitor !== null) {
|
|
41
|
+
kv.setItem(key, visitor);
|
|
42
|
+
// Empty the device list only once the member's copy is confirmed: a web host may swallow a
|
|
43
|
+
// failed write, and clearing then would lose the visitor's saves.
|
|
44
|
+
if (kv.getItem(key) === visitor) kv.setItem(BASE, "[]");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return key;
|
|
48
|
+
}
|
|
49
|
+
const str = (value: unknown): string => (typeof value === "string" ? value : "");
|
|
50
|
+
export function readSavedServices(): SavedService[] {
|
|
51
|
+
const kv = storage();
|
|
52
|
+
const raw = kv.getItem(storeKey(kv));
|
|
53
|
+
if (!raw) return [];
|
|
54
|
+
const parsed: unknown = JSON.parse(raw);
|
|
55
|
+
if (!Array.isArray(parsed)) throw new Error("Invalid saved-service data");
|
|
56
|
+
const rows = new Map<string, SavedService>();
|
|
57
|
+
for (const value of parsed) {
|
|
58
|
+
if (!value || typeof value !== "object") continue;
|
|
59
|
+
const row = value as Record<string, unknown>;
|
|
60
|
+
if (typeof row.id !== "string" || !row.id.trim() || typeof row.name !== "string") continue;
|
|
61
|
+
rows.set(row.id, { id: row.id, name: row.name, price: str(row.price), duration: str(row.duration), image: str(row.image), category: str(row.category), ...(row.course === true ? { course: true } : {}) });
|
|
62
|
+
}
|
|
63
|
+
return [...rows.values()];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isServiceSaved(id: string): boolean {
|
|
67
|
+
return readSavedServices().some(row => row.id === id);
|
|
68
|
+
}
|
|
69
|
+
export function writeSavedServices(rows: SavedService[]): void {
|
|
70
|
+
const kv = storage();
|
|
71
|
+
const key = storeKey(kv);
|
|
72
|
+
const value = JSON.stringify(rows);
|
|
73
|
+
kv.setItem(key, value);
|
|
74
|
+
// Web hosts may swallow quota/security errors. Confirm the synchronous write
|
|
75
|
+
// before displaying success; a missing/mismatched read is a failed save.
|
|
76
|
+
if (kv.getItem(key) !== value) throw new Error("Saved-service write did not persist");
|
|
77
|
+
}
|
|
78
|
+
/** Save or unsave; returns the new saved state. The newest save is listed first. */
|
|
79
|
+
export function toggleSavedService(service: SavedService): boolean {
|
|
80
|
+
if (!service.id.trim() || !service.name.trim()) throw new Error("No service to save");
|
|
81
|
+
const rows = readSavedServices();
|
|
82
|
+
const saved = !rows.some(row => row.id === service.id);
|
|
83
|
+
writeSavedServices(saved ? [service, ...rows] : rows.filter(row => row.id !== service.id));
|
|
84
|
+
return saved;
|
|
85
|
+
}
|
|
86
|
+
export function removeSavedService(id: string): void {
|
|
87
|
+
writeSavedServices(readSavedServices().filter(row => row.id !== id));
|
|
88
|
+
}
|