Skip to content

Commit 7a56074

Browse files
changes
1 parent b4a3945 commit 7a56074

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

packages/compass-preferences-model/src/preferences-schema.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import {
1717
export const THEMES_VALUES = ['DARK', 'LIGHT', 'OS_THEME'] as const;
1818
export type THEMES = typeof THEMES_VALUES[number];
1919

20+
export const SORT_ORDER_VALUES = [
21+
'',
22+
'{ $natural: -1 }',
23+
'{ _id: 1 }',
24+
'{_id: -1 }',
25+
] as const;
26+
export type SORT_ORDERS = typeof SORT_ORDER_VALUES[number];
27+
2028
export type PermanentFeatureFlags = {
2129
showDevFeatureFlags?: boolean;
2230
enableDebugUseCsfleSchemaMap?: boolean;
@@ -69,7 +77,7 @@ export type UserConfigurablePreferences = PermanentFeatureFlags &
6977
enableGenAISampleDocumentPassing: boolean;
7078
enablePerformanceAdvisorBanner: boolean;
7179
maximumNumberOfActiveConnections?: number;
72-
showRecentDocumentsFirst: boolean;
80+
defaultSortOrder: SORT_ORDERS;
7381
enableShowDialogOnQuit: boolean;
7482
enableCreatingNewConnections: boolean;
7583
enableProxySupport: boolean;
@@ -200,6 +208,8 @@ type PreferenceDefinition<K extends keyof AllPreferences> = {
200208
? boolean
201209
: false
202210
: boolean;
211+
212+
selectableValues?: ReadonlyArray<AllPreferences[K]>;
203213
validator: z.Schema<
204214
AllPreferences[K],
205215
z.ZodTypeDef,
@@ -541,16 +551,17 @@ export const storedUserPreferencesProps: Required<{
541551
/**
542552
* Set the default sort.
543553
*/
544-
showRecentDocumentsFirst: {
554+
defaultSortOrder: {
545555
ui: true,
546556
cli: true,
547557
global: true,
548558
description: {
549559
short: 'Default Sort for Query Bar',
550560
long: "All queries executed from the query bar will apply the sort order '$natural: -1'.",
551561
},
552-
validator: z.boolean().default(false),
553-
type: 'boolean',
562+
selectableValues: SORT_ORDER_VALUES,
563+
validator: z.enum(SORT_ORDER_VALUES).default(''),
564+
type: 'string',
554565
},
555566

556567
/**

packages/compass-settings/src/components/settings/settings-list.tsx

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,52 @@ function NumericSetting<PreferenceName extends NumericPreferences>({
157157
);
158158
}
159159

160+
function DropdownSetting<PreferenceName extends StringPreferences>({
161+
name,
162+
onChange,
163+
selectableValues,
164+
value,
165+
disabled,
166+
}: {
167+
name: PreferenceName;
168+
onChange: HandleChange<PreferenceName>;
169+
selectableValues: string[];
170+
value: string | undefined;
171+
disabled: boolean;
172+
}) {
173+
const onChangeEvent = useCallback(
174+
(event: React.ChangeEvent<HTMLSelectElement>) => {
175+
onChange(
176+
name,
177+
event.target.value as UserConfigurablePreferences[PreferenceName]
178+
);
179+
},
180+
[name, onChange]
181+
);
182+
183+
return (
184+
<>
185+
<SettingLabel name={name} />
186+
<select
187+
className={inputStyles}
188+
aria-labelledby={`${name}-label`}
189+
id={name}
190+
name={name}
191+
data-testid={name}
192+
value={value === undefined ? '' : `${value}`}
193+
onChange={onChangeEvent}
194+
disabled={disabled}
195+
>
196+
{selectableValues.map((valu, ie) => (
197+
<option key={i} value={value}>
198+
{value}
199+
</option>
200+
))}
201+
</select>
202+
</>
203+
);
204+
}
205+
160206
function StringSetting<PreferenceName extends StringPreferences>({
161207
name,
162208
onChange,
@@ -206,6 +252,7 @@ function StringSetting<PreferenceName extends StringPreferences>({
206252
type AnySetting = {
207253
name: string;
208254
type: unknown;
255+
selectableValues?: unknown[];
209256
value?: unknown;
210257
onChange(field: string, value: unknown): void;
211258
};
@@ -252,7 +299,7 @@ function SettingsInput({
252299

253300
let input = null;
254301

255-
const { name, type, onChange, value } = props;
302+
const { name, type, onChange, value, selectableValues } = props;
256303

257304
if (type === 'boolean') {
258305
input = (
@@ -263,9 +310,17 @@ function SettingsInput({
263310
disabled={!!disabled}
264311
/>
265312
);
266-
}
267-
268-
if (type === 'number') {
313+
} else if (type === 'string' && selectableValues) {
314+
input = (
315+
<DropdownSetting
316+
name={name}
317+
selectableValues={selectableValues}
318+
onChange={onChange}
319+
value={value as string}
320+
disabled={!!disabled}
321+
/>
322+
);
323+
} else if (type === 'number') {
269324
input = (
270325
<NumericSetting
271326
name={name}
@@ -275,9 +330,7 @@ function SettingsInput({
275330
disabled={!!disabled}
276331
/>
277332
);
278-
}
279-
280-
if (type === 'string') {
333+
} else if (type === 'string') {
281334
input = (
282335
<StringSetting
283336
name={name}

0 commit comments

Comments
 (0)