1
+'use client';
2
+
3
+export type DecimalAPI = {
4
+ show: () => void;
5
+ hide: () => void;
6
+ theme: (config: Record<string, string>) => void;
7
+};
8
+
9
+const DECIMAL_SCRIPT_ID = 'decimal-widget-script';
10
+const DECIMAL_SRC = 'https://app.getdecimal.ai/widget/v1/widget.js';
11
+const DECIMAL_WIDGET_ID = 'wgt_Ze0kCx97w7YXIydXpEAbAVWfu7FO6HG1';
12
+const DECIMAL_MOBILE_CLOSE_ID = 'decimal-mobile-close-button';
13
+const MOBILE_VIEWPORT_QUERY = '(max-width: 767px)';
14
+const DECIMAL_PUBLIC_CONFIG =
15
+ 'eyJhbGciOiJIUzI1NiJ9.eyJ3aWQiOiJ3Z3RfWmUwa0N4OTd3N1lYSXlkWHBFQWJBVldmdTdGTzZIRzEiLCJkb21haW5zIjpbImNvbXBvc2lvLmRldiIsImNvbXBvc2lvLWRlY2ltYWwudmVyY2VsLmFwcCIsImxvY2FsaG9zdDozMDAwIiwiZG9jcy5jb21wb3Npby5kZXYiLCJmdW1hZG9jcy1wc2kudmVyY2VsLmFwcCJdLCJpYXQiOjE3Njk1MDE3NTZ9.j7odPAOmoKSkdkFHQCs7FDpAxHfJuzUOEMb_OuHi81I';
16
+
17
+const DARK_THEME = {
18
+ colorScheme: 'dark',
19
+ primaryColor: '#1e1d1c',
20
+ backgroundColor: '#131211',
21
+ textColor: '#FAFAFA',
22
+ textColorSecondary: '#FFFFFF',
23
+ textColorMuted: '#A1A1AA',
24
+ borderColor: '#27272A',
25
+};
26
+
27
+const LIGHT_THEME = {
28
+ colorScheme: 'light',
29
+ primaryColor: '#171414',
30
+ backgroundColor: '#ffffff',
31
+ textColor: '#171414',
32
+ textColorSecondary: '#ffffff',
33
+ textColorMuted: '#5c5858',
34
+ borderColor: '#e5e0df',
35
+};
36
+
37
+let loadPromise: Promise<DecimalAPI | undefined> | undefined;
38
+let themeObserver: MutationObserver | undefined;
39
+let closeButtonViewportQuery: MediaQueryList | undefined;
40
+let decimalWidgetRequestedOpen = false;
41
+
42
+export function getDecimal() {
43
+ return (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
44
+}
45
+
46
+export function isDecimalWidgetVisible(): boolean {
47
+ const sidebar = document.querySelector('.decimal-widget-sidebar');
48
+ return sidebar?.classList.contains('open') ?? false;
49
+}
50
+
51
+export function applyDecimalTheme() {
52
+ const decimal = getDecimal();
53
+ if (!decimal) return;
54
+
55
+ const isDark = document.documentElement.classList.contains('dark');
56
+ decimal.theme(isDark ? DARK_THEME : LIGHT_THEME);
57
+}
58
+
59
+function watchThemeChanges() {
60
+ if (themeObserver) return;
61
+
62
+ applyDecimalTheme();
63
+ themeObserver = new MutationObserver((mutations) => {
64
+ for (const mutation of mutations) {
65
+ if (mutation.attributeName === 'class') {
66
+ applyDecimalTheme();
67
+ }
68
+ }
69
+ });
70
+ themeObserver.observe(document.documentElement, { attributes: true });
71
+}
72
+
73
+function waitForDecimal(timeoutMs = 2000): Promise<DecimalAPI | undefined> {
74
+ const startedAt = Date.now();
75
+
76
+ return new Promise((resolve) => {
77
+ const check = () => {
78
+ const decimal = getDecimal();
79
+ if (decimal) {
80
+ watchThemeChanges();
81
+ resolve(decimal);
82
+ return;
83
+ }
84
+
85
+ if (Date.now() - startedAt >= timeoutMs) {
86
+ resolve(undefined);
87
+ return;
88
+ }
89
+
90
+ window.setTimeout(check, 50);
91
+ };
92
+
93
+ check();
94
+ });
95
+}
96
+
97
+function finishLoading(resolve: (decimal: DecimalAPI | undefined) => void) {
98
+ waitForDecimal().then((decimal) => {
99
+ if (!decimal) {
100
+ loadPromise = undefined;
101
+ }
102
+ resolve(decimal);
103
+ });
104
+}
105
+
106
+function isMobileViewport() {
107
+ return window.matchMedia(MOBILE_VIEWPORT_QUERY).matches;
108
+}
109
+
110
+function syncMobileCloseButtonVisibility() {
111
+ const button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID);
112
+ if (!button) return;
113
+
114
+ button.style.display = isMobileViewport() && decimalWidgetRequestedOpen ? 'inline-flex' : 'none';
115
+}
116
+
117
+function ensureMobileCloseButton() {
118
+ let button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID) as HTMLButtonElement | null;
119
+ if (!button) {
120
+ button = document.createElement('button');
121
+ button.id = DECIMAL_MOBILE_CLOSE_ID;
122
+ button.type = 'button';
123
+ button.setAttribute('aria-label', 'Close Ask AI');
124
+ button.innerHTML = '×';
125
+ button.style.cssText = [
126
+ 'position: fixed',
127
+ 'top: calc(env(safe-area-inset-top, 0px) + 12px)',
128
+ 'right: 12px',
129
+ 'z-index: 2147483647',
130
+ 'width: 44px',
131
+ 'height: 44px',
132
+ 'align-items: center',
133
+ 'justify-content: center',
134
+ 'border-radius: 9999px',
135
+ 'border: 1px solid rgba(23, 20, 20, 0.12)',
136
+ 'background: rgba(255, 255, 255, 0.96)',
137
+ 'color: #171414',
138
+ 'box-shadow: 0 12px 32px rgba(23, 20, 20, 0.18)',
139
+ 'font-size: 28px',
140
+ 'font-weight: 400',
141
+ 'line-height: 1',
142
+ 'display: none',
143
+ ].join(';');
144
+ button.addEventListener('click', closeDecimalWidget);
145
+ document.body.appendChild(button);
146
+ }
147
+
148
+ if (!closeButtonViewportQuery) {
149
+ closeButtonViewportQuery = window.matchMedia(MOBILE_VIEWPORT_QUERY);
150
+ closeButtonViewportQuery.addEventListener('change', syncMobileCloseButtonVisibility);
151
+ }
152
+
153
+ syncMobileCloseButtonVisibility();
154
+}
155
+
156
+export function closeDecimalWidget() {
157
+ decimalWidgetRequestedOpen = false;
158
+ getDecimal()?.hide();
159
+ syncMobileCloseButtonVisibility();
160
+}
161
+
162
+export function openDecimalWidget(decimal: DecimalAPI) {
163
+ decimalWidgetRequestedOpen = true;
164
+ decimal.show();
165
+ ensureMobileCloseButton();
166
+}
167
+
168
+export function loadDecimalWidget(): Promise<DecimalAPI | undefined> {
169
+ const existingDecimal = getDecimal();
170
+ if (existingDecimal) {
171
+ watchThemeChanges();
172
+ return Promise.resolve(existingDecimal);
173
+ }
174
+
175
+ if (loadPromise) return loadPromise;
176
+
177
+ loadPromise = new Promise((resolve) => {
178
+ const existingScript = document.getElementById(DECIMAL_SCRIPT_ID);
179
+ if (existingScript) {
180
+ finishLoading(resolve);
181
+ return;
182
+ }
183
+
184
+ const script = document.createElement('script');
185
+ script.id = DECIMAL_SCRIPT_ID;
186
+ script.src = DECIMAL_SRC;
187
+ script.async = true;
188
+ script.dataset.widgetId = DECIMAL_WIDGET_ID;
189
+ script.dataset.publicConfig = DECIMAL_PUBLIC_CONFIG;
190
+ script.dataset.displayMode = 'push-sidebar';
191
+
192
+ script.onload = () => {
193
+ finishLoading(resolve);
194
+ };
195
+ script.onerror = () => {
196
+ loadPromise = undefined;
197
+ resolve(undefined);
198
+ };
199
+
200
+ document.head.appendChild(script);
201
+ });
202
+
203
+ return loadPromise;
204
+}