Skip to content

Commit

Permalink
Memomize isCommittingFileHiddenByFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
tidy-dev committed Dec 19, 2024
1 parent 1262dae commit 1ef8592
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/src/ui/changes/commit-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface ICommitMessageProps {
readonly anyFilesSelected: boolean
/** Whether the user can see all the files to commit in the changes list. They
* may not be able to if the list is filtered */
readonly allFilesToCommitNotVisible?: boolean
readonly showPromptForCommittingFileHiddenByFilter?: boolean
readonly isShowingModal: boolean
readonly isShowingFoldout: boolean

Expand Down Expand Up @@ -541,7 +541,7 @@ export class CommitMessage extends React.Component<

if (
options?.warnFilesNotVisible !== false &&
this.props.allFilesToCommitNotVisible === true &&
this.props.showPromptForCommittingFileHiddenByFilter === true &&
this.props.onFilesToCommitNotVisible
) {
this.props.onFilesToCommitNotVisible(() =>
Expand Down
40 changes: 36 additions & 4 deletions app/src/ui/changes/filter-changes-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { StashDiffViewerId } from '../stashing'
import { AugmentedSectionFilterList } from '../lib/augmented-filter-list'
import { IFilterListGroup, IFilterListItem } from '../lib/filter-list'
import { ClickSource } from '../lib/list'
import memoizeOne from 'memoize-one'

interface IChangesListItem extends IFilterListItem {
readonly id: string
Expand Down Expand Up @@ -272,6 +273,31 @@ export class FilterChangesList extends React.Component<
IFilterChangesListProps,
IFilterChangesListState
> {
private isCommittingFileHiddenByFilter = memoizeOne(
(
filterText: string,
fileIdsIncludedInCommit: ReadonlyArray<string>,
filteredItems: Map<string, IChangesListItem>,
fileCount: number
) => {
// All possible files are present in the list (empty filter or all matching filter)
if (filterText === '' || filteredItems.size === fileCount) {
return false
}

// If filtered rows count is 1 and included for commit rows count is 2,
// there is no way the included for commit rows are visible regardless of
// what they are.
if (fileIdsIncludedInCommit.length > this.state.filteredItems.size) {
return true
}

// If we can find a file id included in the commit that does not exist in
// the filtered items, then we are committing a hidden file.
return fileIdsIncludedInCommit.some(fId => !filteredItems.get(fId))
}
)

private headerRef = createObservableRef<HTMLDivElement>()
private includeAllCheckBoxRef = React.createRef<Checkbox>()

Expand Down Expand Up @@ -861,10 +887,14 @@ export class FilterChangesList extends React.Component<
this.props.repository.gitHubRepository === null ||
hasWritePermission(this.props.repository.gitHubRepository)

const allFilesToCommitNotVisible =
const showPromptForCommittingFileHiddenByFilter =
this.props.askForConfirmationOnCommitFilteredChanges &&
(filesSelected.length > this.state.filteredItems.size ||
filesSelected.some(f => !this.state.filteredItems.get(f.id)))
this.isCommittingFileHiddenByFilter(
this.state.filterText,
filesSelected.map(f => f.id),
this.state.filteredItems,
fileCount
)

return (
<CommitMessage
Expand All @@ -875,7 +905,9 @@ export class FilterChangesList extends React.Component<
isShowingModal={this.props.isShowingModal}
isShowingFoldout={this.props.isShowingFoldout}
anyFilesSelected={anyFilesSelected}
allFilesToCommitNotVisible={allFilesToCommitNotVisible}
showPromptForCommittingFileHiddenByFilter={
showPromptForCommittingFileHiddenByFilter
}
anyFilesAvailable={fileCount > 0}
repository={repository}
repositoryAccount={repositoryAccount}
Expand Down

0 comments on commit 1ef8592

Please sign in to comment.