Skip to content

Commit 5850810

Browse files
committed
feat(app): integrate clear mode UI and URL handling
1 parent 1d5f76e commit 5850810

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

app.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as LDClient from 'launchdarkly-js-client-sdk';
22
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';
49

510
// Custom Logger Implementation
611
class CustomLogger {
@@ -56,6 +61,21 @@ function getClientSideIdFromUrl() {
5661
return urlParams.get('clientSideId') || '';
5762
}
5863

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+
5979
// Helper function to format reason object
6080
function formatReason(reason) {
6181
if (!reason) return 'N/A';
@@ -157,8 +177,11 @@ async function initializeLaunchDarkly(clientSideID) {
157177
logger.info('Initializing LaunchDarkly client...');
158178

159179
// 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();
160182
const flagOverridePlugin = createFlagUrlOverridePlugin({
161183
parameterPrefix: 'ld_override_',
184+
clearMode: clearMode,
162185
overrideOptions: {},
163186
logger: logger
164187
});
@@ -233,10 +256,32 @@ function initApp() {
233256
// Get client-side ID from URL
234257
const clientSideId = getClientSideIdFromUrl();
235258

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+
});
240285
}
241286

242287
// Initialize LaunchDarkly if clientSideId is provided

index.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@
157157
.form-group button:active {
158158
background-color: #1f34bb;
159159
}
160+
.form-group select {
161+
padding: 10px;
162+
border: 1px solid #ddd;
163+
border-radius: 4px;
164+
font-size: 14px;
165+
background-color: white;
166+
cursor: pointer;
167+
}
168+
.form-group select:focus {
169+
outline: none;
170+
border-color: #405BFF;
171+
box-shadow: 0 0 0 2px rgba(64, 91, 255, 0.1);
172+
}
173+
.reload-notice {
174+
display: none;
175+
color: #FF9800;
176+
font-size: 0.9em;
177+
margin-top: 5px;
178+
padding: 8px;
179+
background-color: #FFF3E0;
180+
border-radius: 4px;
181+
border-left: 3px solid #FF9800;
182+
}
183+
.reload-notice.visible {
184+
display: block;
185+
}
160186
</style>
161187
</head>
162188
<body>
@@ -170,6 +196,17 @@ <h2>Configuration</h2>
170196
<input type="text" id="clientSideId" name="clientSideId" placeholder="Enter your LaunchDarkly client-side ID" />
171197
<button type="submit">Load SDK</button>
172198
</div>
199+
<div class="form-group">
200+
<label for="clearMode">Override Behavior:</label>
201+
<select id="clearMode" name="clearMode">
202+
<option value="auto">Clear all existing local overrides when overrides are set in the URL</option>
203+
<option value="explicit">Only clear local overrides when URL includes ld_override__clear</option>
204+
<option value="always">Only use URL overrides, ignore local storage</option>
205+
</select>
206+
<div id="clearModeNotice" class="reload-notice">
207+
Setting updated. Click "Load SDK" to apply changes.
208+
</div>
209+
</div>
173210
</form>
174211
</div>
175212

0 commit comments

Comments
 (0)