Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

VideoPress: minor TS enhancement in the useSearchParams() hook
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import { useLocation, useHistory } from 'react-router-dom';

type SearchParamNameProp = 'page' | 'q';

export const useSearchParams = () => {
const location = useLocation();
const history = useHistory();
Expand All @@ -11,31 +13,31 @@ export const useSearchParams = () => {
/**
* Gets a given parameter from the search query.
*
* @param {string} parameterName - The name of the parameter to get from the query string.
* @param {string} defaultValue - The default value to return if the given parameter is not set on the query string.
* @returns {string} - The value of the parameter if it's set. The defaultValue if the parameter is not set.
* @param {SearchParamNameProp} parameterName - The name of the parameter to get from the query string.
* @param {string} defaultValue - The default value to return if the given parameter is not set on the query string.
* @returns {string|null} The value of the parameter if it's set. The defaultValue if the parameter is not set.
*/
const getParam = ( parameterName: string, defaultValue: string = null ) => {
const getParam = ( parameterName: SearchParamNameProp, defaultValue: string = null ): string => {
return searchParams.has( parameterName ) ? searchParams.get( parameterName ) : defaultValue;
};

/**
* Sets a given parameter on the search query data, but does not refresh the URL.
*
* @param {string} parameterName - The name of the parameter to set on the query string.
* @param {string} value - The value to be set for the parameter on the query string.
* @param {SearchParamNameProp} parameterName - The name of the parameter to set on the query string.
* @param {string} value - The value to be set for the parameter on the query string.
*/
const setParam = ( parameterName: string, value: string = null ) => {
const setParam = ( parameterName: SearchParamNameProp, value: string = null ) => {
searchParams.set( parameterName, value );
};

/**
* Deletes a given parameter from the search query data, which results on removing
* it from the URL when it gets updated.
*
* @param {string} parameterName - The name of the parameter to delete.
* @param {SearchParamNameProp} parameterName - The name of the parameter to delete.
*/
const deleteParam = ( parameterName: string ) => {
const deleteParam = ( parameterName: SearchParamNameProp ) => {
searchParams.delete( parameterName );
};

Expand Down