@cccsaurora/clue-ui 1.0.1 → 1.1.0-dev.10

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 (185) hide show
  1. package/ActionForm-D4ryHO0M.js +436 -0
  2. package/AnnotationDetails-YAPoqw3R.js +175 -0
  3. package/AnnotationPreview-DiQDjt9s.js +188 -0
  4. package/ClueEnrichContext-DIn6g8tw.js +522 -0
  5. package/FlexOne-BSYAhhtG.js +9 -0
  6. package/_Map-kgDsDYxq.js +64 -0
  7. package/_MapCache-DabaaWfq.js +161 -0
  8. package/_Uint8Array-BlVVH1tp.js +129 -0
  9. package/_baseAssignValue-CNbcU6Nb.js +25 -0
  10. package/_baseClone-D3a8Pa4T.js +284 -0
  11. package/_baseExtremum-B1o1zHjR.js +33 -0
  12. package/_baseFlatten-D4huXoEI.js +92 -0
  13. package/_baseGet-BSK_nnoz.js +109 -0
  14. package/_baseIsEqual-B5xLoweL.js +238 -0
  15. package/_baseIteratee-p6Nj07-n.js +126 -0
  16. package/_baseSlice-GAv_YFTT.js +20 -0
  17. package/_baseSum-D0WC1dN0.js +13 -0
  18. package/_baseUniq-CpupKWcL.js +89 -0
  19. package/_commonjsHelpers-CUmg6egw.js +6 -0
  20. package/_createAggregator-BpVy5xMi.js +63 -0
  21. package/_getPrototype-D1LAdQKO.js +5 -0
  22. package/_getTag-D3ToyefI.js +126 -0
  23. package/api/lookup/enrich.d.ts +3 -3
  24. package/cloneDeep-CjP5k9zW.js +8 -0
  25. package/components/AnnotationBody.js +49 -34
  26. package/components/AnnotationDetailPopover.js +36 -30
  27. package/components/AnnotationDetails.js +21 -155
  28. package/components/AnnotationEntry.js +50 -52
  29. package/components/AnnotationPreview.js +5 -5
  30. package/components/ClassificationChip.js +44 -23
  31. package/components/CountBadge.js +31 -26
  32. package/components/EnrichedCard.js +104 -92
  33. package/components/EnrichedChip.js +134 -109
  34. package/components/EnrichedTypography.js +136 -110
  35. package/components/ErrorBoundary.js +28 -24
  36. package/components/RetryFailedEnrichments.js +10 -9
  37. package/components/SourcePicker.js +57 -49
  38. package/components/actions/ActionForm.js +4 -4
  39. package/components/actions/ExecutePopover.js +64 -50
  40. package/components/actions/ResultModal.js +37 -34
  41. package/components/actions/form/schemaAdapter.js +39 -20
  42. package/components/display/graph/ExpandMoreButton.js +10 -10
  43. package/components/display/graph/elements/NodeCard.js +92 -76
  44. package/components/display/graph/elements/NodeTag.js +15 -13
  45. package/components/display/graph/index.js +258 -200
  46. package/components/display/graph/visualizations/Leaf.js +88 -69
  47. package/components/display/graph/visualizations/cloud/index.js +98 -81
  48. package/components/display/graph/visualizations/icons/BaseIcon.js +26 -21
  49. package/components/display/graph/visualizations/icons/BugIcon.js +12 -12
  50. package/components/display/graph/visualizations/icons/HostIcon.js +12 -12
  51. package/components/display/graph/visualizations/icons/NetworkIcon.js +12 -12
  52. package/components/display/graph/visualizations/icons/ProcessIcon.js +12 -12
  53. package/components/display/graph/visualizations/icons/TargetIcon.js +13 -13
  54. package/components/display/graph/visualizations/icons/index.js +14 -13
  55. package/components/display/graph/visualizations/panels/NodePanel.js +10 -8
  56. package/components/display/graph/visualizations/tree/BundleLine.js +108 -81
  57. package/components/display/graph/visualizations/tree/Triangle.js +13 -13
  58. package/components/display/graph/visualizations/tree/index.js +407 -305
  59. package/components/display/icons/Iconified.js +27 -12
  60. package/components/display/json/index.js +4 -4
  61. package/components/display/markdown/DynamicTabs.js +22 -17
  62. package/components/display/markdown/index.js +8527 -5670
  63. package/components/display/markdown/markdownPlugins/tabs.js +1 -1
  64. package/components/enrichment/EnrichPopover.js +55 -47
  65. package/components/fetchers/Fetcher.js +123 -95
  66. package/components/fetchers/PreviewModal.js +20 -17
  67. package/components/fetchers/StatusChip.js +22 -18
  68. package/components/group/Entry.js +13 -11
  69. package/components/group/Group.js +13 -10
  70. package/components/group/GroupControl.js +76 -65
  71. package/components/stats/QueryStatus.js +37 -28
  72. package/countBy-C69WslUA.js +14 -0
  73. package/data/event.js +6 -4
  74. package/database/index.js +2 -2
  75. package/debounce-bV0h5FC5.js +92 -0
  76. package/get-D3C3lEU3.js +8 -0
  77. package/groupBy-DC2oOuBN.js +14 -0
  78. package/hooks/ClueActionContext.d.ts +1 -1
  79. package/hooks/ClueActionContext.js +6 -6
  80. package/hooks/ClueComponentContext.js +29 -23
  81. package/hooks/ClueConfigProvider.js +14 -12
  82. package/hooks/ClueDatabaseContext.js +19 -13
  83. package/hooks/ClueEnrichContext.js +8 -8
  84. package/hooks/ClueFetcherContext.js +74 -46
  85. package/hooks/ClueGroupContext.js +17 -14
  86. package/hooks/CluePopupContext.js +5 -5
  87. package/hooks/ClueProvider.js +12 -10
  88. package/hooks/selectors.js +22 -11
  89. package/hooks/useAnnotations.js +45 -33
  90. package/hooks/useClue.js +6 -4
  91. package/hooks/useClueActions.js +3 -3
  92. package/hooks/useClueConfig.js +5 -5
  93. package/hooks/useClueTypeConfig.d.ts +2 -1
  94. package/hooks/useClueTypeConfig.js +3 -3
  95. package/hooks/useComparator.js +722 -435
  96. package/hooks/useErrors.js +22 -18
  97. package/hooks/useMyHighlights.js +66 -36
  98. package/hooks/useMyLocalStorage.js +63 -43
  99. package/iconify-CXMreGTg.js +1782 -0
  100. package/icons/Action.js +66 -49
  101. package/icons/Assessment.js +84 -68
  102. package/icons/Context.js +75 -61
  103. package/icons/Opinion.js +77 -65
  104. package/icons/iconMap.js +2 -2
  105. package/identity-CPGTqrE4.js +6 -0
  106. package/index-BDVjGvMI.js +696 -0
  107. package/index-BHPT3qoB.js +1172 -0
  108. package/index-BMxyILVD.js +465 -0
  109. package/index-BbPn6-Mw.js +15750 -0
  110. package/index-Dz1kF2MU.js +17654 -0
  111. package/isEmpty-BQkZubqU.js +29 -0
  112. package/isNil-CIubwp4T.js +6 -0
  113. package/isObject-FTY-5JQX.js +7 -0
  114. package/isObjectLike-OAgjjZye.js +48 -0
  115. package/isSymbol-Xd2FsJyp.js +8 -0
  116. package/last-CUCl67Im.js +7 -0
  117. package/main.d.ts +0 -1
  118. package/main.js +58 -68
  119. package/package.json +1 -1
  120. package/sortBy-B-UKp4GT.js +100 -0
  121. package/sumBy-MYkDPHZL.js +8 -0
  122. package/tabs-xGuUGsJd.js +254 -0
  123. package/text/Frequency.js +42 -23
  124. package/toFinite-Bc55msYj.js +16 -0
  125. package/toNumber-DPxy1FBy.js +39 -0
  126. package/useClueTypeConfig-CH-nGq6a.js +3184 -0
  127. package/utils/chain.js +91 -64
  128. package/utils/classificationParser.d.ts +8 -8
  129. package/utils/classificationParser.js +517 -267
  130. package/utils/constants.js +35 -10
  131. package/utils/graph.js +72 -45
  132. package/utils/hashUtil.js +7 -7
  133. package/utils/line.js +131 -81
  134. package/utils/loggerUtil.js +5 -3
  135. package/utils/sessionStorage.js +41 -29
  136. package/utils/utils.js +9 -9
  137. package/utils/window.js +21 -10
  138. package/utils-Dr4wbKBZ.js +4182 -0
  139. package/ActionForm-WIj7BfD5.js +0 -340
  140. package/AnnotationPreview-DWRDhxUB.js +0 -140
  141. package/ClueEnrichContext-CbJVBm4w.js +0 -397
  142. package/FlexOne-BXWFOd1T.js +0 -6
  143. package/_Map-DXNg_Z-q.js +0 -54
  144. package/_MapCache-Cu25RRDU.js +0 -129
  145. package/_Uint8Array-DlJCtTvG.js +0 -102
  146. package/_baseAssignValue-CUmzp727.js +0 -20
  147. package/_baseClone-BlMmRXeX.js +0 -208
  148. package/_baseExtremum-P_0akmCi.js +0 -27
  149. package/_baseFlatten-CN7vDNEQ.js +0 -72
  150. package/_baseGet-Dgf6_xCm.js +0 -80
  151. package/_baseIsEqual-Cpjtfb3Q.js +0 -173
  152. package/_baseIteratee-CP1bocOX.js +0 -95
  153. package/_baseSlice-M5RKzt1A.js +0 -10
  154. package/_baseSum-wEbgNeUs.js +0 -10
  155. package/_baseUniq-tMFmk80M.js +0 -61
  156. package/_commonjsHelpers-C6fGbg64.js +0 -6
  157. package/_createAggregator-B4Cav8ZM.js +0 -53
  158. package/_getPrototype-CHAFQYL_.js +0 -5
  159. package/_getTag-BV_UoLYG.js +0 -90
  160. package/cloneDeep-BPVpFBzJ.js +0 -8
  161. package/countBy-DOutsa_w.js +0 -8
  162. package/debounce-DryYcbJ4.js +0 -56
  163. package/get-Bow1vKwx.js +0 -8
  164. package/groupBy-BheQYl6f.js +0 -8
  165. package/iconify-BBckr5AQ.js +0 -1263
  166. package/identity-ByMq8VxU.js +0 -6
  167. package/index-4YFAh_sa.js +0 -358
  168. package/index-E7g8cRyW.js +0 -568
  169. package/index-HuYhutsd.js +0 -975
  170. package/index-WvFmjbxF.js +0 -12734
  171. package/index-p5_wX7q1.js +0 -11729
  172. package/isEmpty-g47Qir2A.js +0 -21
  173. package/isNil-CjWwlQS3.js +0 -6
  174. package/isObject-B53jY8Qg.js +0 -7
  175. package/isObjectLike-BatpeCIi.js +0 -29
  176. package/isSymbol-C3_SC0Qp.js +0 -8
  177. package/isUndefined-DiNCDKoz.js +0 -10
  178. package/last-7CdUxN0r.js +0 -7
  179. package/sortBy-ITdmD17L.js +0 -79
  180. package/sumBy-DxJUU2E8.js +0 -8
  181. package/tabs-CgADNA57.js +0 -195
  182. package/toFinite-BMy6GObD.js +0 -14
  183. package/toNumber-YVhnnJv4.js +0 -31
  184. package/useClueTypeConfig-BQ33EiyB.js +0 -2273
  185. package/utils-C_RX5uMP.js +0 -2704
@@ -0,0 +1,188 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { I as Icon } from "./iconify-CXMreGTg.js";
3
+ import { useTheme, Popper, Fade, Paper, Stack, Typography, Divider } from "@mui/material";
4
+ import AnnotationDetailPopover from "./components/AnnotationDetailPopover.js";
5
+ import useAnnotations from "./hooks/useAnnotations.js";
6
+ import { t as twitterShort } from "./utils-Dr4wbKBZ.js";
7
+ import { useState, useRef, useCallback, useEffect, useMemo } from "react";
8
+ import { c as createContext, u as useContextSelector } from "./index-BDVjGvMI.js";
9
+ import { SHOW_EVENT_ID, HIDE_EVENT_ID } from "./data/event.js";
10
+ import { safeDispatchEvent, safeAddEventListener } from "./utils/window.js";
11
+ import { i as isNull } from "./AnnotationDetails-YAPoqw3R.js";
12
+ const CluePopupContext = createContext(null);
13
+ const CluePopupProvider = ({ children }) => {
14
+ const [popupType, setPopupType] = useState(null);
15
+ const [detailsAnchorEl, setDetailsAnchorEl] = useState(null);
16
+ const [detailsData, setDetailsData] = useState(null);
17
+ const [detailsContent, setDetailsContent] = useState(null);
18
+ const [popoverProps, setPopoverProps] = useState({});
19
+ const externalOnClose = useRef(null);
20
+ const showInfo = useCallback((type, anchorEl, value, options) => {
21
+ safeDispatchEvent(
22
+ new CustomEvent(SHOW_EVENT_ID, {
23
+ detail: {
24
+ type,
25
+ anchorEl,
26
+ value,
27
+ options
28
+ }
29
+ })
30
+ );
31
+ }, []);
32
+ const closeInfo = useCallback(
33
+ (type, value) => {
34
+ safeDispatchEvent(
35
+ new CustomEvent(HIDE_EVENT_ID, {
36
+ detail: {
37
+ type,
38
+ value
39
+ }
40
+ })
41
+ );
42
+ },
43
+ // eslint-disable-next-line react-hooks/exhaustive-deps
44
+ []
45
+ );
46
+ const handleShowPopup = useCallback((event) => {
47
+ const { detail } = event;
48
+ setPopupType(detail.type);
49
+ setDetailsData(detail.value);
50
+ if (detail.options) {
51
+ if (detail.options.content) {
52
+ setDetailsContent(detail.options.content);
53
+ }
54
+ if (detail.options.onClose) {
55
+ externalOnClose.current = detail.options.onClose;
56
+ }
57
+ if (detail.options.popoverProps) {
58
+ setPopoverProps(detail.options.popoverProps);
59
+ }
60
+ }
61
+ setDetailsAnchorEl(detail.anchorEl);
62
+ }, []);
63
+ const handleHidePopup = useCallback(
64
+ (event) => {
65
+ const { detail } = event;
66
+ if (detailsData && (detail.type !== popupType || detailsData.type !== detail.value.type || detailsData.value !== detail.value.value)) {
67
+ return;
68
+ }
69
+ setPopupType(null);
70
+ setDetailsAnchorEl(null);
71
+ setDetailsData(null);
72
+ setDetailsContent(null);
73
+ setPopoverProps({});
74
+ externalOnClose.current = null;
75
+ },
76
+ [detailsData, popupType]
77
+ );
78
+ useEffect(() => {
79
+ const cleanupShow = safeAddEventListener(SHOW_EVENT_ID, handleShowPopup);
80
+ const cleanupHide = safeAddEventListener(HIDE_EVENT_ID, handleHidePopup);
81
+ return () => {
82
+ cleanupShow();
83
+ cleanupHide();
84
+ };
85
+ }, [handleShowPopup, handleHidePopup]);
86
+ const context = useMemo(
87
+ () => ({
88
+ showInfo,
89
+ closeInfo,
90
+ __detailsContent: detailsContent
91
+ }),
92
+ [closeInfo, detailsContent, showInfo]
93
+ );
94
+ return /* @__PURE__ */ jsxs(CluePopupContext.Provider, { value: context, children: [
95
+ children,
96
+ /* @__PURE__ */ jsx(
97
+ AnnotationDetailPopover,
98
+ {
99
+ ...popoverProps,
100
+ anchorEl: detailsAnchorEl,
101
+ open: !!detailsAnchorEl && popupType === "details",
102
+ enrichRequest: detailsData,
103
+ onClose: () => {
104
+ if (externalOnClose.current) {
105
+ externalOnClose.current();
106
+ } else {
107
+ closeInfo("details", detailsData);
108
+ }
109
+ }
110
+ }
111
+ ),
112
+ /* @__PURE__ */ jsx(
113
+ AnnotationPreview,
114
+ {
115
+ ...popoverProps,
116
+ anchorEl: detailsAnchorEl,
117
+ open: !!detailsAnchorEl && !isNull(popupType) && popupType !== "details",
118
+ annotationType: popupType,
119
+ enrichRequest: detailsData
120
+ }
121
+ )
122
+ ] });
123
+ };
124
+ const AnnotationPreview = ({ annotationType, anchorEl, enrichRequest, open, ...otherProps }) => {
125
+ const theme = useTheme();
126
+ const _detailsContent = useContextSelector(CluePopupContext, (state) => state.__detailsContent);
127
+ const [annotations] = useAnnotations(enrichRequest == null ? void 0 : enrichRequest.type, enrichRequest == null ? void 0 : enrichRequest.value, enrichRequest == null ? void 0 : enrichRequest.classification);
128
+ const filteredAnnotations = useMemo(
129
+ () => annotationType === "actionResults" ? [] : annotations.filter((annotation) => annotation.type === annotationType),
130
+ [annotationType, annotations]
131
+ );
132
+ const getZIndex = useCallback((_anchorEl) => {
133
+ try {
134
+ const zIndex = parseInt(window.getComputedStyle(_anchorEl, null).getPropertyValue("z-index"));
135
+ if (isNaN(zIndex)) {
136
+ return getZIndex(_anchorEl.parentElement);
137
+ }
138
+ return (zIndex + 2).toString();
139
+ } catch {
140
+ return "2";
141
+ }
142
+ }, []);
143
+ if (!filteredAnnotations.length && !_detailsContent) {
144
+ return null;
145
+ }
146
+ return /* @__PURE__ */ jsx(
147
+ Popper,
148
+ {
149
+ ...otherProps,
150
+ open,
151
+ anchorEl,
152
+ placement: "bottom-start",
153
+ sx: [{ zIndex: getZIndex(anchorEl) }, ...Array.isArray(otherProps.sx) ? otherProps.sx : [otherProps.sx]],
154
+ children: /* @__PURE__ */ jsx(Fade, { in: !!anchorEl, children: /* @__PURE__ */ jsx(Paper, { sx: { overflowY: "auto", maxWidth: "500px", boxShadow: theme.shadows[2] }, children: /* @__PURE__ */ jsx(Stack, { direction: "column", sx: { p: 1 }, spacing: 1, divider: /* @__PURE__ */ jsx(Divider, { orientation: "horizontal" }), children: _detailsContent ?? filteredAnnotations.map((annotation) => /* @__PURE__ */ jsxs(
155
+ Stack,
156
+ {
157
+ direction: "column",
158
+ spacing: 1,
159
+ children: [
160
+ /* @__PURE__ */ jsxs(Stack, { direction: "row", spacing: 1, alignItems: "center", children: [
161
+ annotation.analytic_icon && /* @__PURE__ */ jsx(
162
+ Icon,
163
+ {
164
+ icon: annotation.analytic_icon,
165
+ style: { filter: "drop-shadow(0px 0px 1px rgb(0 0 0 / 0.4))" }
166
+ }
167
+ ),
168
+ /* @__PURE__ */ jsx(Typography, { variant: "body1", sx: { textTransform: "capitalize" }, children: annotation.analytic ?? annotation.author }),
169
+ /* @__PURE__ */ jsxs(Typography, { variant: "caption", color: "text.secondary", children: [
170
+ "(",
171
+ twitterShort(annotation.timestamp),
172
+ ")"
173
+ ] })
174
+ ] }),
175
+ /* @__PURE__ */ jsx(Typography, { variant: "body2", children: annotation.summary }),
176
+ /* @__PURE__ */ jsx(Typography, { variant: "caption", color: "text.secondary", children: annotation.value })
177
+ ]
178
+ },
179
+ annotation.analytic + annotation.author + annotation.value + annotation.link + annotation.summary + annotation.timestamp
180
+ )) }) }) })
181
+ }
182
+ );
183
+ };
184
+ export {
185
+ AnnotationPreview as A,
186
+ CluePopupContext as C,
187
+ CluePopupProvider as a
188
+ };
@@ -0,0 +1,522 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { a as addAPIProvider } from "./iconify-CXMreGTg.js";
3
+ import { u as useClueTypeConfig, a as api, p as post } from "./useClueTypeConfig-CH-nGq6a.js";
4
+ import { clueDebugLogger } from "./utils/loggerUtil.js";
5
+ import { b as baseSlice } from "./_baseSlice-GAv_YFTT.js";
6
+ import { t as toFinite } from "./toFinite-Bc55msYj.js";
7
+ import { d as debounce } from "./debounce-bV0h5FC5.js";
8
+ import { g as groupBy } from "./groupBy-DC2oOuBN.js";
9
+ import { b as baseUniq } from "./_baseUniq-CpupKWcL.js";
10
+ import { b as baseIteratee } from "./_baseIteratee-p6Nj07-n.js";
11
+ import { useContext, useMemo, useState, useRef, useEffect, useCallback } from "react";
12
+ import { c as createContext } from "./index-BDVjGvMI.js";
13
+ import { ClueDatabaseContext } from "./hooks/ClueDatabaseContext.js";
14
+ import useClueConfig from "./hooks/useClueConfig.js";
15
+ function toInteger(value) {
16
+ var result = toFinite(value), remainder = result % 1;
17
+ return result === result ? remainder ? result - remainder : result : 0;
18
+ }
19
+ var nativeCeil = Math.ceil, nativeMax = Math.max;
20
+ function chunk(array, size, guard) {
21
+ if (size === void 0) {
22
+ size = 1;
23
+ } else {
24
+ size = nativeMax(toInteger(size), 0);
25
+ }
26
+ var length = array == null ? 0 : array.length;
27
+ if (!length || size < 1) {
28
+ return [];
29
+ }
30
+ var index = 0, resIndex = 0, result = Array(nativeCeil(length / size));
31
+ while (index < length) {
32
+ result[resIndex++] = baseSlice(array, index, index += size);
33
+ }
34
+ return result;
35
+ }
36
+ function uniq(array) {
37
+ return array && array.length ? baseUniq(array) : [];
38
+ }
39
+ function uniqBy(array, iteratee) {
40
+ return array && array.length ? baseUniq(array, baseIteratee(iteratee)) : [];
41
+ }
42
+ let getRandomValues;
43
+ const rnds8 = new Uint8Array(16);
44
+ function rng() {
45
+ if (!getRandomValues) {
46
+ getRandomValues = typeof crypto !== "undefined" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
47
+ if (!getRandomValues) {
48
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
49
+ }
50
+ }
51
+ return getRandomValues(rnds8);
52
+ }
53
+ const byteToHex = [];
54
+ for (let i = 0; i < 256; ++i) {
55
+ byteToHex.push((i + 256).toString(16).slice(1));
56
+ }
57
+ function unsafeStringify(arr, offset = 0) {
58
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
59
+ }
60
+ const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
61
+ const native = {
62
+ randomUUID
63
+ };
64
+ function v4(options, buf, offset) {
65
+ if (native.randomUUID && true && !options) {
66
+ return native.randomUUID();
67
+ }
68
+ options = options || {};
69
+ const rnds = options.random || (options.rng || rng)();
70
+ rnds[6] = rnds[6] & 15 | 64;
71
+ rnds[8] = rnds[8] & 63 | 128;
72
+ return unsafeStringify(rnds);
73
+ }
74
+ const ClueEnrichContext = createContext(null);
75
+ const ClueEnrichProvider = ({
76
+ children,
77
+ classification: _defaultClassification,
78
+ baseURL,
79
+ getToken,
80
+ onNetworkCall,
81
+ pickSources,
82
+ chunkSize = 15,
83
+ maxRequestCount = 4,
84
+ defaultTimeout = 5,
85
+ enabled = true,
86
+ ready = false,
87
+ publicIconify = true,
88
+ skipConfigCall = false,
89
+ customIconify: _customIconify,
90
+ debugLogging = true
91
+ }) => {
92
+ var _a;
93
+ const clueConfig = useClueConfig();
94
+ const database = useContext(ClueDatabaseContext);
95
+ const defaultClassification = useMemo(() => {
96
+ var _a2, _b;
97
+ return (_b = (_a2 = clueConfig.config) == null ? void 0 : _a2.c12nDef) == null ? void 0 : _b.UNRESTRICTED;
98
+ }, [clueConfig.config]);
99
+ const [isReady, setIsReady] = useState(ready);
100
+ const runningRequestCount = useRef(0);
101
+ const [sources, setSources] = useState([]);
102
+ const { availableSources, typesDetection } = useClueTypeConfig(
103
+ enabled && isReady,
104
+ baseURL,
105
+ debugLogging,
106
+ getToken,
107
+ onNetworkCall
108
+ );
109
+ useEffect(() => {
110
+ if (skipConfigCall || !enabled || !isReady) {
111
+ return;
112
+ }
113
+ const headers = {};
114
+ const token = getToken == null ? void 0 : getToken();
115
+ if (token) {
116
+ headers.Authorization = `Bearer ${token}`;
117
+ }
118
+ let requestConfig = { baseURL, headers };
119
+ if (onNetworkCall) {
120
+ requestConfig = onNetworkCall(requestConfig);
121
+ }
122
+ api.configs.get(requestConfig).then(clueConfig.setConfig);
123
+ }, [baseURL, onNetworkCall, skipConfigCall, isReady]);
124
+ const [customIconify, setCustomIconify] = useState(_customIconify);
125
+ useEffect(() => {
126
+ if (_customIconify) {
127
+ setCustomIconify(_customIconify);
128
+ }
129
+ }, [_customIconify]);
130
+ useEffect(() => {
131
+ if (publicIconify) {
132
+ return;
133
+ }
134
+ let iconURL = customIconify ?? void 0 ?? (baseURL == null ? void 0 : baseURL.replace(/^[^.]+/, "icons"));
135
+ if (!iconURL && typeof window !== "undefined" && !!window && !window.location.origin.includes("localhost")) {
136
+ iconURL = window.location.protocol + "//" + window.location.origin.replace(/^[^.]+/, "icons");
137
+ }
138
+ if (iconURL) {
139
+ clueDebugLogger(`Using ${iconURL} for iconify`, debugLogging);
140
+ addAPIProvider("", {
141
+ resources: [iconURL]
142
+ });
143
+ }
144
+ }, [baseURL, customIconify, debugLogging, publicIconify]);
145
+ const _addEntries = useCallback(
146
+ async (entries) => {
147
+ const newRecords = [];
148
+ for (const entry of entries) {
149
+ const { latency, source, type, value: value2, items, error } = entry;
150
+ if (error) {
151
+ newRecords.push({
152
+ id: v4(),
153
+ source,
154
+ type,
155
+ value: value2,
156
+ annotations: [],
157
+ classification: defaultClassification,
158
+ latency,
159
+ count: 0,
160
+ error
161
+ });
162
+ }
163
+ for (const item of items) {
164
+ const { classification, count, link, annotations } = item;
165
+ await database.selectors.find({ selector: { type, value: value2, source, classification } }).incrementalRemove();
166
+ const record = {
167
+ id: v4(),
168
+ source,
169
+ type,
170
+ value: value2,
171
+ annotations,
172
+ classification,
173
+ latency,
174
+ count,
175
+ link,
176
+ error
177
+ };
178
+ if (newRecords.some((_entry) => _entry.id === record.id)) {
179
+ record.id = v4();
180
+ }
181
+ newRecords.push(record);
182
+ }
183
+ }
184
+ const result = await database.selectors.bulkInsert(newRecords);
185
+ if (result.error.length > 0) {
186
+ console.warn("Errors on upsert:");
187
+ result.error.forEach((err) => console.warn(err.documentId, err.validationErrors ?? err.status));
188
+ }
189
+ },
190
+ [database, defaultClassification]
191
+ );
192
+ const enrich = useCallback(
193
+ async (type, value2, _options = {}) => {
194
+ var _a2, _b;
195
+ if (!type || !value2) {
196
+ console.error(`Type (${type}) or value (${value2}) is empty, returning empty response`);
197
+ return {};
198
+ }
199
+ const _sources = (pickSources == null ? void 0 : pickSources(sources, availableSources, [{ type, value: value2, classification: _options.classification }])) ?? sources;
200
+ const options = {
201
+ timeout: defaultTimeout,
202
+ force: false,
203
+ includeRaw: false,
204
+ noCache: false,
205
+ classification: defaultClassification,
206
+ sources: _sources,
207
+ ..._options
208
+ };
209
+ const headers = {};
210
+ const token = getToken == null ? void 0 : getToken();
211
+ if (token) {
212
+ headers.Authorization = `Bearer ${token}`;
213
+ }
214
+ let requestConfig = { baseURL, headers };
215
+ if (onNetworkCall) {
216
+ requestConfig = onNetworkCall(requestConfig);
217
+ }
218
+ const selector = {
219
+ type,
220
+ value: value2,
221
+ classification: options.classification
222
+ };
223
+ let statusRecord = await ((_a2 = database.status) == null ? void 0 : _a2.findOne({ selector: { ...selector } }).exec());
224
+ if (!statusRecord) {
225
+ statusRecord = await ((_b = database.status) == null ? void 0 : _b.insert({
226
+ id: v4(),
227
+ type: selector.type,
228
+ value: selector.value,
229
+ classification: selector.classification ?? defaultClassification,
230
+ status: "in-progress"
231
+ }));
232
+ } else {
233
+ await statusRecord.incrementalPatch({ status: "in-progress" });
234
+ }
235
+ try {
236
+ const enrichmentResult = await post([selector], options.sources, options, requestConfig);
237
+ const enrichData = Object.values(Object.values(enrichmentResult)[0])[0];
238
+ await (statusRecord == null ? void 0 : statusRecord.incrementalPatch({ status: "complete" }));
239
+ await _addEntries(Object.values(enrichData));
240
+ return enrichData;
241
+ } catch (e) {
242
+ console.error(e);
243
+ return {};
244
+ }
245
+ },
246
+ [
247
+ _addEntries,
248
+ availableSources,
249
+ baseURL,
250
+ database,
251
+ defaultClassification,
252
+ defaultTimeout,
253
+ getToken,
254
+ onNetworkCall,
255
+ pickSources,
256
+ sources
257
+ ]
258
+ );
259
+ const bulkEnrich = useCallback(
260
+ async (bulkRequest, _options) => {
261
+ const _sources = (pickSources == null ? void 0 : pickSources(sources, availableSources, bulkRequest)) ?? sources;
262
+ const options = {
263
+ timeout: defaultTimeout,
264
+ includeRaw: false,
265
+ noCache: false,
266
+ classification: defaultClassification,
267
+ sources: _sources,
268
+ ..._options
269
+ };
270
+ const headers = {};
271
+ const token = getToken == null ? void 0 : getToken();
272
+ if (token) {
273
+ headers.Authorization = `Bearer ${token}`;
274
+ }
275
+ let requestConfig = { baseURL, headers };
276
+ if (onNetworkCall) {
277
+ requestConfig = onNetworkCall(requestConfig);
278
+ }
279
+ const statuses = [];
280
+ for (const selector of bulkRequest) {
281
+ const query = { type: selector.type, value: selector.value, classification: options.classification };
282
+ let statusRecord = await database.status.findOne({
283
+ selector: query
284
+ }).incrementalPatch({
285
+ status: "in-progress"
286
+ });
287
+ if (!statusRecord) {
288
+ statusRecord = await database.status.insert({
289
+ id: v4(),
290
+ ...query,
291
+ status: "in-progress",
292
+ sources: options.sources
293
+ });
294
+ }
295
+ statuses.push(statusRecord.toMutableJSON());
296
+ }
297
+ try {
298
+ const result = await post(bulkRequest, options.sources, options, requestConfig);
299
+ const entries = Object.values(result).flatMap(Object.values).flatMap(Object.values);
300
+ await _addEntries(entries);
301
+ return result;
302
+ } catch (e) {
303
+ console.error(e);
304
+ return {};
305
+ } finally {
306
+ database.status.bulkUpsert(
307
+ uniqBy(statuses, (_record) => _record.id).map((record) => {
308
+ record.status = "complete";
309
+ return record;
310
+ })
311
+ );
312
+ }
313
+ },
314
+ [
315
+ pickSources,
316
+ sources,
317
+ availableSources,
318
+ defaultTimeout,
319
+ defaultClassification,
320
+ getToken,
321
+ baseURL,
322
+ onNetworkCall,
323
+ database,
324
+ _addEntries
325
+ ]
326
+ );
327
+ const enrichFailedEnrichments = useCallback(async () => {
328
+ const failedEnrichments = await database.selectors.find({ selector: { error: { $exists: true } } }).exec();
329
+ const byClassification = groupBy(failedEnrichments, "classification");
330
+ const newRequests = [];
331
+ for (const [classification, selectors] of Object.entries(byClassification)) {
332
+ const bySelector = groupBy(selectors, (_selector) => `${_selector.type}:${_selector.value}`);
333
+ Object.values(bySelector).forEach((records) => {
334
+ newRequests.push({
335
+ id: v4(),
336
+ type: records[0].type,
337
+ value: records[0].value,
338
+ classification,
339
+ sources: uniq(records.map((_record) => _record.source)).sort(),
340
+ status: "pending"
341
+ });
342
+ });
343
+ }
344
+ await database.status.bulkInsert(newRequests);
345
+ await database.selectors.bulkRemove(failedEnrichments);
346
+ }, [database]);
347
+ const enrichQueued = useMemo(
348
+ () => debounce(
349
+ async () => {
350
+ if (!(database == null ? void 0 : database.status)) {
351
+ return;
352
+ } else if (database.status.closed) {
353
+ console.warn("Status database is closed, will not enrich");
354
+ return;
355
+ }
356
+ const selectors = await database.status.find({ selector: { status: "pending" } }).update({ $set: { status: "in-progress" } });
357
+ if (selectors.length < 1) {
358
+ return;
359
+ }
360
+ const chunks = chunk(selectors, chunkSize);
361
+ clueDebugLogger(
362
+ `Enriching ${selectors.length} selectors in ${chunks.length} chunks of ${chunkSize}.`,
363
+ debugLogging
364
+ );
365
+ await Promise.all(
366
+ // For performance reasons, we chunk the requests. This will allow us to take advantage of parellelization in the
367
+ // backend, both on the pod level and kubernetes level
368
+ chunks.map(async (reqsChunk) => {
369
+ let _interval = null;
370
+ if (runningRequestCount.current <= maxRequestCount) {
371
+ runningRequestCount.current += 1;
372
+ } else {
373
+ let startOfWait = Date.now();
374
+ await new Promise((res) => {
375
+ _interval = setInterval(() => {
376
+ clueDebugLogger(
377
+ `Waiting on ${runningRequestCount.current} existing requests to complete (total delay: ${Date.now() - startOfWait}ms)`,
378
+ debugLogging
379
+ );
380
+ if (runningRequestCount.current < maxRequestCount) {
381
+ res();
382
+ }
383
+ }, 400);
384
+ }).finally(() => {
385
+ runningRequestCount.current += 1;
386
+ clearInterval(_interval);
387
+ });
388
+ }
389
+ try {
390
+ const options = {};
391
+ const _sources = uniq(reqsChunk.flatMap((record) => record.sources ?? []));
392
+ if (_sources.length > 0) {
393
+ options.sources = _sources;
394
+ }
395
+ await bulkEnrich(
396
+ reqsChunk.map((record) => record.toSelector()),
397
+ options
398
+ );
399
+ await database.status.findByIds(reqsChunk.map((selector) => selector.id)).update({ $set: { status: "complete" } });
400
+ } catch (e) {
401
+ console.error(e);
402
+ } finally {
403
+ runningRequestCount.current -= 1;
404
+ if (_interval) {
405
+ clearInterval(_interval);
406
+ }
407
+ }
408
+ })
409
+ );
410
+ },
411
+ 200,
412
+ { maxWait: 500, leading: false }
413
+ ),
414
+ [bulkEnrich, chunkSize, database, debugLogging, maxRequestCount]
415
+ );
416
+ useEffect(() => {
417
+ {
418
+ return;
419
+ }
420
+ }, [database, debugLogging]);
421
+ useEffect(() => {
422
+ if (!enabled || !isReady || !(database == null ? void 0 : database.status)) {
423
+ return;
424
+ }
425
+ const observer = database.status.find({
426
+ selector: {
427
+ status: "pending"
428
+ }
429
+ }).$.subscribe(() => enrichQueued());
430
+ return () => {
431
+ try {
432
+ observer == null ? void 0 : observer.unsubscribe();
433
+ } catch (e) {
434
+ console.warn(e);
435
+ }
436
+ };
437
+ }, [enabled, isReady, database, debugLogging, enrichQueued]);
438
+ const queueEnrich = useCallback(
439
+ async (type, value2, classification) => {
440
+ if (!type) {
441
+ throw new Error("Type cannot be null");
442
+ }
443
+ if (!value2) {
444
+ throw new Error("Value cannot be null");
445
+ }
446
+ const query = { type, value: value2, classification: classification ?? defaultClassification };
447
+ let statusRecord = await database.status.findOne({
448
+ selector: query
449
+ }).exec();
450
+ if (!statusRecord) {
451
+ statusRecord = await database.status.queueInsert({
452
+ id: v4(),
453
+ ...query,
454
+ status: "pending"
455
+ });
456
+ }
457
+ return statusRecord;
458
+ },
459
+ [defaultClassification, database]
460
+ );
461
+ const guessType = useCallback(
462
+ (value2) => {
463
+ var _a2, _b;
464
+ if (!value2) {
465
+ return null;
466
+ }
467
+ const types = Object.entries(typesDetection);
468
+ const regularCheck = (_a2 = types.find(([_type, _regexp]) => _regexp.exec(value2))) == null ? void 0 : _a2[0];
469
+ if (regularCheck) {
470
+ return regularCheck;
471
+ }
472
+ const lowercased = value2.toLowerCase();
473
+ const lowercaseCheck = (_b = types.find(([_type, _regexp]) => _regexp.exec(lowercased))) == null ? void 0 : _b[0];
474
+ if (lowercaseCheck) {
475
+ return lowercaseCheck;
476
+ }
477
+ return null;
478
+ },
479
+ [typesDetection]
480
+ );
481
+ const value = useMemo(
482
+ () => {
483
+ var _a2;
484
+ return {
485
+ bulkEnrich,
486
+ enrich,
487
+ enrichFailedEnrichments,
488
+ sources,
489
+ setSources,
490
+ typesDetection,
491
+ availableSources,
492
+ guessType,
493
+ queueEnrich,
494
+ setCustomIconify,
495
+ setReady: setIsReady,
496
+ defaultClassification,
497
+ ready: isReady && !!database && !!((_a2 = clueConfig.config) == null ? void 0 : _a2.c12nDef)
498
+ };
499
+ },
500
+ [
501
+ bulkEnrich,
502
+ enrich,
503
+ enrichFailedEnrichments,
504
+ sources,
505
+ typesDetection,
506
+ availableSources,
507
+ guessType,
508
+ queueEnrich,
509
+ defaultClassification,
510
+ isReady,
511
+ database,
512
+ (_a = clueConfig.config) == null ? void 0 : _a.c12nDef
513
+ ]
514
+ );
515
+ return /* @__PURE__ */ jsx(ClueEnrichContext.Provider, { value, children });
516
+ };
517
+ export {
518
+ ClueEnrichContext as C,
519
+ ClueEnrichProvider as a,
520
+ uniqBy as b,
521
+ uniq as u
522
+ };