-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(profile): add memory cahce search by tag
- Loading branch information
1 parent
4112f6e
commit 642b9da
Showing
5 changed files
with
144 additions
and
53 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
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,3 @@ | ||
const useBlogHook = () => { | ||
return {}; | ||
}; |
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,91 @@ | ||
import { generateColorArray } from "@/utils"; | ||
import { checkIsFoundTag, profileHelperService } from "@/utils/profileHelper"; | ||
import { Profile } from "contentlayer/generated"; | ||
import { ChangeEvent, useCallback, useMemo, useState } from "react"; | ||
|
||
const useProfileHook = (initProfiles: Profile[], initSearchTag: string) => { | ||
const [uniqueTags, setUniqueTags] = useState<string[]>([]); | ||
const [foundProfiles, setFoundProfiles] = useState<Profile[]>([]); | ||
const [searchedTags, setSelectedTags] = useState<string[]>([]); | ||
const [uniqueTagColors, setUniqueTagColors] = useState<string[]>(); | ||
const [searchTag, setSearchTag] = useState<string>(initSearchTag); | ||
|
||
const [profiles] = useState<Profile[]>(() => { | ||
const expensiveCalculation = profileHelperService( | ||
initProfiles, | ||
initSearchTag | ||
); | ||
|
||
setUniqueTags(expensiveCalculation.uniqueTags); | ||
|
||
setFoundProfiles(expensiveCalculation.foundProfiles); | ||
|
||
setSelectedTags(expensiveCalculation.searchedTags); | ||
|
||
setUniqueTagColors( | ||
generateColorArray(expensiveCalculation.uniqueTags.length) | ||
); | ||
|
||
return initProfiles; | ||
}); | ||
|
||
const [searchByName, setSearchByName] = useState<string>(""); | ||
|
||
const handleSearchByName = useCallback((e: ChangeEvent<HTMLInputElement>) => { | ||
setSearchByName(e.target.value); | ||
}, []); | ||
|
||
const handleSearchTag = useCallback( | ||
(tagName: string) => { | ||
const isAlreadySearched = searchedTags.some((tag) => | ||
checkIsFoundTag(tagName, tag) | ||
); | ||
|
||
console.log("isAlreadySearched", isAlreadySearched); | ||
if (isAlreadySearched) { | ||
setSearchTag(""); | ||
setSelectedTags((prev) => []); | ||
setFoundProfiles((profile) => profiles); | ||
return; | ||
} | ||
|
||
setFoundProfiles((prevFoundProfiles) => { | ||
return profiles.filter((profile) => { | ||
return profile.tags?.some((tag) => checkIsFoundTag(tag, tagName)); | ||
}); | ||
}); | ||
|
||
setSelectedTags( | ||
uniqueTags.filter((tag) => checkIsFoundTag(tag, tagName)) | ||
); | ||
|
||
setSearchTag(tagName); | ||
}, | ||
[profiles, searchTag, uniqueTags] | ||
); | ||
|
||
const filteredProfiles = useMemo(() => { | ||
const isSetSearchByName = !!searchByName.trim().length; | ||
|
||
if (isSetSearchByName) { | ||
return foundProfiles.filter((profile) => | ||
profile.name.toLowerCase().includes(searchByName.toLowerCase()) | ||
); | ||
} | ||
return foundProfiles; | ||
}, [foundProfiles, searchByName]); | ||
|
||
return { | ||
searchTag, | ||
uniqueTagColors, | ||
profiles, | ||
searchByName, | ||
foundProfiles, | ||
uniqueTags, | ||
searchedTags, | ||
filteredProfiles, | ||
handleSearchByName, | ||
handleSearchTag, | ||
}; | ||
}; | ||
export default useProfileHook; |