forked from DevLeonardoCommunity/github-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle issue (Add custom sorting DevLeonardoCommunity#60)
- Loading branch information
Showing
7 changed files
with
197 additions
and
73 deletions.
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./useGitHubPullRequests"; | ||
export * from "./useGitHubQuery"; | ||
export * from "./useFilteredRepositories"; | ||
export * from "./useHandleStateRepositories"; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { useMemo } from "react"; | ||
import { useSession } from "next-auth/react"; | ||
import { | ||
PullRequestContributionsByRepository, | ||
PullRequestState, | ||
RepositoryOrder, | ||
} from "@/types/github"; | ||
import { compareArrayString } from "@/utils/compare"; | ||
|
||
export const useHandleStateRepositories = ( | ||
repositories: PullRequestContributionsByRepository[], | ||
searchQuery: string, | ||
hideOwnRepo: boolean, | ||
pullRequestState: PullRequestState, | ||
orderState: RepositoryOrder | ||
) => { | ||
const { data: session } = useSession(); | ||
|
||
const filteredRepositories = useMemo(() => { | ||
//Filter section | ||
const filterOutOwnRepos = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return repos?.filter( | ||
(repoData) => repoData.repository.owner.login !== session?.user.login | ||
); | ||
}; | ||
const filterReposBySearchQuery = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
const query = searchQuery.toLowerCase(); | ||
return repos?.filter((repoData) => | ||
repoData.repository.name.toLowerCase().includes(query) | ||
); | ||
}; | ||
const filterReposByPullRequestState = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return repos?.filter((repoData) => | ||
repoData.contributions.nodes.some( | ||
(contribution) => contribution.pullRequest.state === pullRequestState | ||
) | ||
); | ||
}; | ||
|
||
const filterRepos = (repos: PullRequestContributionsByRepository[]) => { | ||
let filteredRepos = repos; | ||
if (!searchQuery) { | ||
filteredRepos = hideOwnRepo ? filterOutOwnRepos(repos) : repos; | ||
} else { | ||
const filteredReposBySearchQuery = filterReposBySearchQuery(repos); | ||
filteredRepos = hideOwnRepo | ||
? filterOutOwnRepos(filteredReposBySearchQuery) | ||
: filteredReposBySearchQuery; | ||
} | ||
|
||
filteredRepos = pullRequestState | ||
? filterReposByPullRequestState(filteredRepos) | ||
: filteredRepos; | ||
|
||
return filteredRepos; | ||
}; | ||
/** */ | ||
|
||
//Order section | ||
const orderRepoByOwner = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return [...repos].sort((a, b) => | ||
compareArrayString(a.repository.owner.login, b.repository.owner.login) | ||
); | ||
}; | ||
const orderRepoByName = (repos: PullRequestContributionsByRepository[]) => { | ||
return [...repos].sort((a, b) => | ||
compareArrayString(a.repository.name, b.repository.name) | ||
); | ||
}; | ||
const orderRepoByCountAscending = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return [...repos].sort( | ||
(a, b) => a.contributions.totalCount - b.contributions.totalCount | ||
); | ||
}; | ||
const orderRepoByCountDescending = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return [...repos].sort( | ||
(a, b) => b.contributions.totalCount - a.contributions.totalCount | ||
); | ||
}; | ||
|
||
const orderRepos = (repos: PullRequestContributionsByRepository[]) => { | ||
let orderedRepos = repos; | ||
if (orderState === "OWNER") { | ||
orderedRepos = | ||
orderedRepos !== undefined ? orderRepoByOwner(repos) : orderedRepos; | ||
} else if (orderState === "REPOSITORY") { | ||
orderedRepos = | ||
orderedRepos !== undefined ? orderRepoByName(repos) : orderedRepos; | ||
} else if (orderState === "PRASCENDING") { | ||
orderedRepos = | ||
orderedRepos !== undefined | ||
? orderRepoByCountAscending(repos) | ||
: orderedRepos; | ||
} else if (orderState === "PRDESCENDING") { | ||
orderedRepos = | ||
orderedRepos !== undefined | ||
? orderRepoByCountDescending(repos) | ||
: orderedRepos; | ||
} | ||
|
||
return orderedRepos; | ||
}; | ||
/** */ | ||
|
||
const filterAndOrderRepos = ( | ||
repos: PullRequestContributionsByRepository[] | ||
) => { | ||
return orderRepos(filterRepos(repos)); | ||
}; | ||
|
||
return filterAndOrderRepos(repositories); | ||
}, [ | ||
repositories, | ||
searchQuery, | ||
hideOwnRepo, | ||
pullRequestState, | ||
orderState, | ||
session, | ||
]); | ||
|
||
return filteredRepositories; | ||
}; |
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
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const compareArrayString = ( | ||
itemNumberOne: string, | ||
itemNumberTwo: string | ||
) => { | ||
if (itemNumberOne < itemNumberTwo) { | ||
return -1; | ||
} | ||
if (itemNumberOne > itemNumberTwo) { | ||
return 1; | ||
} | ||
return 0; | ||
}; |