Skip to content

Commit

Permalink
feat: add deeplinked search
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 4, 2023
1 parent 83a64c6 commit b75dbdc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/components/form/SearchInput.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ from 'lodash';
import React from 'react';
import React, { useRef, useEffect } from 'react';
import { useIntl } from 'react-intl';
import { useSelector } from 'react-redux';
import styled, { withTheme } from 'styled-components';
import { useQueryParamState } from '../hooks/useQueryParamState';

import { MagnifyGlassIcon, MagnifyGlassIconWhite } from '../icons';

Expand Down Expand Up @@ -89,7 +90,10 @@ const ButtonSearchText = styled('p')`
align-items: center;
font-size: 1rem;
width: 3.125rem;
color: ${props => (props.usePrimaryButton ? props.theme.colors.default.onButton : props.theme.colors.default.onSurface)};
color: ${props =>
props.usePrimaryButton
? props.theme.colors.default.onButton
: props.theme.colors.default.onSurface};
box-sizing: border-box;
`;

Expand All @@ -104,11 +108,24 @@ const SearchInput = withTheme(
disabled = false,
}) => {
const intl = useIntl();
const ref = useRef();
const appStore = useSelector(state => state.app);
const [searchQuery] = useQueryParamState('search');

useEffect(() => {
console.log(searchQuery);
onChange({
target: {
value: searchQuery,
},
});
ref.current.value = searchQuery;
}, [searchQuery]);

return (
<SearchContainer usePrimaryButton={usePrimaryButton} size={size}>
<Input
ref={ref}
type="text"
selectedTheme={appStore.theme}
placeholder={intl.formatMessage({ id: 'search' })}
Expand All @@ -124,7 +141,8 @@ const SearchInput = withTheme(
selectedTheme={appStore.theme}
usePrimaryButton={usePrimaryButton}
buttonText={buttonText}
disabled={disabled}>
disabled={disabled}
>
{(buttonText && (
<ButtonSearchText usePrimaryButton={usePrimaryButton}>
{buttonText}
Expand Down
37 changes: 37 additions & 0 deletions src/components/hooks/useQueryParamState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import _ from 'lodash';
import { useState, useCallback, useMemo } from 'react';
import { useLocation } from 'react-router-dom';

const useQueryParamState = (name, defaultValue) => {
const [param, setParam] = useState();
const location = useLocation();

const setQueryStringParameter = useCallback(
value => {
const [base, hashFragment] = window.location.hash.split('?');
let params = new URLSearchParams(hashFragment);

if (_.isNil(value) || value === '') {
params.delete(name);
} else {
params.set(name, value);
}

const newHash = params.toString() ? `${base}?${params}` : base;

window.location.hash = decodeURIComponent(newHash);
setParam(value);
},
[name],
);

const value = useMemo(() => {
const [, hashFragment] = window.location.hash.split('?');
const params = new URLSearchParams(hashFragment);
return params.get(name) || defaultValue || '';
}, [location, param, name]);

return [value, setQueryStringParameter];
};

export { useQueryParamState };

0 comments on commit b75dbdc

Please sign in to comment.