@huskel/sdk 0.4.3 → 0.4.6
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/dist/index.css +37 -0
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +90 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +102 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -358,6 +358,9 @@ var _HuskelClient = class _HuskelClient {
|
|
|
358
358
|
} catch (e) {
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
|
+
reRegister() {
|
|
362
|
+
instance = this;
|
|
363
|
+
}
|
|
361
364
|
setShopperId(id) {
|
|
362
365
|
this.shopperId = id;
|
|
363
366
|
}
|
|
@@ -491,11 +494,17 @@ function HuskelProvider({ siteId, apiUrl, apiToken, shopperId, children }) {
|
|
|
491
494
|
const clientRef = (0, import_react2.useRef)(null);
|
|
492
495
|
if (!clientRef.current) {
|
|
493
496
|
clientRef.current = new HuskelClient({ siteId, apiUrl, apiToken, shopperId });
|
|
497
|
+
} else {
|
|
498
|
+
clientRef.current.reRegister();
|
|
494
499
|
}
|
|
495
500
|
(0, import_react2.useEffect)(() => {
|
|
496
501
|
var _a;
|
|
497
502
|
(_a = clientRef.current) == null ? void 0 : _a.setShopperId(shopperId);
|
|
498
503
|
}, [shopperId]);
|
|
504
|
+
(0, import_react2.useEffect)(() => {
|
|
505
|
+
var _a;
|
|
506
|
+
(_a = clientRef.current) == null ? void 0 : _a.reRegister();
|
|
507
|
+
}, []);
|
|
499
508
|
(0, import_react2.useEffect)(() => {
|
|
500
509
|
return () => {
|
|
501
510
|
var _a;
|
|
@@ -527,6 +536,7 @@ function useSearch() {
|
|
|
527
536
|
return;
|
|
528
537
|
}
|
|
529
538
|
const gen = ++genRef.current;
|
|
539
|
+
setLoading(true);
|
|
530
540
|
setError(null);
|
|
531
541
|
try {
|
|
532
542
|
const res = await client.api.searchAutocomplete(query, limit);
|
|
@@ -747,7 +757,7 @@ var SearchIcon = () => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("svg", { wi
|
|
|
747
757
|
function SearchBar({
|
|
748
758
|
placeholder = "Search products\u2026",
|
|
749
759
|
limit = 10,
|
|
750
|
-
debounceMs =
|
|
760
|
+
debounceMs = 300,
|
|
751
761
|
onSelect,
|
|
752
762
|
className,
|
|
753
763
|
inputClassName,
|
|
@@ -758,18 +768,28 @@ function SearchBar({
|
|
|
758
768
|
}) {
|
|
759
769
|
const [query, setQuery] = (0, import_react8.useState)("");
|
|
760
770
|
const [open, setOpen] = (0, import_react8.useState)(false);
|
|
771
|
+
const [isDebouncing, setIsDebouncing] = (0, import_react8.useState)(false);
|
|
761
772
|
const { results, loading, search, clear } = useSearch();
|
|
773
|
+
const client = useHuskelContext();
|
|
762
774
|
const timer = (0, import_react8.useRef)();
|
|
763
775
|
const wrap = (0, import_react8.useRef)(null);
|
|
776
|
+
const ignoreNextQueryChange = (0, import_react8.useRef)(false);
|
|
764
777
|
(0, import_react8.useEffect)(() => {
|
|
778
|
+
if (ignoreNextQueryChange.current) {
|
|
779
|
+
ignoreNextQueryChange.current = false;
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
765
782
|
clearTimeout(timer.current);
|
|
766
783
|
if (!query.trim()) {
|
|
767
784
|
clear();
|
|
768
785
|
setOpen(false);
|
|
786
|
+
setIsDebouncing(false);
|
|
769
787
|
return;
|
|
770
788
|
}
|
|
771
789
|
setOpen(true);
|
|
790
|
+
setIsDebouncing(true);
|
|
772
791
|
timer.current = setTimeout(() => {
|
|
792
|
+
setIsDebouncing(false);
|
|
773
793
|
search(query, limit);
|
|
774
794
|
}, debounceMs);
|
|
775
795
|
return () => clearTimeout(timer.current);
|
|
@@ -782,10 +802,23 @@ function SearchBar({
|
|
|
782
802
|
return () => document.removeEventListener("mousedown", h);
|
|
783
803
|
}, []);
|
|
784
804
|
const handleSelect = (r) => {
|
|
805
|
+
if (query.trim()) {
|
|
806
|
+
client.api.searchVector(query, 1).catch(() => {
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
ignoreNextQueryChange.current = true;
|
|
785
810
|
setOpen(false);
|
|
786
811
|
setQuery(r.product.name);
|
|
787
812
|
onSelect == null ? void 0 : onSelect(r);
|
|
788
813
|
};
|
|
814
|
+
const handleCommitSearch = () => {
|
|
815
|
+
if (!query.trim()) return;
|
|
816
|
+
client.api.searchVector(query, 1).catch(() => {
|
|
817
|
+
});
|
|
818
|
+
if (results.length > 0) {
|
|
819
|
+
handleSelect(results[0]);
|
|
820
|
+
}
|
|
821
|
+
};
|
|
789
822
|
const showDrop = open && query.trim().length > 0;
|
|
790
823
|
const customStyles = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, (theme == null ? void 0 : theme.primaryColor) && { "--hsk-primary": theme.primaryColor }), (theme == null ? void 0 : theme.backgroundColor) && { "--hsk-bg": theme.backgroundColor }), (theme == null ? void 0 : theme.textColor) && { "--hsk-text": theme.textColor }), (theme == null ? void 0 : theme.fontFamily) && { "--hsk-font": theme.fontFamily }), (theme == null ? void 0 : theme.borderRadius) && { "--hsk-border-radius": theme.borderRadius });
|
|
791
824
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: cn("hsk-sb-wrap", classNames.root, className), ref: wrap, style: customStyles, children: [
|
|
@@ -799,45 +832,67 @@ function SearchBar({
|
|
|
799
832
|
placeholder,
|
|
800
833
|
onChange: (e) => setQuery(e.target.value),
|
|
801
834
|
onFocus: () => results.length > 0 && query.trim() && setOpen(true),
|
|
835
|
+
onKeyDown: (e) => {
|
|
836
|
+
if (e.key === "Enter") {
|
|
837
|
+
handleCommitSearch();
|
|
838
|
+
}
|
|
839
|
+
},
|
|
802
840
|
autoComplete: "off",
|
|
803
841
|
spellCheck: false
|
|
804
842
|
}
|
|
805
843
|
),
|
|
806
844
|
showDrop && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: cn("hsk-sb-drop", classNames.dropdown, dropdownClassName), style: { position: "absolute" }, children: [
|
|
807
|
-
loading && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-loading-bar" }),
|
|
808
|
-
results.length === 0
|
|
809
|
-
"
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
{
|
|
818
|
-
|
|
819
|
-
className: "hsk-sb-
|
|
820
|
-
style: {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
"
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
845
|
+
(loading || isDebouncing) && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-loading-bar" }),
|
|
846
|
+
(loading || isDebouncing) && results.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
847
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-skeleton-row", children: [
|
|
848
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "hsk-sb-skeleton-icon" }),
|
|
849
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-row-body", children: [
|
|
850
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-skeleton-text1" }),
|
|
851
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-skeleton-text2" })
|
|
852
|
+
] })
|
|
853
|
+
] }),
|
|
854
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-skeleton-row", children: [
|
|
855
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "hsk-sb-skeleton-icon" }),
|
|
856
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-row-body", children: [
|
|
857
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-skeleton-text1", style: { width: "45%" } }),
|
|
858
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-skeleton-text2", style: { width: "25%" } })
|
|
859
|
+
] })
|
|
860
|
+
] })
|
|
861
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
|
|
862
|
+
results.length === 0 && !loading && !isDebouncing && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-empty", children: [
|
|
863
|
+
"No results for \u201C",
|
|
864
|
+
query,
|
|
865
|
+
"\u201D"
|
|
866
|
+
] }),
|
|
867
|
+
results.map((r, i) => {
|
|
868
|
+
var _a;
|
|
869
|
+
return renderResult ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
870
|
+
"div",
|
|
871
|
+
{
|
|
872
|
+
onClick: () => handleSelect(r),
|
|
873
|
+
className: "hsk-sb-fade",
|
|
874
|
+
style: { animationDelay: `${i * 18}ms` },
|
|
875
|
+
children: renderResult(r)
|
|
876
|
+
},
|
|
877
|
+
r.id
|
|
878
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
879
|
+
"div",
|
|
880
|
+
{
|
|
881
|
+
className: cn("hsk-sb-row hsk-sb-fade", classNames.row),
|
|
882
|
+
style: { animationDelay: `${i * 18}ms` },
|
|
883
|
+
onClick: () => handleSelect(r),
|
|
884
|
+
children: [
|
|
885
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "hsk-sb-row-icon", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SearchIcon, {}) }),
|
|
886
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "hsk-sb-row-body", children: [
|
|
887
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-row-title", children: r.product.name }),
|
|
888
|
+
(r.product.category || r.product.brand) && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "hsk-sb-row-sub", children: (_a = r.product.category) != null ? _a : r.product.brand })
|
|
889
|
+
] })
|
|
890
|
+
]
|
|
891
|
+
},
|
|
892
|
+
r.id
|
|
893
|
+
);
|
|
894
|
+
})
|
|
895
|
+
] })
|
|
841
896
|
] })
|
|
842
897
|
] });
|
|
843
898
|
}
|