This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
-
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.
- Loading branch information
Showing
5 changed files
with
304 additions
and
215 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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// hooks/useRepositoryFilters.ts | ||
import { useState, useMemo } from "react"; | ||
import { sortByKeyAscending, sortByKeyDescending } from "@/lib/utils/sort"; | ||
|
||
type SetSelectedFunction = (value: string) => void; | ||
|
||
export const useRepositoryFilters = (repos: GitHubRepo[]) => { | ||
const [sort, setSort] = useState("Stars Descending"); | ||
const [filterValue, setFilterValue] = useState(""); | ||
const [selectedTopic, setSelectedTopic] = useState(""); | ||
const [selectedLanguage, setSelectedLanguage] = useState(""); | ||
const [selectedFilter, setSelectedFilter] = useState("All"); | ||
const [selectedLicense, setSelectedLicense] = useState(""); | ||
|
||
const handleFilterClick = ( | ||
value: string, | ||
setSelectedFunction: SetSelectedFunction, | ||
selectedValue: string, | ||
): void => { | ||
setSelectedFunction(value); | ||
if (value === selectedValue) { | ||
setSelectedFunction(""); | ||
} | ||
}; | ||
|
||
const filteredAndSortedRepos = useMemo(() => { | ||
const filteredRepos = repos | ||
? repos.filter((repo: any) => { | ||
if (selectedTopic) { | ||
return repo.topics.includes(selectedTopic); | ||
} | ||
if (selectedLanguage) { | ||
return repo.language === selectedLanguage; | ||
} | ||
if (selectedLicense) { | ||
return repo.license?.spdx_id === selectedLicense; | ||
} | ||
|
||
const nameMatches = repo.name | ||
.toLowerCase() | ||
.includes(filterValue.toLowerCase()); | ||
const isForked = repo.fork; | ||
const isNotForked = !repo.fork; | ||
|
||
switch (selectedFilter) { | ||
case "All": | ||
return nameMatches; | ||
case "Forked": | ||
return nameMatches && isForked; | ||
case "Not Forked": | ||
return nameMatches && isNotForked; | ||
default: | ||
return nameMatches; | ||
} | ||
}) | ||
: []; | ||
|
||
switch (sort) { | ||
case "Created Ascending": | ||
return sortByKeyAscending(filteredRepos, "created_at"); | ||
case "Created Descending": | ||
return sortByKeyDescending(filteredRepos, "created_at"); | ||
case "Updated Ascending": | ||
return sortByKeyAscending(filteredRepos, "pushed_at"); | ||
case "Updated Descending": | ||
return sortByKeyDescending(filteredRepos, "pushed_at"); | ||
case "Stars Ascending": | ||
return sortByKeyAscending(filteredRepos, "stargazers_count"); | ||
default: | ||
return sortByKeyDescending(filteredRepos, "stargazers_count"); | ||
} | ||
}, [ | ||
repos, | ||
sort, | ||
selectedTopic, | ||
selectedLanguage, | ||
selectedLicense, | ||
filterValue, | ||
selectedFilter, | ||
]); | ||
|
||
return { | ||
sort, | ||
setSort, | ||
filterValue, | ||
setFilterValue, | ||
selectedTopic, | ||
setSelectedTopic, | ||
selectedLanguage, | ||
setSelectedLanguage, | ||
selectedFilter, | ||
setSelectedFilter, | ||
selectedLicense, | ||
setSelectedLicense, | ||
handleFilterClick, | ||
filteredAndSortedRepos, | ||
}; | ||
}; |
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
Oops, something went wrong.
1d79cee
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.
Successfully deployed to the following URLs:
github-profile-viewer – ./
github-profile-viewer-sametcn99.vercel.app
github-profile-viewer-git-master-sametcn99.vercel.app
next-github-profile-viewer.vercel.app
githubprofileviewer.com
www.githubprofileviewer.com