Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix docs Decimal widget loading (#3575)

Malay Vasad8ec506ec802
diff --git a/docs/app/layout.tsx b/docs/app/layout.tsx
index 55200bc49..c4a627856 100644
--- a/docs/app/layout.tsx
+++ b/docs/app/layout.tsx
@@ -5,7 +5,6 @@ import './global.css';
import { JetBrains_Mono } from 'next/font/google';
import localFont from 'next/font/local';
import { PostHogProvider } from '@/components/posthog-provider';
-import { DecimalWidget } from '@/components/decimal-widget';
import CustomSearchDialog from '@/components/custom-search-dialog';
import { ScrollReset } from '@/components/scroll-reset';
import { source } from '@/lib/source';
@@ -129,7 +128,6 @@ export default function Layout({ children }: LayoutProps<'/'>) {
{children}
</RootProvider>
</PostHogProvider>
- <DecimalWidget />
</body>
</html>
);
diff --git a/docs/components/ask-ai-button.tsx b/docs/components/ask-ai-button.tsx
index 5bd2fd36a..4c6763849 100644
--- a/docs/components/ask-ai-button.tsx
+++ b/docs/components/ask-ai-button.tsx
@@ -1,30 +1,23 @@
'use client';
-import { useEffect, useState } from 'react';
+import { useEffect, useSyncExternalStore } from 'react';
import { Search, MessageSquare } from 'lucide-react';
import { useSearchContext } from '@fumadocs/ui/contexts/search';
import { useI18n } from '@fumadocs/ui/contexts/i18n';
-import type { DecimalAPI } from './decimal-widget';
+import {
+ closeDecimalWidget,
+ getDecimal,
+ isDecimalWidgetVisible,
+ loadDecimalWidget,
+ openDecimalWidget,
+} from './decimal-widget';
-function getDecimal() {
- return (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
-}
-
-function isWidgetVisible(): boolean {
- const sidebar = document.querySelector('.decimal-widget-sidebar');
- return sidebar?.classList.contains('open') ?? false;
-}
+export async function toggleDecimalWidget() {
+ const decimal = getDecimal() ?? (await loadDecimalWidget());
+ if (!decimal) return;
-export function toggleDecimalWidget() {
- const decimal = getDecimal();
- if (!decimal) {
- setTimeout(() => {
- getDecimal()?.show();
- }, 500);
- return;
- }
- isWidgetVisible() ? decimal.hide() : decimal.show();
+ isDecimalWidgetVisible() ? closeDecimalWidget() : openDecimalWidget(decimal);
}
export function detectMac(): boolean {
@@ -42,17 +35,17 @@ export function detectMac(): boolean {
}
function useIsMac() {
- const [isMac, setIsMac] = useState(true);
- useEffect(() => {
- setIsMac(detectMac());
- }, []);
- return isMac;
+ return useSyncExternalStore(
+ () => () => {},
+ detectMac,
+ () => true,
+ );
}
const handleKeyDown = (e: KeyboardEvent) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'i') {
e.preventDefault();
- toggleDecimalWidget();
+ void toggleDecimalWidget();
}
};
@@ -92,7 +85,9 @@ export function SearchAndAskAI() {
)}
<button
type="button"
- onClick={toggleDecimalWidget}
+ onClick={() => {
+ void toggleDecimalWidget();
+ }}
className="inline-flex items-center gap-2 rounded-lg border border-[var(--composio-orange)]/20 bg-[var(--composio-orange)]/5 p-1.5 ps-2.5 text-sm text-[var(--composio-orange)] transition-colors hover:bg-[var(--composio-orange)]/10 shrink-0"
>
Ask AI
@@ -125,7 +120,9 @@ export function SearchAndAskAIMobile() {
<button
type="button"
aria-label="Ask AI"
- onClick={toggleDecimalWidget}
+ onClick={() => {
+ void toggleDecimalWidget();
+ }}
className="inline-flex items-center justify-center rounded-md p-2 text-sm font-medium transition-colors duration-100 hover:bg-fd-accent hover:text-fd-accent-foreground"
>
<MessageSquare className="size-4.5" />
diff --git a/docs/components/custom-search-dialog.tsx b/docs/components/custom-search-dialog.tsx
index 9f1108ea7..fe72bd5e1 100644
--- a/docs/components/custom-search-dialog.tsx
+++ b/docs/components/custom-search-dialog.tsx
@@ -275,7 +275,7 @@ export default function CustomSearchDialog({
className="inline-flex items-center gap-1.5 text-xs text-[var(--composio-orange)] hover:text-[var(--composio-orange)]/80 transition-colors"
onClick={() => {
props.onOpenChange(false);
- toggleDecimalWidget();
+ void toggleDecimalWidget();
}}
>
<BotMessageSquare className="size-3.5" />
diff --git a/docs/components/decimal-widget.ts b/docs/components/decimal-widget.ts
new file mode 100644
index 000000000..0f8343a4f
--- /dev/null
+++ b/docs/components/decimal-widget.ts
@@ -0,0 +1,204 @@
+'use client';
+
+export type DecimalAPI = {
+ show: () => void;
+ hide: () => void;
+ theme: (config: Record<string, string>) => void;
+};
+
+const DECIMAL_SCRIPT_ID = 'decimal-widget-script';
+const DECIMAL_SRC = 'https://app.getdecimal.ai/widget/v1/widget.js';
+const DECIMAL_WIDGET_ID = 'wgt_Ze0kCx97w7YXIydXpEAbAVWfu7FO6HG1';
+const DECIMAL_MOBILE_CLOSE_ID = 'decimal-mobile-close-button';
+const MOBILE_VIEWPORT_QUERY = '(max-width: 767px)';
+const DECIMAL_PUBLIC_CONFIG =
+ 'eyJhbGciOiJIUzI1NiJ9.eyJ3aWQiOiJ3Z3RfWmUwa0N4OTd3N1lYSXlkWHBFQWJBVldmdTdGTzZIRzEiLCJkb21haW5zIjpbImNvbXBvc2lvLmRldiIsImNvbXBvc2lvLWRlY2ltYWwudmVyY2VsLmFwcCIsImxvY2FsaG9zdDozMDAwIiwiZG9jcy5jb21wb3Npby5kZXYiLCJmdW1hZG9jcy1wc2kudmVyY2VsLmFwcCJdLCJpYXQiOjE3Njk1MDE3NTZ9.j7odPAOmoKSkdkFHQCs7FDpAxHfJuzUOEMb_OuHi81I';
+
+const DARK_THEME = {
+ colorScheme: 'dark',
+ primaryColor: '#1e1d1c',
+ backgroundColor: '#131211',
+ textColor: '#FAFAFA',
+ textColorSecondary: '#FFFFFF',
+ textColorMuted: '#A1A1AA',
+ borderColor: '#27272A',
+};
+
+const LIGHT_THEME = {
+ colorScheme: 'light',
+ primaryColor: '#171414',
+ backgroundColor: '#ffffff',
+ textColor: '#171414',
+ textColorSecondary: '#ffffff',
+ textColorMuted: '#5c5858',
+ borderColor: '#e5e0df',
+};
+
+let loadPromise: Promise<DecimalAPI | undefined> | undefined;
+let themeObserver: MutationObserver | undefined;
+let closeButtonViewportQuery: MediaQueryList | undefined;
+let decimalWidgetRequestedOpen = false;
+
+export function getDecimal() {
+ return (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
+}
+
+export function isDecimalWidgetVisible(): boolean {
+ const sidebar = document.querySelector('.decimal-widget-sidebar');
+ return sidebar?.classList.contains('open') ?? false;
+}
+
+export function applyDecimalTheme() {
+ const decimal = getDecimal();
+ if (!decimal) return;
+
+ const isDark = document.documentElement.classList.contains('dark');
+ decimal.theme(isDark ? DARK_THEME : LIGHT_THEME);
+}
+
+function watchThemeChanges() {
+ if (themeObserver) return;
+
+ applyDecimalTheme();
+ themeObserver = new MutationObserver((mutations) => {
+ for (const mutation of mutations) {
+ if (mutation.attributeName === 'class') {
+ applyDecimalTheme();
+ }
+ }
+ });
+ themeObserver.observe(document.documentElement, { attributes: true });
+}
+
+function waitForDecimal(timeoutMs = 2000): Promise<DecimalAPI | undefined> {
+ const startedAt = Date.now();
+
+ return new Promise((resolve) => {
+ const check = () => {
+ const decimal = getDecimal();
+ if (decimal) {
+ watchThemeChanges();
+ resolve(decimal);
+ return;
+ }
+
+ if (Date.now() - startedAt >= timeoutMs) {
+ resolve(undefined);
+ return;
+ }
+
+ window.setTimeout(check, 50);
+ };
+
+ check();
+ });
+}
+
+function finishLoading(resolve: (decimal: DecimalAPI | undefined) => void) {
+ waitForDecimal().then((decimal) => {
+ if (!decimal) {
+ loadPromise = undefined;
+ }
+ resolve(decimal);
+ });
+}
+
+function isMobileViewport() {
+ return window.matchMedia(MOBILE_VIEWPORT_QUERY).matches;
+}
+
+function syncMobileCloseButtonVisibility() {
+ const button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID);
+ if (!button) return;
+
+ button.style.display = isMobileViewport() && decimalWidgetRequestedOpen ? 'inline-flex' : 'none';
+}
+
+function ensureMobileCloseButton() {
+ let button = document.getElementById(DECIMAL_MOBILE_CLOSE_ID) as HTMLButtonElement | null;
+ if (!button) {
+ button = document.createElement('button');
+ button.id = DECIMAL_MOBILE_CLOSE_ID;
+ button.type = 'button';
+ button.setAttribute('aria-label', 'Close Ask AI');
+ button.innerHTML = '&times;';
+ button.style.cssText = [
+ 'position: fixed',
+ 'top: calc(env(safe-area-inset-top, 0px) + 12px)',
+ 'right: 12px',
+ 'z-index: 2147483647',
+ 'width: 44px',
+ 'height: 44px',
+ 'align-items: center',
+ 'justify-content: center',
+ 'border-radius: 9999px',
+ 'border: 1px solid rgba(23, 20, 20, 0.12)',
+ 'background: rgba(255, 255, 255, 0.96)',
+ 'color: #171414',
+ 'box-shadow: 0 12px 32px rgba(23, 20, 20, 0.18)',
+ 'font-size: 28px',
+ 'font-weight: 400',
+ 'line-height: 1',
+ 'display: none',
+ ].join(';');
+ button.addEventListener('click', closeDecimalWidget);
+ document.body.appendChild(button);
+ }
+
+ if (!closeButtonViewportQuery) {
+ closeButtonViewportQuery = window.matchMedia(MOBILE_VIEWPORT_QUERY);
+ closeButtonViewportQuery.addEventListener('change', syncMobileCloseButtonVisibility);
+ }
+
+ syncMobileCloseButtonVisibility();
+}
+
+export function closeDecimalWidget() {
+ decimalWidgetRequestedOpen = false;
+ getDecimal()?.hide();
+ syncMobileCloseButtonVisibility();
+}
+
+export function openDecimalWidget(decimal: DecimalAPI) {
+ decimalWidgetRequestedOpen = true;
+ decimal.show();
+ ensureMobileCloseButton();
+}
+
+export function loadDecimalWidget(): Promise<DecimalAPI | undefined> {
+ const existingDecimal = getDecimal();
+ if (existingDecimal) {
+ watchThemeChanges();
+ return Promise.resolve(existingDecimal);
+ }
+
+ if (loadPromise) return loadPromise;
+
+ loadPromise = new Promise((resolve) => {
+ const existingScript = document.getElementById(DECIMAL_SCRIPT_ID);
+ if (existingScript) {
+ finishLoading(resolve);
+ return;
+ }
+
+ const script = document.createElement('script');
+ script.id = DECIMAL_SCRIPT_ID;
+ script.src = DECIMAL_SRC;
+ script.async = true;
+ script.dataset.widgetId = DECIMAL_WIDGET_ID;
+ script.dataset.publicConfig = DECIMAL_PUBLIC_CONFIG;
+ script.dataset.displayMode = 'push-sidebar';
+
+ script.onload = () => {
+ finishLoading(resolve);
+ };
+ script.onerror = () => {
+ loadPromise = undefined;
+ resolve(undefined);
+ };
+
+ document.head.appendChild(script);
+ });
+
+ return loadPromise;
+}
diff --git a/docs/components/decimal-widget.tsx b/docs/components/decimal-widget.tsx
deleted file mode 100644
index 34bfa4f0c..000000000
--- a/docs/components/decimal-widget.tsx
+++ /dev/null
@@ -1,70 +0,0 @@
-'use client';
-
-import Script from 'next/script';
-import { useEffect, useState } from 'react';
-
-export type DecimalAPI = {
- show: () => void;
- hide: () => void;
- theme: (config: Record<string, string>) => void;
-};
-
-const DARK_THEME = {
- colorScheme: 'dark',
- primaryColor: '#1e1d1c',
- backgroundColor: '#131211',
- textColor: '#FAFAFA',
- textColorSecondary: '#FFFFFF',
- textColorMuted: '#A1A1AA',
- borderColor: '#27272A',
-};
-
-const LIGHT_THEME = {
- colorScheme: 'light',
- primaryColor: '#171414',
- backgroundColor: '#ffffff',
- textColor: '#171414',
- textColorSecondary: '#ffffff',
- textColorMuted: '#5c5858',
- borderColor: '#e5e0df',
-};
-
-export function DecimalWidget() {
- const [scriptLoaded, setScriptLoaded] = useState(false);
-
- useEffect(() => {
- if (!scriptLoaded) return;
-
- const Decimal = (window as typeof window & { Decimal?: DecimalAPI }).Decimal;
- if (!Decimal) return;
-
- const applyTheme = () => {
- const isDark = document.documentElement.classList.contains('dark');
- Decimal.theme(isDark ? DARK_THEME : LIGHT_THEME);
- };
-
- applyTheme();
-
- const observer = new MutationObserver((mutations) => {
- for (const mutation of mutations) {
- if (mutation.attributeName === 'class') {
- applyTheme();
- }
- }
- });
- observer.observe(document.documentElement, { attributes: true });
-
- return () => observer.disconnect();
- }, [scriptLoaded]);
-
- return (
- <Script
- src="https://app.getdecimal.ai/widget/v1/widget.js"
- data-widget-id="wgt_Ze0kCx97w7YXIydXpEAbAVWfu7FO6HG1"
- data-public-config="eyJhbGciOiJIUzI1NiJ9.eyJ3aWQiOiJ3Z3RfWmUwa0N4OTd3N1lYSXlkWHBFQWJBVldmdTdGTzZIRzEiLCJkb21haW5zIjpbImNvbXBvc2lvLmRldiIsImNvbXBvc2lvLWRlY2ltYWwudmVyY2VsLmFwcCIsImxvY2FsaG9zdDozMDAwIiwiZG9jcy5jb21wb3Npby5kZXYiLCJmdW1hZG9jcy1wc2kudmVyY2VsLmFwcCJdLCJpYXQiOjE3Njk1MDE3NTZ9.j7odPAOmoKSkdkFHQCs7FDpAxHfJuzUOEMb_OuHi81I"
- data-display-mode="push-sidebar"
- strategy="afterInteractive"
- onLoad={() => setScriptLoaded(true)}
- />
- );
-}