Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 검색 기능을 최적화한다 #928

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
@@ -1,9 +1,6 @@
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';

import { useState } from 'react';

import { useSearchStations } from '@hooks/tanstack-query/useSearchStations';
import { useDebounce } from '@hooks/useDebounce';

import FlexBox from '@common/FlexBox';
import Loader from '@common/Loader';
Expand All @@ -20,17 +17,8 @@ const StationSearchBar = () => {
handleCloseResult,
showStationDetails,
isFocused,
searchWord,
debouncedSearchWord,
} = useStationSearchWindow();
const [debouncedSearchWord, setDebouncedSearchWord] = useState(searchWord);

useDebounce(
() => {
setDebouncedSearchWord(searchWord);
},
[searchWord],
400
);

const {
data: searchResult,
Expand All @@ -45,6 +33,7 @@ const StationSearchBar = () => {
<label htmlFor="station-search-bar" aria-hidden>
<StyledSearch
id="station-search-bar"
name="station-search"
type="search"
role="searchbox"
placeholder="충전소명 또는 지역명을 입력해 주세요"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { markerInstanceStore } from '@stores/google-maps/markerInstanceStore';

import { useStationInfoWindow } from '@hooks/google-maps/useStationInfoWindow';
import { fetchSearchedStations } from '@hooks/tanstack-query/useSearchStations';
import { useDebounce } from '@hooks/useDebounce';
import useMediaQueries from '@hooks/useMediaQueries';

import { useNavigationBar } from '@ui/Navigator/NavigationBar/hooks/useNavigationBar';
Expand All @@ -24,20 +25,34 @@ import type { StationDetails, StationPosition } from '@type';
import StationDetailsWindow from '../../StationDetailsWindow/index';

export const useStationSearchWindow = () => {
const queryClient = useQueryClient();
const screen = useMediaQueries();

const [isFocused, setIsFocused] = useState(false);
const [searchWord, setSearchWord] = useState('');

const { renderDefaultMarker } = useMarker();
const queryClient = useQueryClient();

const [searchWord, setSearchWord] = useState('');
const [debouncedSearchWord, setDebouncedSearchWord] = useState(searchWord);

const { openLastPanel } = useNavigationBar();
const { openStationInfoWindow } = useStationInfoWindow();
const { renderDefaultMarker } = useMarker();

const screen = useMediaQueries();
useDebounce(
() => {
setDebouncedSearchWord(searchWord);
},
[searchWord],
400
);

const handleOpenResult = (
event?: MouseEvent<HTMLInputElement> | FocusEvent<HTMLInputElement>
) => {
if (event !== undefined) {
event.stopPropagation();
}

const handleOpenResult = (event: MouseEvent<HTMLInputElement> | FocusEvent<HTMLInputElement>) => {
event.stopPropagation();
setIsFocused(true);
};

Expand All @@ -47,11 +62,22 @@ export const useStationSearchWindow = () => {

const handleSubmitSearchWord = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const searchWordFromForm = new FormData(event.currentTarget).get('station-search');

if (encodeURIComponent(String(searchWordFromForm)) === searchWord) {
handleOpenResult();

return;
}

handleCloseResult();

const searchedStations = await fetchSearchedStations(searchWord);
const isSearchedStationExisting =
searchedStations !== undefined && searchedStations.stations.length > 0;

if (searchedStations !== undefined && searchedStations.stations.length > 0) {
if (isSearchedStationExisting) {
const [{ stationId, latitude, longitude }] = searchedStations.stations;
showStationDetails({ stationId, latitude, longitude });
}
Expand All @@ -66,12 +92,11 @@ export const useStationSearchWindow = () => {
openLastPanel(<StationDetailsWindow stationId={stationId} />);
}

// 지금 보여지는 화면에 검색한 충전소가 존재할 경우의 처리
if (
markerInstanceStore
.getState()
.some(({ id: cachedStationId }) => cachedStationId === stationId)
) {
const isStationInDisplayPosition = markerInstanceStore
.getState()
.some(({ id: cachedStationId }) => cachedStationId === stationId);

if (isStationInDisplayPosition) {
openStationInfoWindow(stationId);
} else {
const stationDetails = await fetch(
Expand Down Expand Up @@ -100,7 +125,7 @@ export const useStationSearchWindow = () => {
const handleChangeSearchWord = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
const searchWord = encodeURIComponent(value);

setIsFocused(true);
handleOpenResult();
setSearchWord(searchWord);
};

Expand All @@ -111,6 +136,6 @@ export const useStationSearchWindow = () => {
handleCloseResult,
showStationDetails,
isFocused,
searchWord,
debouncedSearchWord,
};
};
Loading