|
| 1 | +import type { Theme, DisplayMode, DeviceType, ViewMode } from '../types/runtime'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Strongly-typed URL parameters for the ChatGPT Simulator. |
| 5 | + * |
| 6 | + * Use with `createSimulatorUrl()` to generate type-safe URL paths for e2e tests. |
| 7 | + * |
| 8 | + * @example |
| 9 | + * ```ts |
| 10 | + * import { createSimulatorUrl } from 'sunpeak/chatgpt'; |
| 11 | + * |
| 12 | + * // In e2e tests: |
| 13 | + * await page.goto(createSimulatorUrl({ |
| 14 | + * simulation: 'albums-show', |
| 15 | + * theme: 'dark', |
| 16 | + * displayMode: 'fullscreen', |
| 17 | + * })); |
| 18 | + * ``` |
| 19 | + */ |
| 20 | +export interface SimulatorUrlParams { |
| 21 | + /** |
| 22 | + * The simulation name to load (e.g., 'albums-show', 'review-diff'). |
| 23 | + * Corresponds to the simulation JSON filename without the '-simulation.json' suffix. |
| 24 | + */ |
| 25 | + simulation?: string; |
| 26 | + |
| 27 | + /** |
| 28 | + * The color theme for the simulator. |
| 29 | + * @default 'dark' |
| 30 | + */ |
| 31 | + theme?: Theme; |
| 32 | + |
| 33 | + /** |
| 34 | + * The display mode for the widget. |
| 35 | + * - 'inline': Embedded in the conversation |
| 36 | + * - 'pip': Picture-in-picture mode with max height |
| 37 | + * - 'fullscreen': Full screen overlay |
| 38 | + * @default 'inline' |
| 39 | + */ |
| 40 | + displayMode?: DisplayMode; |
| 41 | + |
| 42 | + /** |
| 43 | + * The locale for the simulator (e.g., 'en-US', 'ja-JP'). |
| 44 | + * @default 'en-US' |
| 45 | + */ |
| 46 | + locale?: string; |
| 47 | + |
| 48 | + /** |
| 49 | + * Maximum height in pixels for PiP mode. |
| 50 | + * Only applicable when displayMode is 'pip'. |
| 51 | + */ |
| 52 | + maxHeight?: number; |
| 53 | + |
| 54 | + /** |
| 55 | + * The device type to simulate. |
| 56 | + * Affects default hover/touch capabilities. |
| 57 | + */ |
| 58 | + deviceType?: DeviceType; |
| 59 | + |
| 60 | + /** |
| 61 | + * Whether the device supports hover interactions. |
| 62 | + * @default true for desktop, false for mobile/tablet |
| 63 | + */ |
| 64 | + hover?: boolean; |
| 65 | + |
| 66 | + /** |
| 67 | + * Whether the device supports touch interactions. |
| 68 | + * @default false for desktop, true for mobile/tablet |
| 69 | + */ |
| 70 | + touch?: boolean; |
| 71 | + |
| 72 | + /** |
| 73 | + * Safe area inset from the top of the screen (in pixels). |
| 74 | + * Used for devices with notches or status bars. |
| 75 | + */ |
| 76 | + safeAreaTop?: number; |
| 77 | + |
| 78 | + /** |
| 79 | + * Safe area inset from the bottom of the screen (in pixels). |
| 80 | + * Used for devices with home indicators. |
| 81 | + */ |
| 82 | + safeAreaBottom?: number; |
| 83 | + |
| 84 | + /** |
| 85 | + * Safe area inset from the left of the screen (in pixels). |
| 86 | + */ |
| 87 | + safeAreaLeft?: number; |
| 88 | + |
| 89 | + /** |
| 90 | + * Safe area inset from the right of the screen (in pixels). |
| 91 | + */ |
| 92 | + safeAreaRight?: number; |
| 93 | + |
| 94 | + /** |
| 95 | + * The view mode for the widget. |
| 96 | + * - 'modal': Display as a modal dialog |
| 97 | + * - 'default': Normal display |
| 98 | + */ |
| 99 | + viewMode?: ViewMode; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Creates a URL path with query parameters for the ChatGPT Simulator. |
| 104 | + * |
| 105 | + * @param params - The simulator parameters to encode |
| 106 | + * @param basePath - The base path for the URL (default: '/') |
| 107 | + * @returns A URL path string with encoded query parameters |
| 108 | + * |
| 109 | + * @example |
| 110 | + * ```ts |
| 111 | + * // Basic usage |
| 112 | + * createSimulatorUrl({ simulation: 'albums-show', theme: 'light' }) |
| 113 | + * // Returns: '/?simulation=albums-show&theme=light' |
| 114 | + * |
| 115 | + * // With display mode |
| 116 | + * createSimulatorUrl({ |
| 117 | + * simulation: 'review-diff', |
| 118 | + * theme: 'dark', |
| 119 | + * displayMode: 'fullscreen', |
| 120 | + * }) |
| 121 | + * // Returns: '/?simulation=review-diff&theme=dark&displayMode=fullscreen' |
| 122 | + * |
| 123 | + * // With device simulation |
| 124 | + * createSimulatorUrl({ |
| 125 | + * simulation: 'map-show', |
| 126 | + * deviceType: 'mobile', |
| 127 | + * touch: true, |
| 128 | + * hover: false, |
| 129 | + * }) |
| 130 | + * // Returns: '/?simulation=map-show&deviceType=mobile&touch=true&hover=false' |
| 131 | + * |
| 132 | + * // With safe area insets (for notch simulation) |
| 133 | + * createSimulatorUrl({ |
| 134 | + * simulation: 'carousel-show', |
| 135 | + * safeAreaTop: 44, |
| 136 | + * safeAreaBottom: 34, |
| 137 | + * }) |
| 138 | + * // Returns: '/?simulation=carousel-show&safeAreaTop=44&safeAreaBottom=34' |
| 139 | + * ``` |
| 140 | + */ |
| 141 | +export function createSimulatorUrl(params: SimulatorUrlParams, basePath = '/'): string { |
| 142 | + const searchParams = new URLSearchParams(); |
| 143 | + |
| 144 | + // Add each defined parameter |
| 145 | + if (params.simulation !== undefined) { |
| 146 | + searchParams.set('simulation', params.simulation); |
| 147 | + } |
| 148 | + if (params.theme !== undefined) { |
| 149 | + searchParams.set('theme', params.theme); |
| 150 | + } |
| 151 | + if (params.displayMode !== undefined) { |
| 152 | + searchParams.set('displayMode', params.displayMode); |
| 153 | + } |
| 154 | + if (params.locale !== undefined) { |
| 155 | + searchParams.set('locale', params.locale); |
| 156 | + } |
| 157 | + if (params.maxHeight !== undefined) { |
| 158 | + searchParams.set('maxHeight', String(params.maxHeight)); |
| 159 | + } |
| 160 | + if (params.deviceType !== undefined) { |
| 161 | + searchParams.set('deviceType', params.deviceType); |
| 162 | + } |
| 163 | + if (params.hover !== undefined) { |
| 164 | + searchParams.set('hover', String(params.hover)); |
| 165 | + } |
| 166 | + if (params.touch !== undefined) { |
| 167 | + searchParams.set('touch', String(params.touch)); |
| 168 | + } |
| 169 | + if (params.safeAreaTop !== undefined) { |
| 170 | + searchParams.set('safeAreaTop', String(params.safeAreaTop)); |
| 171 | + } |
| 172 | + if (params.safeAreaBottom !== undefined) { |
| 173 | + searchParams.set('safeAreaBottom', String(params.safeAreaBottom)); |
| 174 | + } |
| 175 | + if (params.safeAreaLeft !== undefined) { |
| 176 | + searchParams.set('safeAreaLeft', String(params.safeAreaLeft)); |
| 177 | + } |
| 178 | + if (params.safeAreaRight !== undefined) { |
| 179 | + searchParams.set('safeAreaRight', String(params.safeAreaRight)); |
| 180 | + } |
| 181 | + if (params.viewMode !== undefined) { |
| 182 | + searchParams.set('viewMode', params.viewMode); |
| 183 | + } |
| 184 | + |
| 185 | + const queryString = searchParams.toString(); |
| 186 | + return queryString ? `${basePath}?${queryString}` : basePath; |
| 187 | +} |
0 commit comments