Line data Source code
1 : import { debounce } from 'es-toolkit/compat'
2 : import { onScopeDispose, watch } from 'vue'
3 : import type { Ref } from 'vue'
4 :
5 : import { useTelemetry } from '@comfyorg/telemetry'
6 : import type { SearchSurface } from '@comfyorg/telemetry/types'
7 :
8 2355 : const DEBOUNCE_MS = 500
9 2355 : const MAX_QUERY_CHARS = 100
10 :
11 : /**
12 : * Fires `app:search_query` for the given surface, debounced 500ms.
13 : * Empty queries are skipped. `results` is read at fire time and only its
14 : * `.length` is observed, so callers can pass any reactive array (filtered
15 : * suggestions, displayed results, etc.). The pending debounced call is
16 : * cancelled on scope dispose so a user who types-and-closes within the
17 : * window doesn't emit a stale event.
18 : *
19 : * The captured `query` is truncated to MAX_QUERY_CHARS to cap PII
20 : * exposure (mirrors GTM provider's sanitizeProperties cap). `query_length`
21 : * remains the full pre-truncation length so we keep the distributional
22 : * signal.
23 : */
24 442 : export function useSearchQueryTracking(
25 : surface: SearchSurface,
26 : query: Ref<string>,
27 : results: Ref<{ length: number }>
28 : ): void {
29 442 : const fire = debounce((value: string) => {
30 10 : const trimmed = value.trim()
31 10 : if (!trimmed) return
32 10 : const count = results.value.length
33 10 : useTelemetry()?.trackSearchQuery({
34 : surface,
35 : query: trimmed.slice(0, MAX_QUERY_CHARS),
36 : query_length: trimmed.length,
37 : result_count: count,
38 : has_results: count > 0
39 : })
40 : }, DEBOUNCE_MS)
41 :
42 442 : watch(query, fire)
43 442 : onScopeDispose(() => fire.cancel())
44 : }
|