Skip to content

Commit 60668d2

Browse files
changes
1 parent ddcab6f commit 60668d2

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const SORT_ORDER_VALUES = [
2121
'',
2222
'{ $natural: -1 }',
2323
'{ _id: 1 }',
24-
'{_id: -1 }',
24+
'{ _id: -1 }',
2525
] as const;
2626
export type SORT_ORDERS = typeof SORT_ORDER_VALUES[number];
2727

@@ -196,7 +196,13 @@ type PreferenceDefinition<K extends keyof AllPreferences> = {
196196
/** A description used for the --help text and the Settings UI */
197197
description: K extends keyof InternalUserPreferences
198198
? null
199-
: { short: string; long?: string };
199+
: {
200+
short: string;
201+
long?: string;
202+
options?: AllPreferences[K] extends string
203+
? { [k in AllPreferences[K]]: string }
204+
: never;
205+
};
200206
/** A method for deriving the current semantic value of this option, even if it differs from the stored value */
201207
deriveValue?: DeriveValueFunction<AllPreferences[K]>;
202208
/** A method for cleaning up/normalizing input from the command line or global config file */
@@ -558,13 +564,13 @@ export const storedUserPreferencesProps: Required<{
558564
description: {
559565
short: 'Default Sort for Query Bar',
560566
long: "All queries executed from the query bar will apply the sort order '$natural: -1'.",
567+
options: {
568+
'': 'Default',
569+
'{ $natural: -1 }': 'Natural Order recent first',
570+
'{ _id: 1 }': 'ID Ascending',
571+
'{ _id: -1 }': 'ID Descending',
572+
},
561573
},
562-
selectableValues: [
563-
{ value: '', label: 'Default' },
564-
{ value: '{ $natural: -1 }', label: 'Natural Order recent first' },
565-
{ value: '{ _id: 1 }', label: 'ID Ascending' },
566-
{ value: '{ _id: -1 }', label: 'ID Descending' },
567-
],
568574
validator: z.enum(SORT_ORDER_VALUES).default(''),
569575
type: 'string',
570576
},

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export { featureFlags } from './feature-flags';
1313
export {
1414
getSettingDescription,
1515
getSettingSelectableValues,
16+
SORT_ORDER_VALUES,
1617
} from './preferences-schema';
17-
export type { AllPreferences } from './preferences-schema';
18+
export type { AllPreferences, SORT_ORDERS } from './preferences-schema';
1819
export type { DevtoolsProxyOptions } from '@mongodb-js/devtools-proxy-support';

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React, { useCallback } from 'react';
22
import type { UserConfigurablePreferences } from 'compass-preferences-model';
33
import {
44
getSettingDescription,
5-
getSettingSelectableValues,
65
featureFlags,
76
} from 'compass-preferences-model/provider';
7+
import { SORT_ORDER_VALUES } from 'compass-preferences-model/provider';
88
import { settingStateLabels } from './state-labels';
99
import {
1010
Checkbox,
@@ -158,20 +158,18 @@ function NumericSetting<PreferenceName extends NumericPreferences>({
158158
);
159159
}
160160

161-
function DropdownSetting<PreferenceName extends StringPreferences>({
161+
function DefaultSortOrderSetting<PreferenceName extends 'defaultSortOrder'>({
162162
name,
163163
onChange,
164-
selectableValues,
165164
value,
166165
disabled,
167166
}: {
168167
name: PreferenceName;
169168
onChange: HandleChange<PreferenceName>;
170-
selectableValues: ReadonlyArray<{ label: string; value: string }>;
171169
value: string | undefined;
172170
disabled: boolean;
173171
}) {
174-
selectableValues = JSON.parse(JSON.stringify(selectableValues));
172+
const optionDescriptions = getSettingDescription(name).description.options;
175173
const onChangeEvent = useCallback(
176174
(event: React.ChangeEvent<HTMLSelectElement>) => {
177175
onChange(
@@ -195,9 +193,9 @@ function DropdownSetting<PreferenceName extends StringPreferences>({
195193
onChange={onChangeEvent}
196194
disabled={disabled}
197195
>
198-
{selectableValues.map(({ label, value }, i) => (
199-
<option key={i} value={value}>
200-
{label}
196+
{SORT_ORDER_VALUES.map((option, i) => (
197+
<option key={i} value={option}>
198+
{(optionDescriptions && optionDescriptions[option]) || value}
201199
</option>
202200
))}
203201
</select>
@@ -255,7 +253,6 @@ type AnySetting = {
255253
name: string;
256254
type: unknown;
257255
value?: unknown;
258-
selectableValues?: ReadonlyArray<{ label: string; value: string }>;
259256
onChange(field: string, value: unknown): void;
260257
};
261258

@@ -301,7 +298,7 @@ function SettingsInput({
301298

302299
let input = null;
303300

304-
const { name, type, onChange, value, selectableValues } = props;
301+
const { name, type, onChange, value } = props;
305302

306303
if (type === 'boolean') {
307304
input = (
@@ -312,11 +309,10 @@ function SettingsInput({
312309
disabled={!!disabled}
313310
/>
314311
);
315-
} else if (type === 'string' && selectableValues) {
312+
} else if (type === 'string' && name === 'defaultSortOrder') {
316313
input = (
317-
<DropdownSetting
314+
<DefaultSortOrderSetting
318315
name={name}
319-
selectableValues={selectableValues}
320316
onChange={onChange}
321317
value={value as string}
322318
disabled={!!disabled}
@@ -364,7 +360,6 @@ const ConnectedSettingsInput = connect(
364360

365361
return {
366362
value: settings[name],
367-
selectableValues: getSettingSelectableValues(name).selectableValues,
368363
type: type,
369364
disabled: !!preferenceStates[name],
370365
stateLabel: settingStateLabels[preferenceStates[name] ?? ''],

0 commit comments

Comments
 (0)