|
1 | 1 | import * as LDClient from 'launchdarkly-js-client-sdk'; |
2 | 2 | import { EventInterceptionPlugin } from '@launchdarkly/toolbar'; |
3 | | -import { createFlagUrlOverridePlugin } from './flag-url-override-plugin.js'; |
| 3 | +import { |
| 4 | + createFlagUrlOverridePlugin, |
| 5 | + CLEAR_MODE_EXPLICIT, |
| 6 | + CLEAR_MODE_ALWAYS, |
| 7 | + CLEAR_MODE_AUTO |
| 8 | +} from './flag-url-override-plugin.js'; |
4 | 9 |
|
5 | 10 | // Custom Logger Implementation |
6 | 11 | class CustomLogger { |
@@ -56,6 +61,21 @@ function getClientSideIdFromUrl() { |
56 | 61 | return urlParams.get('clientSideId') || ''; |
57 | 62 | } |
58 | 63 |
|
| 64 | +// Get clear mode from query parameter and map to symbol |
| 65 | +function getClearModeFromUrl() { |
| 66 | + const urlParams = new URLSearchParams(window.location.search); |
| 67 | + const clearModeStr = urlParams.get('clearMode') || 'auto'; |
| 68 | + |
| 69 | + // Map string to symbol |
| 70 | + const modeMap = { |
| 71 | + 'auto': CLEAR_MODE_AUTO, |
| 72 | + 'explicit': CLEAR_MODE_EXPLICIT, |
| 73 | + 'always': CLEAR_MODE_ALWAYS |
| 74 | + }; |
| 75 | + |
| 76 | + return modeMap[clearModeStr] || CLEAR_MODE_AUTO; |
| 77 | +} |
| 78 | + |
59 | 79 | // Helper function to format reason object |
60 | 80 | function formatReason(reason) { |
61 | 81 | if (!reason) return 'N/A'; |
@@ -157,8 +177,11 @@ async function initializeLaunchDarkly(clientSideID) { |
157 | 177 | logger.info('Initializing LaunchDarkly client...'); |
158 | 178 |
|
159 | 179 | // Create plugin instances that will be shared between client and toolbar |
| 180 | + // Get clearMode from URL (defaults to AUTO for reliable URL sharing) |
| 181 | + const clearMode = getClearModeFromUrl(); |
160 | 182 | const flagOverridePlugin = createFlagUrlOverridePlugin({ |
161 | 183 | parameterPrefix: 'ld_override_', |
| 184 | + clearMode: clearMode, |
162 | 185 | overrideOptions: {}, |
163 | 186 | logger: logger |
164 | 187 | }); |
@@ -233,10 +256,32 @@ function initApp() { |
233 | 256 | // Get client-side ID from URL |
234 | 257 | const clientSideId = getClientSideIdFromUrl(); |
235 | 258 |
|
236 | | - // Populate the form input with the current value |
237 | | - const input = document.getElementById('clientSideId'); |
238 | | - if (input) { |
239 | | - input.value = clientSideId; |
| 259 | + // Populate the form inputs with current values |
| 260 | + const clientIdInput = document.getElementById('clientSideId'); |
| 261 | + if (clientIdInput) { |
| 262 | + clientIdInput.value = clientSideId; |
| 263 | + } |
| 264 | + |
| 265 | + const urlParams = new URLSearchParams(window.location.search); |
| 266 | + const clearModeSelect = document.getElementById('clearMode'); |
| 267 | + const clearModeNotice = document.getElementById('clearModeNotice'); |
| 268 | + |
| 269 | + if (clearModeSelect) { |
| 270 | + const clearModeStr = urlParams.get('clearMode') || 'auto'; |
| 271 | + clearModeSelect.value = clearModeStr; |
| 272 | + |
| 273 | + // Add change listener to update URL when clearMode changes |
| 274 | + clearModeSelect.addEventListener('change', (e) => { |
| 275 | + const url = new URL(window.location.href); |
| 276 | + url.searchParams.set('clearMode', e.target.value); |
| 277 | + window.history.replaceState({}, '', url.toString()); |
| 278 | + logger.info(`Clear mode changed to: ${e.target.value}`); |
| 279 | + |
| 280 | + // Show notice to reload |
| 281 | + if (clearModeNotice) { |
| 282 | + clearModeNotice.classList.add('visible'); |
| 283 | + } |
| 284 | + }); |
240 | 285 | } |
241 | 286 |
|
242 | 287 | // Initialize LaunchDarkly if clientSideId is provided |
|
0 commit comments