-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: LaunchDevly: Cleanup code and remove only show overrides when there are no overrides #427
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import { | |
Stack, | ||
} from '@launchpad-ui/core'; | ||
import Theme from '@launchpad-ui/tokens'; | ||
import { useState, useCallback, useMemo } from 'react'; | ||
import { useState, useCallback, useMemo, useEffect } from 'react'; | ||
import { Icon } from '@launchpad-ui/icons'; | ||
import { apiRoute } from './util.ts'; | ||
import { FlagVariation } from './api.ts'; | ||
|
@@ -42,14 +42,20 @@ function Flags({ | |
}: FlagProps) { | ||
const [onlyShowOverrides, setOnlyShowOverrides] = useState(false); | ||
const [searchTerm, setSearchTerm] = useState(''); | ||
const [currentPage, setCurrentPage] = useState(0); // Change initial page to 0 | ||
const [currentPage, setCurrentPage] = useState(0); | ||
const flagsPerPage = 20; | ||
|
||
const overridesPresent = useMemo( | ||
() => overrides && Object.keys(overrides).length > 0, | ||
[overrides], | ||
); | ||
|
||
useEffect(() => { | ||
if (!overridesPresent && onlyShowOverrides) { | ||
setOnlyShowOverrides(false); | ||
} | ||
}, [overridesPresent, onlyShowOverrides]); | ||
|
||
const filteredFlags = useMemo(() => { | ||
if (!flags) return []; | ||
const flagEntries = Object.entries(flags); | ||
|
@@ -58,7 +64,7 @@ function Flags({ | |
if (!searchTerm) return true; | ||
const [flagKey] = entry; | ||
const result = fuzzysort.single(searchTerm.toLowerCase(), flagKey); | ||
return result && result.score > -5000; // Adjust threshold as needed | ||
return result && result.score > -5000; | ||
}) | ||
.filter((entry) => { | ||
const [flagKey] = entry; | ||
|
@@ -70,10 +76,10 @@ function Flags({ | |
|
||
return true; | ||
}); | ||
}, [flags, searchTerm, onlyShowOverrides]); | ||
}, [flags, searchTerm, onlyShowOverrides, overrides]); | ||
|
||
const paginatedFlags = useMemo(() => { | ||
const startIndex = currentPage * flagsPerPage; // Adjust startIndex calculation | ||
const startIndex = currentPage * flagsPerPage; | ||
const endIndex = startIndex + flagsPerPage; | ||
return filteredFlags.slice(startIndex, endIndex); | ||
}, [filteredFlags, currentPage]); | ||
|
@@ -144,18 +150,16 @@ function Flags({ | |
const handlePageChange = (direction: string) => { | ||
switch (direction) { | ||
case 'next': | ||
setCurrentPage( | ||
(prevPage) => Math.min(prevPage + 1, totalPages - 1), // Adjust page increment | ||
); | ||
setCurrentPage((prevPage) => Math.min(prevPage + 1, totalPages - 1)); | ||
break; | ||
case 'prev': | ||
setCurrentPage((prevPage) => Math.max(prevPage - 1, 0)); // Adjust page decrement | ||
setCurrentPage((prevPage) => Math.max(prevPage - 1, 0)); | ||
break; | ||
case 'first': | ||
setCurrentPage(0); // Adjust first page | ||
setCurrentPage(0); | ||
break; | ||
case 'last': | ||
setCurrentPage(totalPages - 1); // Adjust last page | ||
setCurrentPage(totalPages - 1); | ||
break; | ||
default: | ||
console.error('invalid page change direction.'); | ||
|
@@ -197,8 +201,6 @@ function Flags({ | |
variant="destructive" | ||
isDisabled={!overridesPresent} | ||
onPress={async () => { | ||
// This button is disabled unless overrides are present, but the | ||
// type is nullable | ||
if (!overrides) { | ||
return; | ||
} | ||
|
@@ -207,24 +209,11 @@ function Flags({ | |
|
||
await Promise.all( | ||
overrideKeys.map((flagKey) => { | ||
// Opt out of local state updates since we're bulk-removing | ||
// overrides async | ||
removeOverride(flagKey); | ||
return removeOverride(flagKey); | ||
}), | ||
); | ||
|
||
// Winnow out removed overrides and update local state in a | ||
// single pass | ||
const updatedOverrides = overrideKeys.reduce( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out this was entirely unnecessary |
||
(accum, flagKey) => { | ||
delete accum[flagKey]; | ||
|
||
return accum; | ||
}, | ||
{ ...overrides }, | ||
); | ||
|
||
setOverrides(updatedOverrides); | ||
setOverrides({}); | ||
setOnlyShowOverrides(false); | ||
}} | ||
> | ||
|
@@ -238,10 +227,10 @@ function Flags({ | |
<Group> | ||
<Icon name="search" size="small" /> | ||
<Input | ||
placeholder="Search flags" | ||
placeholder="Search flags by key" | ||
onChange={(e) => { | ||
setSearchTerm(e.target.value); | ||
setCurrentPage(0); // Reset pagination | ||
setCurrentPage(0); | ||
}} | ||
value={searchTerm} | ||
aria-label="Search flags input" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main bug fix here.