forked from SWYP-team-2th/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
카카오톡 링크 공유, URL 복사 로직을 작성한다. (SWYP-team-2th#114)
* 카카오톡 링크 공유, URL 복사 로직을 작성한다. * 공유하기 적용 * 로고 추가 * 테스트용 코드 추가 * og image 추가 * 썸네일 제거 * 예제 제거
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useEffect } from 'react'; | ||
import { useBottomSheet } from '@/components/common/BottomSheet/hooks'; | ||
|
||
interface KakaoShareUrlProps { | ||
author: string; | ||
shareUrl: string; | ||
} | ||
|
||
export function useKakaoShareUrl({ author, shareUrl }: KakaoShareUrlProps) { | ||
const { closeBottomSheet } = useBottomSheet(); | ||
|
||
useEffect(() => { | ||
if (window.Kakao && !window.Kakao.isInitialized()) { | ||
console.log(import.meta.env.VITE_KAKAO_JAVASCRIPT_APP_KEY); | ||
window.Kakao.init(import.meta.env.VITE_KAKAO_JAVASCRIPT_APP_KEY); | ||
} | ||
}, []); | ||
|
||
const handleClickKakaoShareButton = () => { | ||
if (!window.Kakao.isInitialized()) { | ||
console.error('카카오 SDK가 초기화되지 않았습니다.'); | ||
return; | ||
} | ||
|
||
try { | ||
window.Kakao.Share.sendDefault({ | ||
objectType: 'text', | ||
text: `${author}님이 투표를 공유했어요! 💛`, | ||
link: { | ||
mobileWebUrl: shareUrl, | ||
webUrl: shareUrl, | ||
}, | ||
}); | ||
|
||
closeBottomSheet(); | ||
} catch (error) { | ||
console.error('카카오 공유 에러:', error); | ||
} | ||
}; | ||
|
||
return { | ||
handleClickKakaoShareButton, | ||
}; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/components/common/LinkShareBottomSheet/LinkShareBottomSheet.tsx
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,36 @@ | ||
import BottomSheet from '../BottomSheet'; | ||
import Icon from '../Icon'; | ||
import useLinkShareBottomSheet from './hooks'; | ||
|
||
interface LinkShareBottomSheetProps { | ||
author: string; | ||
shareUrl: string; | ||
} | ||
|
||
export default function LinkShareBottomSheet({ | ||
author, | ||
shareUrl, | ||
}: LinkShareBottomSheetProps) { | ||
const { handleClickKakaoShareButton, handleClickUrlShareButton } = | ||
useLinkShareBottomSheet({ author, shareUrl }); | ||
|
||
return ( | ||
<BottomSheet title="투표 공유하기" hasCloseButton> | ||
<div className="flex flex-col gap-5 text-title-medium"> | ||
<button | ||
onClick={handleClickKakaoShareButton} | ||
className="flex gap-4 items-center" | ||
> | ||
<p>카카오로 공유하기</p> | ||
</button> | ||
<button | ||
onClick={handleClickUrlShareButton} | ||
className="flex gap-4 items-center" | ||
> | ||
<Icon name="Link" size="medium" /> | ||
<p>URL로 공유하기</p> | ||
</button> | ||
</div> | ||
</BottomSheet> | ||
); | ||
} |
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,42 @@ | ||
import { useBottomSheet } from '../BottomSheet/hooks'; | ||
import useToast from '../Toast/hooks'; | ||
import { useKakaoShareUrl } from '@/api/useKakaoShareUrl'; | ||
|
||
interface UseLinkShareBottomSheetOptions { | ||
author: string; | ||
shareUrl: string; | ||
} | ||
|
||
export default function useLinkShareBottomSheet({ | ||
author, | ||
shareUrl, | ||
}: UseLinkShareBottomSheetOptions) { | ||
const toast = useToast(); | ||
const { closeBottomSheet } = useBottomSheet(); | ||
const { handleClickKakaoShareButton } = useKakaoShareUrl({ | ||
author, | ||
shareUrl, | ||
}); | ||
|
||
const handleClickUrlShareButton = () => { | ||
navigator.clipboard | ||
.writeText(shareUrl) | ||
.then(() => { | ||
toast.success({ | ||
title: '투표 주소가 복사됐어요!😉', | ||
}); | ||
closeBottomSheet(); | ||
}) | ||
.catch(() => { | ||
toast.error({ | ||
title: '투표 주소 복사에 실패했어요..', | ||
description: '다시 시도해주세요.', | ||
}); | ||
}); | ||
}; | ||
|
||
return { | ||
handleClickKakaoShareButton, | ||
handleClickUrlShareButton, | ||
}; | ||
} |
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 @@ | ||
export { default } from './LinkShareBottomSheet'; |
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,16 @@ | ||
interface Window { | ||
Kakao: { | ||
Share: { | ||
sendDefault: (options: { | ||
objectType: string; | ||
text: string; | ||
link: { | ||
mobileWebUrl: string; | ||
webUrl: string; | ||
}; | ||
}) => void; | ||
}; | ||
init: (key: string) => void; | ||
isInitialized: () => boolean; | ||
}; | ||
} |