Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: command events (#39775)

Francesco Sansalvadoreaf43adb9a497
diff --git a/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx b/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
index b74d5b6c45a..c56f34d0fe9 100644
--- a/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
+++ b/apps/docs/components/Navigation/NavigationMenu/TopNavBar.tsx
@@ -9,7 +9,7 @@ import { memo, useState } from 'react'
import { useIsLoggedIn, useIsUserLoading, useUser } from 'common'
import { isFeatureEnabled } from 'common/enabled-features'
import { Button, buttonVariants, cn } from 'ui'
-import { AuthenticatedDropdownMenu, CommandMenuTrigger } from 'ui-patterns'
+import { AuthenticatedDropdownMenu, CommandMenuTriggerInput } from 'ui-patterns'
import { getCustomContent } from '../../../lib/custom-content/getCustomContent'
import GlobalNavigationMenu from './GlobalNavigationMenu'
import useDropdownMenu from './useDropdownMenu'
@@ -43,37 +43,14 @@ const TopNavBar: FC = () => {
</div>
<div className="flex gap-2 items-center">
- <CommandMenuTrigger>
- <button
- className={cn(
- 'group',
- 'flex-grow md:w-44 xl:w-56 h-[30px] rounded-md',
- 'pl-1.5 md:pl-2 pr-1',
- 'flex items-center justify-between',
- 'bg-surface-100/75 text-foreground-lighter border',
- 'hover:bg-opacity-100 hover:border-strong',
- 'focus-visible:!outline-4 focus-visible:outline-offset-1 focus-visible:outline-brand-600',
- 'transition'
- )}
- >
- <div className="flex items-center space-x-2 text-foreground-muted">
- <Search size={18} strokeWidth={2} />
- <p className="flex text-sm pr-2">
- Search
- <span className="hidden xl:inline ml-1"> docs...</span>
- </p>
- </div>
- <div className="hidden md:flex items-center space-x-1">
- <div
- aria-hidden="true"
- className="md:flex items-center justify-center h-full px-1 border rounded bg-surface-300 gap-0.5"
- >
- <Command size={12} strokeWidth={1.5} />
- <span className="text-[12px]">K</span>
- </div>
- </div>
- </button>
- </CommandMenuTrigger>
+ <CommandMenuTriggerInput
+ placeholder={
+ <>
+ Search
+ <span className="hidden xl:inline ml-1"> docs...</span>
+ </>
+ }
+ />
<button
title="Menu dropdown button"
className={cn(
diff --git a/apps/docs/features/app.providers.tsx b/apps/docs/features/app.providers.tsx
index f6c2f3089b3..99e73b93c98 100644
--- a/apps/docs/features/app.providers.tsx
+++ b/apps/docs/features/app.providers.tsx
@@ -1,12 +1,11 @@
-import { type PropsWithChildren } from 'react'
+import type { PropsWithChildren } from 'react'
import { FeatureFlagProvider, IS_PLATFORM, ThemeProvider } from 'common'
import { SonnerToaster, TooltipProvider } from 'ui'
-import { CommandProvider } from 'ui-patterns/CommandMenu'
import SiteLayout from '~/layouts/SiteLayout'
import { API_URL } from '~/lib/constants'
import { AuthContainer } from './auth/auth.client'
-import { DocsCommandMenu } from './command'
+import { DocsCommandMenu, DocsCommandProvider } from './command'
import { QueryClientProvider } from './data/queryClient.client'
import { PageTelemetry } from './telemetry/telemetry.client'
import { ScrollRestoration } from './ui/helpers.scroll.client'
@@ -24,7 +23,7 @@ function GlobalProviders({ children }: PropsWithChildren) {
<ScrollRestoration />
<ThemeProvider defaultTheme="system" enableSystem disableTransitionOnChange>
<TooltipProvider delayDuration={0}>
- <CommandProvider>
+ <DocsCommandProvider>
<div className="flex flex-col">
<SiteLayout>
{children}
@@ -32,7 +31,7 @@ function GlobalProviders({ children }: PropsWithChildren) {
</SiteLayout>
<ThemeSandbox />
</div>
- </CommandProvider>
+ </DocsCommandProvider>
<SonnerToaster position="top-right" />
</TooltipProvider>
</ThemeProvider>
diff --git a/apps/docs/features/command/DocsCommandProvider.tsx b/apps/docs/features/command/DocsCommandProvider.tsx
new file mode 100644
index 00000000000..62e300fdda4
--- /dev/null
+++ b/apps/docs/features/command/DocsCommandProvider.tsx
@@ -0,0 +1,16 @@
+'use client'
+
+import type { PropsWithChildren } from 'react'
+
+import { CommandProvider } from 'ui-patterns/CommandMenu'
+import { useDocsCommandMenuTelemetry } from 'hooks/useDocsCommandMenuTelemetry'
+
+export function DocsCommandProvider({ children }: PropsWithChildren) {
+ const { onTelemetry } = useDocsCommandMenuTelemetry()
+
+ return (
+ <CommandProvider app="docs" onTelemetry={onTelemetry}>
+ {children}
+ </CommandProvider>
+ )
+}
diff --git a/apps/docs/features/command/index.tsx b/apps/docs/features/command/index.tsx
index 198bba2b8ee..99c78222319 100644
--- a/apps/docs/features/command/index.tsx
+++ b/apps/docs/features/command/index.tsx
@@ -3,6 +3,7 @@
import dynamic from 'next/dynamic'
import { useCommandMenuInitiated } from 'ui-patterns/CommandMenu'
+import { DocsCommandProvider } from './DocsCommandProvider'
const LazyCommandMenu = dynamic(() => import('./CommandMenu'), { ssr: false })
@@ -11,4 +12,4 @@ const DocsCommandMenu = () => {
return isInitiated && <LazyCommandMenu />
}
-export { DocsCommandMenu }
+export { DocsCommandMenu, DocsCommandProvider }
diff --git a/apps/docs/hooks/useDocsCommandMenuTelemetry.ts b/apps/docs/hooks/useDocsCommandMenuTelemetry.ts
new file mode 100644
index 00000000000..c2b8371d183
--- /dev/null
+++ b/apps/docs/hooks/useDocsCommandMenuTelemetry.ts
@@ -0,0 +1,28 @@
+'use client'
+
+import { useCallback } from 'react'
+
+import type {
+ CommandMenuOpenedEvent,
+ CommandMenuCommandSelectedEvent,
+ CommandMenuSearchSubmittedEvent,
+} from 'common/telemetry-constants'
+import { useSendTelemetryEvent } from 'lib/telemetry'
+
+export function useDocsCommandMenuTelemetry() {
+ const sendTelemetryEvent = useSendTelemetryEvent()
+
+ const onTelemetry = useCallback(
+ (
+ event:
+ | CommandMenuOpenedEvent
+ | CommandMenuCommandSelectedEvent
+ | CommandMenuSearchSubmittedEvent
+ ) => {
+ sendTelemetryEvent(event)
+ },
+ [sendTelemetryEvent]
+ )
+
+ return { onTelemetry }
+}
diff --git a/apps/studio/components/interfaces/App/CommandMenu/StudioCommandProvider.tsx b/apps/studio/components/interfaces/App/CommandMenu/StudioCommandProvider.tsx
new file mode 100644
index 00000000000..8a948cf692c
--- /dev/null
+++ b/apps/studio/components/interfaces/App/CommandMenu/StudioCommandProvider.tsx
@@ -0,0 +1,24 @@
+import type { PropsWithChildren } from 'react'
+
+import { CommandProvider } from 'ui-patterns/CommandMenu'
+import { useStudioCommandMenuTelemetry } from 'hooks/misc/useStudioCommandMenuTelemetry'
+import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
+import { LOCAL_STORAGE_KEYS } from 'common'
+
+export function StudioCommandProvider({ children }: PropsWithChildren) {
+ const { onTelemetry } = useStudioCommandMenuTelemetry()
+ const [commandMenuHotkeyEnabled] = useLocalStorageQuery<boolean>(
+ LOCAL_STORAGE_KEYS.HOTKEY_COMMAND_MENU,
+ true
+ )
+
+ return (
+ <CommandProvider
+ app="studio"
+ onTelemetry={onTelemetry}
+ openKey={commandMenuHotkeyEnabled ? 'k' : ''}
+ >
+ {children}
+ </CommandProvider>
+ )
+}
diff --git a/apps/studio/components/interfaces/UserDropdown.tsx b/apps/studio/components/interfaces/UserDropdown.tsx
index 1fe53c346dc..02d3d4a0463 100644
--- a/apps/studio/components/interfaces/UserDropdown.tsx
+++ b/apps/studio/components/interfaces/UserDropdown.tsx
@@ -23,7 +23,7 @@ import {
Theme,
singleThemes,
} from 'ui'
-import { useSetCommandMenuOpen } from 'ui-patterns/CommandMenu'
+import { useCommandMenuOpenedTelemetry, useSetCommandMenuOpen } from 'ui-patterns/CommandMenu'
import { useFeaturePreviewModal } from './App/FeaturePreview/FeaturePreviewContext'
export function UserDropdown() {
@@ -35,8 +35,14 @@ export function UserDropdown() {
const signOut = useSignOut()
const setCommandMenuOpen = useSetCommandMenuOpen()
+ const sendTelemetry = useCommandMenuOpenedTelemetry()
const { openFeaturePreviewModal } = useFeaturePreviewModal()
+ const handleCommandMenuOpen = () => {
+ setCommandMenuOpen(true)
+ sendTelemetry()
+ }
+
return (
<DropdownMenu>
<DropdownMenuTrigger asChild className="border flex-shrink-0 px-3">
@@ -97,7 +103,7 @@ export function UserDropdown() {
<FlaskConical size={14} strokeWidth={1.5} className="text-foreground-lighter" />
Feature previews
</DropdownMenuItem>
- <DropdownMenuItem className="flex gap-2" onClick={() => setCommandMenuOpen(true)}>
+ <DropdownMenuItem className="flex gap-2" onClick={handleCommandMenuOpen}>
<Command size={14} strokeWidth={1.5} className="text-foreground-lighter" />
Command menu
</DropdownMenuItem>
diff --git a/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts b/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts
new file mode 100644
index 00000000000..925c9c600a8
--- /dev/null
+++ b/apps/studio/hooks/misc/useStudioCommandMenuTelemetry.ts
@@ -0,0 +1,40 @@
+import { useCallback } from 'react'
+
+import { useParams } from 'common'
+import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
+import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
+import type {
+ CommandMenuOpenedEvent,
+ CommandMenuCommandSelectedEvent,
+ CommandMenuSearchSubmittedEvent,
+} from 'common/telemetry-constants'
+
+export function useStudioCommandMenuTelemetry() {
+ const { ref: projectRef } = useParams()
+ const { data: organization } = useSelectedOrganizationQuery()
+ const { mutate: sendEvent } = useSendEventMutation()
+
+ const onTelemetry = useCallback(
+ (
+ event:
+ | CommandMenuOpenedEvent
+ | CommandMenuCommandSelectedEvent
+ | CommandMenuSearchSubmittedEvent
+ ) => {
+ // Add studio-specific groups (project and organization)
+ const eventWithGroups = {
+ ...event,
+ groups: {
+ ...event.groups,
+ ...(projectRef && { project: projectRef }),
+ ...(organization?.slug && { organization: organization.slug }),
+ },
+ }
+
+ sendEvent(eventWithGroups)
+ },
+ [projectRef, organization?.slug, sendEvent]
+ )
+
+ return { onTelemetry }
+}
diff --git a/apps/studio/pages/_app.tsx b/apps/studio/pages/_app.tsx
index fb4ae48c14c..f6816f5ed79 100644
--- a/apps/studio/pages/_app.tsx
+++ b/apps/studio/pages/_app.tsx
@@ -30,13 +30,12 @@ import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import Head from 'next/head'
import { NuqsAdapter } from 'nuqs/adapters/next/pages'
-import { ErrorInfo, PropsWithChildren, useCallback } from 'react'
+import { ErrorInfo, useCallback } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import {
FeatureFlagProvider,
getFlags,
- LOCAL_STORAGE_KEYS,
TelemetryTagManager,
ThemeProvider,
useThemeSandbox,
@@ -52,14 +51,13 @@ import { GlobalErrorBoundaryState } from 'components/ui/ErrorBoundary/GlobalErro
import { useRootQueryClient } from 'data/query-client'
import { customFont, sourceCodePro } from 'fonts'
import { useCustomContent } from 'hooks/custom-content/useCustomContent'
-import { useLocalStorageQuery } from 'hooks/misc/useLocalStorage'
import { AuthProvider } from 'lib/auth'
import { API_URL, BASE_PATH, IS_PLATFORM, useDefaultProvider } from 'lib/constants'
import { ProfileProvider } from 'lib/profile'
import { Telemetry } from 'lib/telemetry'
import { AppPropsWithLayout } from 'types'
import { SonnerToaster, TooltipProvider } from 'ui'
-import { CommandProvider } from 'ui-patterns/CommandMenu'
+import { StudioCommandProvider as CommandProvider } from 'components/interfaces/App/CommandMenu/StudioCommandProvider'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
@@ -80,15 +78,6 @@ loader.config({
},
})
-const CommandProviderWithPreferences = ({ children }: PropsWithChildren) => {
- const [commandMenuHotkeyEnabled] = useLocalStorageQuery<boolean>(
- LOCAL_STORAGE_KEYS.HOTKEY_COMMAND_MENU,
- true
- )
-
- return <CommandProvider openKey={commandMenuHotkeyEnabled ? 'k' : ''}>{children}</CommandProvider>
-}
-
// [Joshen TODO] Once we settle on the new nav layout - we'll need a lot of clean up in terms of our layout components
// a lot of them are unnecessary and introduce way too many cluttered CSS especially with the height styles that make
// debugging way too difficult. Ideal scenario is we just have one AppLayout to control the height and scroll areas of
@@ -161,7 +150,7 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
disableTransitionOnChange
>
<AppBannerContextProvider>
- <CommandProviderWithPreferences>
+ <CommandProvider>
<FeaturePreviewContextProvider>
{getLayout(<Component {...pageProps} />)}
<StudioCommandMenu />
@@ -169,7 +158,7 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {
</FeaturePreviewContextProvider>
<SonnerToaster position="top-right" />
<MonacoThemeProvider />
- </CommandProviderWithPreferences>
+ </CommandProvider>
</AppBannerContextProvider>
</ThemeProvider>
</RouteValidationWrapper>
diff --git a/apps/www/hooks/useWwwCommandMenuTelemetry.ts b/apps/www/hooks/useWwwCommandMenuTelemetry.ts
new file mode 100644
index 00000000000..cf7a6037eff
--- /dev/null
+++ b/apps/www/hooks/useWwwCommandMenuTelemetry.ts
@@ -0,0 +1,26 @@
+import { useCallback } from 'react'
+
+import type {
+ CommandMenuOpenedEvent,
+ CommandMenuCommandSelectedEvent,
+ CommandMenuSearchSubmittedEvent,
+} from 'common/telemetry-constants'
+import { useSendTelemetryEvent } from 'lib/telemetry'
+
+export function useWwwCommandMenuTelemetry() {
+ const sendTelemetryEvent = useSendTelemetryEvent()
+
+ const onTelemetry = useCallback(
+ (
+ event:
+ | CommandMenuOpenedEvent
+ | CommandMenuCommandSelectedEvent
+ | CommandMenuSearchSubmittedEvent
+ ) => {
+ sendTelemetryEvent(event)
+ },
+ [sendTelemetryEvent]
+ )
+
+ return { onTelemetry }
+}
diff --git a/apps/www/lib/telemetry.ts b/apps/www/lib/telemetry.ts
index 0e781bd5eed..686ed8482fe 100644
--- a/apps/www/lib/telemetry.ts
+++ b/apps/www/lib/telemetry.ts
@@ -4,16 +4,20 @@ import { sendTelemetryEvent } from 'common'
import type { TelemetryEvent } from 'common/telemetry-constants'
import { API_URL } from 'lib/constants'
import { usePathname, useSearchParams } from 'next/navigation'
+import { useCallback } from 'react'
export function useSendTelemetryEvent() {
const pathname = usePathname()
const searchParams = useSearchParams()
- return (event: TelemetryEvent) => {
- const url = new URL(API_URL ?? 'http://localhost:3000')
- url.pathname = pathname ?? ''
- url.search = searchParams?.toString() ?? ''
+ return useCallback(
+ (event: TelemetryEvent) => {
+ const url = new URL(API_URL ?? 'http://localhost:3000')
+ url.pathname = pathname ?? ''
+ url.search = searchParams?.toString() ?? ''
- return sendTelemetryEvent(API_URL, event, url.toString())
- }
+ return sendTelemetryEvent(API_URL, event, url.toString())
+ },
+ [pathname, searchParams]
+ )
}
diff --git a/apps/www/pages/_app.tsx b/apps/www/pages/_app.tsx
index f839f5cbd40..55a845f09b0 100644
--- a/apps/www/pages/_app.tsx
+++ b/apps/www/pages/_app.tsx
@@ -12,7 +12,7 @@ import {
useThemeSandbox,
} from 'common'
import { DefaultSeo } from 'next-seo'
-import { AppProps } from 'next/app'
+import type { AppProps } from 'next/app'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { SonnerToaster, themes, TooltipProvider } from 'ui'
@@ -26,10 +26,12 @@ import MetaFaviconsPagesRouter, {
import { WwwCommandMenu } from '~/components/CommandMenu'
import { API_URL, APP_NAME, DEFAULT_META_DESCRIPTION } from '~/lib/constants'
import useDarkLaunchWeeks from '../hooks/useDarkLaunchWeeks'
+import { useWwwCommandMenuTelemetry } from '../hooks/useWwwCommandMenuTelemetry'
export default function App({ Component, pageProps }: AppProps) {
const router = useRouter()
const { hasAcceptedConsent } = useConsentToast()
+ const { onTelemetry } = useWwwCommandMenuTelemetry()
useThemeSandbox()
@@ -43,7 +45,7 @@ export default function App({ Component, pageProps }: AppProps) {
let faviconRoute = DEFAULT_FAVICON_ROUTE
let themeColor = DEFAULT_FAVICON_THEME_COLOR
- if (router.asPath && router.asPath.includes('/launch-week/x')) {
+ if (router.asPath?.includes('/launch-week/x')) {
applicationName = 'Supabase LWX'
faviconRoute = 'images/launchweek/lwx/favicon'
themeColor = 'FFFFFF'
@@ -95,7 +97,7 @@ export default function App({ Component, pageProps }: AppProps) {
forcedTheme={forceDarkMode ? 'dark' : undefined}
>
<TooltipProvider delayDuration={0}>
- <CommandProvider>
+ <CommandProvider app="www" onTelemetry={onTelemetry}>
<SonnerToaster position="top-right" />
<Component {...pageProps} />
<WwwCommandMenu />
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index 5d699a3d4c8..0d370b71ea4 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -1949,6 +1949,78 @@ export interface AuthUsersSearchSubmittedEvent {
groups: TelemetryGroups
}
+/**
+ * User opened the command menu.
+ *
+ * @group Events
+ * @source ui-patterns
+ * @page any
+ */
+export interface CommandMenuOpenedEvent {
+ action: 'command_menu_opened'
+ properties: {
+ /**
+ * The trigger that opened the command menu
+ */
+ trigger_type: 'keyboard_shortcut' | 'search_input'
+ /**
+ * The location where the command menu was opened
+ */
+ trigger_location?: string
+ /**
+ * In which app the command input was typed
+ */
+ app: 'studio' | 'docs' | 'www'
+ }
+ groups: Partial<TelemetryGroups>
+}
+
+/**
+ * User typed a search term in the command menu input.
+ *
+ * @group Events
+ * @source ui-patterns
+ * @page any
+ */
+export interface CommandMenuSearchSubmittedEvent {
+ action: 'command_menu_search_submitted'
+ properties: {
+ /**
+ * Search term typed into the command menu input
+ */
+ value: string
+ /**
+ * In which app the command input was typed
+ */
+ app: 'studio' | 'docs' | 'www'
+ }
+ groups: Partial<TelemetryGroups>
+}
+
+/**
+ * User selected a command from the command menu.
+ *
+ * @group Events
+ * @source ui-patterns
+ * @page any
+ */
+export interface CommandMenuCommandSelectedEvent {
+ action: 'command_menu_command_selected'
+ properties: {
+ /**
+ * The selected command
+ */
+ command_name: string
+ command_value?: string
+ command_type: 'action' | 'route'
+ /**
+ * In which app the command input was typed
+ */
+ app: 'studio' | 'docs' | 'www'
+ }
+ groups: Partial<TelemetryGroups>
+}
+
/**
* @hidden
*/
@@ -2063,3 +2135,6 @@ export type TelemetryEvent =
| TableDataAddedEvent
| TableRLSEnabledEvent
| AuthUsersSearchSubmittedEvent
+ | CommandMenuOpenedEvent
+ | CommandMenuSearchSubmittedEvent
+ | CommandMenuCommandSelectedEvent
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandInput.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandInput.tsx
index a2386f484af..afa27b8361c 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandInput.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandInput.tsx
@@ -1,11 +1,15 @@
'use client'
-import React, { forwardRef, useEffect, useRef, useState } from 'react'
+import { forwardRef, useCallback, useEffect, useRef, useState } from 'react'
+import type React from 'react'
-import { useBreakpoint } from 'common'
+import { useBreakpoint, useDebounce } from 'common'
import { CommandInput_Shadcn_, cn } from 'ui'
import { useQuery, useSetQuery } from './hooks/queryHooks'
+import { useCommandMenuTelemetryContext } from './hooks/useCommandMenuTelemetryContext'
+
+const INPUT_TYPED_EVENT_DEBOUNCE_TIME = 2000 // 2s
function useFocusInputOnWiderScreens(ref: React.ForwardedRef<HTMLInputElement>) {
const isBelowSm = useBreakpoint('sm')
@@ -50,8 +54,56 @@ const CommandInput = forwardRef<
const [inputValue, setInputValue] = useState(query)
useEffect(() => {
setInputValue(query)
+ previousValueRef.current = query
}, [query])
+ // Get telemetry context
+ const telemetryContext = useCommandMenuTelemetryContext()
+ const previousValueRef = useRef<string>(inputValue)
+
+ const inputTelemetryEvent = useCallback(
+ (value: string) => {
+ if (telemetryContext?.onTelemetry) {
+ const event = {
+ action: 'command_menu_search_submitted' as const,
+ properties: {
+ value: value,
+ app: telemetryContext.app,
+ },
+ groups: {},
+ }
+ telemetryContext.onTelemetry(event)
+ }
+ },
+ [telemetryContext]
+ )
+
+ const debouncedTelemetry = useDebounce(
+ useCallback(() => {
+ inputTelemetryEvent(inputValue)
+ previousValueRef.current = inputValue
+ }, [inputTelemetryEvent, inputValue]),
+ INPUT_TYPED_EVENT_DEBOUNCE_TIME
+ )
+
+ const handleValueChange = useCallback(
+ (value: string) => {
+ setInputValue(value)
+
+ // Only trigger telemetry if the user is adding characters (not removing with backspace)
+ const isAddingCharacters = value.length > previousValueRef.current.length
+
+ if (!isAddingCharacters) {
+ previousValueRef.current = value
+ return
+ }
+
+ // Trigger debounced telemetry
+ debouncedTelemetry()
+ },
+ [debouncedTelemetry]
+ )
+
// To handle CJK input
const [imeComposing, setImeComposing] = useState(false)
useEffect(() => {
@@ -67,8 +119,8 @@ const CommandInput = forwardRef<
autoFocus={false}
ref={inputRef}
value={inputValue}
- onValueChange={setInputValue}
- placeholder="Type a command or search..."
+ onValueChange={handleValueChange}
+ placeholder="Run a command or search..."
onCompositionStart={() => setImeComposing(true)}
onCompositionEnd={() => setImeComposing(false)}
className={cn(
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
index 205d8f1f6f8..1c1238310ef 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandMenu.tsx
@@ -1,6 +1,6 @@
'use client'
-import { AlertTriangle, ArrowLeft } from 'lucide-react'
+import { AlertTriangle, ArrowLeft, Command, Search } from 'lucide-react'
import type { HTMLAttributes, MouseEvent, PropsWithChildren, ReactElement, ReactNode } from 'react'
import { Children, cloneElement, forwardRef, isValidElement, useEffect, useMemo } from 'react'
import { ErrorBoundary } from 'react-error-boundary'
@@ -11,6 +11,7 @@ import { Button, Command_Shadcn_, Dialog, DialogContent, cn } from 'ui'
import { useCurrentPage, usePageComponent, usePopPage } from './hooks/pagesHooks'
import { useQuery, useSetQuery } from './hooks/queryHooks'
+import { useCommandMenuTelemetryContext } from './hooks/useCommandMenuTelemetryContext'
import {
useCommandMenuSize,
useCommandMenuOpen,
@@ -26,6 +27,7 @@ function Breadcrumb({ className }: { className?: string }) {
return (
<button
+ type="button"
className={cn(
'p-2 bg-overlay flex items-center gap-2 text-xs text-foreground-muted',
className
@@ -108,6 +110,7 @@ function useTouchGestures({ toggleOpen }: { toggleOpen: () => void }) {
function CommandMenuTrigger({ children }: PropsWithChildren) {
const open = useCommandMenuOpen()
const setOpen = useSetCommandMenuOpen()
+ const telemetryContext = useCommandMenuTelemetryContext()
const childFromProps = Children.only(children) as ReactElement<
{
@@ -120,6 +123,20 @@ function CommandMenuTrigger({ children }: PropsWithChildren) {
const handleOpen = () => {
setOpen(!open)
childFromProps.props.onOpen?.(!open)
+
+ // Send telemetry when opening via click
+ if (!open && telemetryContext?.onTelemetry) {
+ const event = {
+ action: 'command_menu_opened' as const,
+ properties: {
+ trigger_type: 'search_input' as const,
+ app: telemetryContext.app,
+ },
+ groups: {},
+ }
+
+ telemetryContext.onTelemetry(event)
+ }
}
const childWithClickHandler = cloneElement(childFromProps, {
@@ -143,6 +160,47 @@ function CommandMenuTrigger({ children }: PropsWithChildren) {
return childWithClickHandler
}
+function CommandMenuTriggerInput({
+ className,
+ placeholder = 'Search...',
+}: {
+ className?: string
+ placeholder?: string | React.ReactNode
+}) {
+ return (
+ <CommandMenuTrigger>
+ <button
+ type="button"
+ className={cn(
+ 'group',
+ 'flex-grow md:min-w-44 xl:min-w-56 h-[30px] rounded-md',
+ 'pl-1.5 md:pl-2 pr-1',
+ 'flex items-center justify-between',
+ 'bg-surface-100/75 text-foreground-lighter border',
+ 'hover:bg-opacity-100 hover:border-strong',
+ 'focus-visible:!outline-4 focus-visible:outline-offset-1 focus-visible:outline-brand-600',
+ 'transition',
+ className
+ )}
+ >
+ <div className="flex items-center space-x-2 text-foreground-muted">
+ <Search size={18} strokeWidth={2} />
+ <p className="flex text-sm pr-2">{placeholder}</p>
+ </div>
+ <div className="command-shortcut hidden md:flex items-center space-x-1">
+ <div
+ aria-hidden="true"
+ className="md:flex items-center justify-center h-full px-1 border rounded bg-surface-300 gap-0.5"
+ >
+ <Command size={12} strokeWidth={1.5} />
+ <span className="text-[12px]">K</span>
+ </div>
+ </div>
+ </button>
+ </CommandMenuTrigger>
+ )
+}
+
interface CommandMenuProps extends PropsWithChildren {
trigger?: ReactNode
}
@@ -202,4 +260,4 @@ function CommandMenu({ children, trigger }: CommandMenuProps) {
)
}
-export { Breadcrumb, CommandMenu, CommandMenuTrigger, CommandWrapper }
+export { Breadcrumb, CommandMenu, CommandMenuTrigger, CommandMenuTriggerInput, CommandWrapper }
diff --git a/packages/ui-patterns/src/CommandMenu/api/CommandProvider.tsx b/packages/ui-patterns/src/CommandMenu/api/CommandProvider.tsx
index 8cb7079a737..1bdd0e3041b 100644
--- a/packages/ui-patterns/src/CommandMenu/api/CommandProvider.tsx
+++ b/packages/ui-patterns/src/CommandMenu/api/CommandProvider.tsx
@@ -11,7 +11,12 @@ import { initPagesState } from '../internal/state/pagesState'
import { initQueryState } from '../internal/state/queryState'
import { initViewState } from '../internal/state/viewState'
import { CrossCompatRouterContext } from './hooks/useCrossCompatRouter'
-import { useSetCommandMenuOpen, useToggleCommandMenu } from './hooks/viewHooks'
+import {
+ useCommandMenuTelemetry,
+ type CommandMenuTelemetryCallback,
+} from './hooks/useCommandMenuTelemetry'
+import { CommandMenuTelemetryContext } from './hooks/useCommandMenuTelemetryContext'
+import { useCommandMenuOpen, useSetCommandMenuOpen, useToggleCommandMenu } from './hooks/viewHooks'
const CommandProviderInternal = ({ children }: PropsWithChildren) => {
const combinedState = useConstant(() => ({
@@ -25,8 +30,21 @@ const CommandProviderInternal = ({ children }: PropsWithChildren) => {
}
// This is a component not a hook so it can access the wrapping context.
-const CommandShortcut = ({ openKey }: { openKey: string }) => {
+const CommandShortcut = ({
+ openKey,
+ app,
+ onTelemetry,
+}: {
+ openKey: string
+ app?: 'studio' | 'docs' | 'www'
+ onTelemetry?: CommandMenuTelemetryCallback
+}) => {
const toggleOpen = useToggleCommandMenu()
+ const isOpen = useCommandMenuOpen()
+ const { sendTelemetry } = useCommandMenuTelemetry({
+ app: app ?? 'studio',
+ onTelemetry,
+ })
useEffect(() => {
if (openKey === '') return
@@ -37,13 +55,14 @@ const CommandShortcut = ({ openKey }: { openKey: string }) => {
if (evt.key === openKey && usesPrimaryModifier && !otherModifiersActive) {
evt.preventDefault()
toggleOpen()
+ !isOpen && sendTelemetry('keyboard_shortcut')
}
}
document.addEventListener('keydown', handleKeydown)
return () => document.removeEventListener('keydown', handleKeydown)
- }, [openKey, toggleOpen])
+ }, [isOpen, openKey, sendTelemetry, toggleOpen])
return null
}
@@ -85,12 +104,22 @@ interface CommandProviderProps extends PropsWithChildren {
* Defaults to `k`. Pass an empty string to disable the keyboard shortcut.
*/
openKey?: string
+ /**
+ * The app where the command menu is being used
+ */
+ app?: 'studio' | 'docs' | 'www'
+ /**
+ * Optional callback to send telemetry events
+ */
+ onTelemetry?: CommandMenuTelemetryCallback
}
-const CommandProvider = ({ children, openKey }: CommandProviderProps) => (
+const CommandProvider = ({ children, openKey, app, onTelemetry }: CommandProviderProps) => (
<CommandProviderInternal>
- <CommandShortcut openKey={openKey ?? 'k'} />
- <CloseOnNavigation>{children}</CloseOnNavigation>
+ <CommandMenuTelemetryContext.Provider value={{ app: app ?? 'studio', onTelemetry }}>
+ <CommandShortcut openKey={openKey ?? 'k'} app={app} onTelemetry={onTelemetry} />
+ <CloseOnNavigation>{children}</CloseOnNavigation>
+ </CommandMenuTelemetryContext.Provider>
</CommandProviderInternal>
)
diff --git a/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts
new file mode 100644
index 00000000000..1ec7f51d8a7
--- /dev/null
+++ b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetry.ts
@@ -0,0 +1,78 @@
+'use client'
+
+import { useCallback } from 'react'
+import { useCommandMenuTelemetryContext } from './useCommandMenuTelemetryContext'
+import { useCommandMenuOpen } from './viewHooks'
+
+import type {
+ CommandMenuOpenedEvent,
+ CommandMenuCommandSelectedEvent,
+ CommandMenuSearchSubmittedEvent,
+} from 'common/telemetry-constants'
+
+export type CommandMenuTelemetryCallback = (
+ event: CommandMenuOpenedEvent | CommandMenuCommandSelectedEvent | CommandMenuSearchSubmittedEvent
+) => void
+
+export interface UseCommandMenuTelemetryOptions {
+ /**
+ * The app where the command menu is being used
+ */
+ app: 'studio' | 'docs' | 'www'
+ /**
+ * Optional callback to send telemetry events
+ */
+ onTelemetry?: CommandMenuTelemetryCallback
+}
+
+export function useCommandMenuTelemetry({ app, onTelemetry }: UseCommandMenuTelemetryOptions) {
+ const sendTelemetry = useCallback(
+ (
+ triggerType: 'keyboard_shortcut' | 'search_input' = 'search_input',
+ groups: Partial<CommandMenuOpenedEvent['groups']> = {},
+ triggerLocation?: string
+ ) => {
+ if (!onTelemetry) return
+
+ const event: CommandMenuOpenedEvent = {
+ action: 'command_menu_opened',
+ properties: {
+ trigger_type: triggerType,
+ trigger_location: triggerLocation,
+ app,
+ },
+ groups: groups as CommandMenuOpenedEvent['groups'],
+ }
+
+ onTelemetry(event)
+ },
+ [app, onTelemetry]
+ )
+
+ return { sendTelemetry }
+}
+
+export const useCommandMenuOpenedTelemetry: (
+ trigger?: 'keyboard_shortcut' | 'search_input'
+) => () => void = (trigger = 'search_input') => {
+ const telemetryContext = useCommandMenuTelemetryContext()
+ const open = useCommandMenuOpen()
+
+ const sendTelemetry = useCallback(() => {
+ if (!open && telemetryContext?.onTelemetry) {
+ const event = {
+ action: 'command_menu_opened' as const,
+ properties: {
+ trigger_type: trigger,
+ location: 'user_dropdown_menu',
+ app: telemetryContext.app,
+ },
+ groups: {},
+ }
+
+ telemetryContext.onTelemetry(event)
+ }
+ }, [open, trigger, telemetryContext])
+
+ return sendTelemetry
+}
diff --git a/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetryContext.tsx b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetryContext.tsx
new file mode 100644
index 00000000000..bf39002f05d
--- /dev/null
+++ b/packages/ui-patterns/src/CommandMenu/api/hooks/useCommandMenuTelemetryContext.tsx
@@ -0,0 +1,18 @@
+'use client'
+
+import { createContext, useContext } from 'react'
+import type { CommandMenuTelemetryCallback } from './useCommandMenuTelemetry'
+
+interface CommandMenuTelemetryContextValue {
+ app: 'studio' | 'docs' | 'www'
+ onTelemetry?: CommandMenuTelemetryCallback
+}
+
+const CommandMenuTelemetryContext = createContext<CommandMenuTelemetryContextValue | null>(null)
+
+export function useCommandMenuTelemetryContext() {
+ const context = useContext(CommandMenuTelemetryContext)
+ return context
+}
+
+export { CommandMenuTelemetryContext }
diff --git a/packages/ui-patterns/src/CommandMenu/index.tsx b/packages/ui-patterns/src/CommandMenu/index.tsx
index 82a0d154f22..e41f375511d 100644
--- a/packages/ui-patterns/src/CommandMenu/index.tsx
+++ b/packages/ui-patterns/src/CommandMenu/index.tsx
@@ -2,7 +2,13 @@ export * from './api/Badges'
export { CommandHeader } from './api/CommandHeader'
export { CommandInput } from './api/CommandInput'
export { CommandList } from './api/CommandList'
-export { Breadcrumb, CommandMenu, CommandMenuTrigger, CommandWrapper } from './api/CommandMenu'
+export {
+ Breadcrumb,
+ CommandMenu,
+ CommandMenuTrigger,
+ CommandMenuTriggerInput,
+ CommandWrapper,
+} from './api/CommandMenu'
export { CommandProvider } from './api/CommandProvider'
export { TextHighlighter, TextHighlighterBase } from './api/TextHighlighter'
export * from './api/hooks/commandsHooks'
@@ -10,6 +16,10 @@ export * from './api/hooks/pagesHooks'
export * from './api/hooks/queryHooks'
export { useCommandFilterState } from './api/hooks/useCommandFilterState'
export { useCrossCompatRouter } from './api/hooks/useCrossCompatRouter'
+export {
+ useCommandMenuOpenedTelemetry,
+ useCommandMenuTelemetry,
+} from './api/hooks/useCommandMenuTelemetry'
export { useHistoryKeys } from './api/hooks/useHistoryKeys'
export * from './api/hooks/viewHooks'
export * from './api/utils'
diff --git a/packages/ui-patterns/src/CommandMenu/internal/Command.tsx b/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
index e07c1a287ca..9fa962fa79c 100644
--- a/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
+++ b/packages/ui-patterns/src/CommandMenu/internal/Command.tsx
@@ -3,8 +3,9 @@
import { type PropsWithChildren, forwardRef } from 'react'
import { CommandItem_Shadcn_, cn } from 'ui'
import { useCrossCompatRouter } from '../api/hooks/useCrossCompatRouter'
+import { useCommandMenuTelemetryContext } from '../api/hooks/useCommandMenuTelemetryContext'
import { useSetCommandMenuOpen } from '../api/hooks/viewHooks'
-import { type ICommand, type IActionCommand, type IRouteCommand } from './types'
+import type { ICommand, IActionCommand, IRouteCommand } from './types'
const isActionCommand = (command: ICommand): command is IActionCommand => 'action' in command
const isRouteCommand = (command: ICommand): command is IRouteCommand => 'route' in command
@@ -52,23 +53,44 @@ const CommandItem = forwardRef<
>(({ children, className, command: _command, ...props }, ref) => {
const router = useCrossCompatRouter()
const setIsOpen = useSetCommandMenuOpen()
+ const telemetryContext = useCommandMenuTelemetryContext()
const command = _command as ICommand // strip the readonly applied from the proxy
+ const handleCommandSelect = () => {
+ // Send telemetry event
+ if (telemetryContext?.onTelemetry) {
+ const event = {
+ action: 'command_menu_command_selected' as const,
+ properties: {
+ command_name: command.name,
+ command_value: command.value,
+ command_type: isActionCommand(command) ? ('action' as const) : ('route' as const),
+ app: telemetryContext.app,
+ },
+ groups: {},
+ }
+
+ telemetryContext.onTelemetry(event)
+ }
+
+ // Execute the original command logic
+ if (isActionCommand(command)) {
+ command.action()
+ } else if (isRouteCommand(command)) {
+ if (command.route.startsWith('http')) {
+ setIsOpen(false)
+ window.open(command.route, '_blank', 'noreferrer,noopener')
+ } else {
+ router.push(command.route)
+ }
+ }
+ }
+
return (
<CommandItem_Shadcn_
ref={ref}
- onSelect={
- isActionCommand(command)
- ? command.action
- : isRouteCommand(command)
- ? () => {
- command.route.startsWith('http')
- ? (setIsOpen(false), window.open(command.route, '_blank', 'noreferrer,noopener'))
- : router.push(command.route)
- }
- : () => {}
- }
+ onSelect={handleCommandSelect}
value={command.value ?? command.name}
forceMount={command.forceMount}
className={cn(
diff --git a/packages/ui/src/components/shadcn/ui/command.tsx b/packages/ui/src/components/shadcn/ui/command.tsx
index 2b2b82b8951..84cb0361bab 100644
--- a/packages/ui/src/components/shadcn/ui/command.tsx
+++ b/packages/ui/src/components/shadcn/ui/command.tsx
@@ -62,7 +62,7 @@ const CommandInput = React.forwardRef<
<CommandPrimitive.Input
ref={ref}
className={cn(
- 'flex h-9 w-full rounded-md bg-transparent py-3 md:text-xs text outline-none placeholder:text-muted disabled:cursor-not-allowed disabled:opacity-50 border-none focus:ring-0',
+ 'flex h-9 w-full rounded-md bg-transparent py-3 md:text-sm text outline-none placeholder:text-muted disabled:cursor-not-allowed disabled:opacity-50 border-none focus:ring-0',
className
)}
{...props}