-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: 지도 관련 변수들을 한번에 생성하는 useCustomMap 생성
- Loading branch information
Showing
3 changed files
with
76 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { EventCardType } from "@/types/index"; | ||
|
||
const useCustomMap = () => { | ||
const [toggleTab, setToggleTab] = useState(true); | ||
const [selectedCard, setSelectedCard] = useState<EventCardType | null>(null); | ||
|
||
const handleCardClick = (select: EventCardType) => { | ||
setSelectedCard(select.id === selectedCard?.id ? null : select); | ||
}; | ||
|
||
const handleButtonClick = () => { | ||
setToggleTab((prev) => !prev); | ||
|
||
if (toggleTab) { | ||
setSelectedCard(null); | ||
} | ||
}; | ||
|
||
const scrollRef = useRef<HTMLDivElement>(); | ||
|
||
const scrollRefCallback = (el: HTMLDivElement) => { | ||
scrollRef.current = el; | ||
}; | ||
|
||
useEffect(() => { | ||
scrollRef.current?.scrollIntoView({ behavior: "smooth", block: "nearest" }); | ||
}, [selectedCard?.id, toggleTab]); | ||
|
||
const mapVar = { | ||
toggleTab, | ||
setToggleTab, | ||
selectedCard, | ||
setSelectedCard, | ||
}; | ||
|
||
const mapCallback = { | ||
handleCardClick, | ||
handleButtonClick, | ||
scrollRefCallback, | ||
}; | ||
|
||
return { mapVar, mapCallback }; | ||
}; | ||
|
||
export default useCustomMap; |