Skip to content

Commit 1ec251b

Browse files
dylanjeffersclaude
andauthored
Fix SegmentedControl to handle invalid selected values gracefully (#14059)
## Summary This PR adds validation to the SegmentedControl component to gracefully handle cases where the selected value doesn't match any available option, and fixes a bug in TracksPreview where an invalid enum value was being passed. ## Key Changes - **SegmentedControl**: Added fallback logic to validate that the selected value exists in the options list. If the selected value is invalid, it falls back to the first available option instead of rendering an unmatched state. - **TracksPreview**: Fixed a bug where `UploadType.INDIVIDUAL_TRACK` (singular) was being passed to SegmentedControl, but the available options only included `UploadType.INDIVIDUAL_TRACKS` (plural). Added a mapping to convert the singular enum value to the plural one that matches the available options. ## Implementation Details - The SegmentedControl now performs an `options.some()` check to verify the selected value exists before using it - If validation fails, it safely falls back to `options[0]?.key` or the raw selected value as a last resort - This prevents UI inconsistencies and ensures the component always renders in a valid state https://claude.ai/code/session_01S8Yn3jaHMpAZ2LRKDoDPfA --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6fc39c6 commit 1ec251b

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@audius/harmony": patch
3+
"@audius/web": patch
4+
---
5+
6+
Fix SegmentedControl text color being subdued on initial render when selected value doesn't match any option key

packages/harmony/src/components/segmented-control/SegmentedControl.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ export const SegmentedControl = <T extends string>(
4242
const [localSelected, setLocalSelected] = useState(options[0]?.key ?? '')
4343
const [maxOptionWidth, setMaxOptionWidth] = useState(0)
4444

45-
const selectedOption = selected ?? localSelected
45+
const rawSelectedOption = selected ?? localSelected
46+
// If the selected value doesn't match any option, fall back to the first option
47+
const selectedOption =
48+
options.some((option) => option.key === rawSelectedOption)
49+
? rawSelectedOption
50+
: options[0]?.key ?? rawSelectedOption
4651

4752
const onSetSelected = (option: T) => {
4853
// Call props function if controlled

packages/web/src/pages/upload-page/components/TracksPreview.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ export const TracksPreview = (props: TracksPreviewProps) => {
5959
<SegmentedControl
6060
label={messages.releaseType}
6161
onSelectOption={handleOptionSelect}
62-
selected={String(uploadType)}
62+
selected={String(
63+
uploadType === UploadType.INDIVIDUAL_TRACK
64+
? UploadType.INDIVIDUAL_TRACKS
65+
: uploadType
66+
)}
6367
options={[
6468
{ key: String(UploadType.INDIVIDUAL_TRACKS), text: 'Tracks' },
6569
{ key: String(UploadType.ALBUM), text: 'Album' },

0 commit comments

Comments
 (0)