Skip to content

Commit

Permalink
Bugfix/203 sorting resets on refresh (cribeiro84#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerillio authored Dec 8, 2022
1 parent d1c3d69 commit fe24b9f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
26 changes: 20 additions & 6 deletions src/tabs/PulRequestsTabData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
} from "azure-devops-extension-api/Git/Git";
import { IStatusProps } from "azure-devops-ui/Status";
import { IColor } from "azure-devops-ui/Utilities/Color";
import { SortOrder } from "azure-devops-ui/Table";
import { IdentityRef } from "azure-devops-extension-api/WebApi/WebApi";
import {
TeamProjectReference,
WebApiTagDefinition
} from "azure-devops-extension-api/Core/Core";
import { PullRequestModel } from "../models/PullRequestModel";
import { UserPreferencesInstance } from "../common";

export const refsPreffix = "refs/heads/";

Expand Down Expand Up @@ -193,6 +193,8 @@ export interface IPullRequestsTabState {
errorMessage: string;
pullRequestCount: number;
savedProjects: string[];
/** Direction to sort pull request age in */
sortOrder: SortOrder;
}

export function sortBranchOrIdentity(
Expand Down Expand Up @@ -224,12 +226,24 @@ export function sortTagRepoTeamProject(
}

export function sortPullRequests(
a: PullRequestModel,
b: PullRequestModel,
order: SortOrder
) {
return order === SortOrder.ascending
? comparePullRequestAge(a, b)
: -comparePullRequestAge(a, b); // Invert if descending
}

/**
* Compares the age of two pull requests.
* @returns A negative number if {@link a} is more recent than {@link b},
* a positive number if {@link a} is older than {@link b}, otherwise 0.
*/
export function comparePullRequestAge(
a: PullRequestModel,
b: PullRequestModel
) {
return UserPreferencesInstance.selectedDefaultSorting === "asc"
? b.gitPullRequest.creationDate.getTime() -
a.gitPullRequest.creationDate.getTime()
: a.gitPullRequest.creationDate.getTime() -
b.gitPullRequest.creationDate.getTime();
return b.gitPullRequest.creationDate.getTime() -
a.gitPullRequest.creationDate.getTime();
}
17 changes: 7 additions & 10 deletions src/tabs/PullRequestsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export class PullRequestsTab extends React.Component<
errorMessage: "",
pullRequestCount: 0,
savedProjects: [],
sortOrder: UserPreferencesInstance.selectedDefaultSorting === "asc"
? SortOrder.ascending
: SortOrder.descending
};

this.filter = new Filter();
Expand Down Expand Up @@ -511,8 +514,9 @@ export class PullRequestsTab extends React.Component<
})
.finally(async () => {
if (newPullRequestList.length > 0) {
const { sortOrder } = this.state;
pullRequests.push(...newPullRequestList);
pullRequests = pullRequests.sort(Data.sortPullRequests);
pullRequests = pullRequests.sort((a, b) => Data.sortPullRequests(a, b, sortOrder));

this.setState({
pullRequests,
Expand Down Expand Up @@ -934,6 +938,7 @@ export class PullRequestsTab extends React.Component<
pullRequests
)
);
this.setState({ sortOrder: proposedSortOrder });
});

if (
Expand Down Expand Up @@ -1074,15 +1079,7 @@ export class PullRequestsTab extends React.Component<
null, // Title column
null, // Details column
// Sort on When column
(
item1: PullRequestModel.PullRequestModel,
item2: PullRequestModel.PullRequestModel
): number => {
return (
item2.gitPullRequest.creationDate.getTime() -
item1.gitPullRequest.creationDate.getTime()
);
},
Data.comparePullRequestAge,
null, // Reviewers column
];

Expand Down

0 comments on commit fe24b9f

Please sign in to comment.