Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: some search params is not preserved on search (#86)
- Loading branch information
Showing
10 changed files
with
68 additions
and
47 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
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
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,23 +1,48 @@ | ||
import { useRouter } from "next/navigation"; | ||
import { useCallback } from "react"; | ||
|
||
import { MASTER_VERSION_SEARCH_PARAM_NAME, SHINRYOUKOUI_MASTER_VERSION_KEYS } from "@/features/shinryoukoui-master-versions/constants"; | ||
import { SEARCH_PARAM_NAMES } from "@/search-param-names"; | ||
|
||
export type SearchParamNames = typeof SEARCH_PARAM_NAMES["SEARCH"][keyof typeof SEARCH_PARAM_NAMES["SEARCH"]]; | ||
|
||
type Options = { | ||
preserveExtraSearchParams: boolean | ||
} | ||
|
||
export function useShinryoukouiSearch() { | ||
export function useShinryoukouiSearch({ | ||
preserveExtraSearchParams, | ||
}: Options) { | ||
const { push } = useRouter(); | ||
|
||
return useCallback((query: string) => { | ||
const currentSearchParams = new URLSearchParams(location.search); | ||
const masterVersion = currentSearchParams.get(MASTER_VERSION_SEARCH_PARAM_NAME); | ||
return useCallback((params: { | ||
[key in SearchParamNames]?: string | undefined | ||
}) => { | ||
const currentSearchParams = new URLSearchParams(window.location.search); | ||
const searchParams = new URLSearchParams(); | ||
if (query) { | ||
searchParams.set("q", query); | ||
} | ||
if (masterVersion && SHINRYOUKOUI_MASTER_VERSION_KEYS.includes(masterVersion)) { | ||
searchParams.set(MASTER_VERSION_SEARCH_PARAM_NAME, masterVersion); | ||
} | ||
|
||
const searchParamNames = Object.values(SEARCH_PARAM_NAMES.SEARCH); | ||
currentSearchParams.forEach((value, key) => { | ||
if (preserveExtraSearchParams || searchParamNames.includes(key as SearchParamNames)) { | ||
searchParams.set(key, value); | ||
} | ||
}); | ||
|
||
Object.entries(params).forEach(([key, value]) => { | ||
if (value) { | ||
searchParams.set(key, value); | ||
} else { | ||
searchParams.delete(key); | ||
} | ||
}); | ||
push(`/s?${searchParams.toString()}`); | ||
}, [push]); | ||
}, [preserveExtraSearchParams, push]); | ||
} | ||
|
||
export function useShinryoukouiSearchByQuery() { | ||
const search = useShinryoukouiSearch({preserveExtraSearchParams: false}); | ||
return useCallback((q: string) => { | ||
search({ | ||
q, | ||
}); | ||
}, [search]); | ||
} |
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,7 @@ | ||
export const SEARCH_PARAM_NAMES = { | ||
FIELDS: "fields", | ||
}; | ||
SEARCH: { | ||
QUERY: "q", | ||
MASTER_VERSION: "v", | ||
FIELDS: "fields", | ||
} | ||
} as const; |