createMcpCopyHandler.ts76 lines · main
| 1 | import type { McpClient, McpOnCopyCallback } from '../types' |
| 2 | |
| 3 | export type McpCopyType = McpOnCopyCallback |
| 4 | |
| 5 | export interface McpCopyHandlerParams { |
| 6 | selectedClient: McpClient | null |
| 7 | source: 'studio' | 'docs' |
| 8 | onTrack: (event: { |
| 9 | action: 'connection_string_copied' |
| 10 | properties: { |
| 11 | connectionTab: 'MCP' |
| 12 | selectedItem?: string |
| 13 | connectionType: string |
| 14 | source: 'studio' | 'docs' |
| 15 | } |
| 16 | groups?: { |
| 17 | project?: string |
| 18 | organization?: string |
| 19 | [key: string]: string | undefined |
| 20 | } |
| 21 | }) => void |
| 22 | projectRef?: string |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Creates a copy handler for MCP configuration copying events. |
| 27 | * Centralizes the telemetry logic for tracking MCP URL, config, and command copies. |
| 28 | * |
| 29 | * @param params - Configuration for the copy handler |
| 30 | * @returns A function that handles copy events and sends appropriate telemetry |
| 31 | * |
| 32 | * @example |
| 33 | * ```ts |
| 34 | * const handleCopy = createMcpCopyHandler({ |
| 35 | * selectedClient, |
| 36 | * source: 'studio', |
| 37 | * onTrack: track, |
| 38 | * projectRef: project?.ref |
| 39 | * }) |
| 40 | * |
| 41 | * // Usage in component |
| 42 | * handleCopy('config') // Tracks copying of config file |
| 43 | * handleCopy('command') // Tracks copying of command line |
| 44 | * handleCopy('url') // Tracks copying of MCP URL |
| 45 | * ``` |
| 46 | */ |
| 47 | export function createMcpCopyHandler(params: McpCopyHandlerParams) { |
| 48 | const { selectedClient, source, onTrack, projectRef } = params |
| 49 | |
| 50 | return (type?: McpCopyType) => { |
| 51 | let connectionType: string |
| 52 | switch (type) { |
| 53 | case 'command': |
| 54 | connectionType = 'Command Line' |
| 55 | break |
| 56 | case 'config': |
| 57 | connectionType = 'Config File' |
| 58 | break |
| 59 | case 'url': |
| 60 | default: |
| 61 | connectionType = 'MCP URL' |
| 62 | break |
| 63 | } |
| 64 | |
| 65 | onTrack({ |
| 66 | action: 'connection_string_copied', |
| 67 | properties: { |
| 68 | connectionTab: 'MCP', |
| 69 | selectedItem: selectedClient?.label, |
| 70 | connectionType, |
| 71 | source, |
| 72 | }, |
| 73 | ...(projectRef && { groups: { project: projectRef } }), |
| 74 | }) |
| 75 | } |
| 76 | } |