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

NEW : 클립보드 복사 기능 추가 #92

Merged
merged 1 commit into from
Jan 4, 2024
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
29 changes: 25 additions & 4 deletions client/src/components/post/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import "../newpost/quill.mention.css";
import { sanitize } from "isomorphic-dompurify";
import UserAvatar from "../user/info/UserAvatar";
import Link from "next/link";
import { USER_PAGE } from "@/const/clientPath";
import { POST_DETAIL, USER_PAGE } from "@/const/clientPath";
import { useMyInfoQuery } from "@/queries/auth/useMyInfoQuery";
import PostCardOptionDropdown from "./PostCardOptionDropdown";
import { postcardContext } from "@/store/post/PostCardContext";
import { useRouter } from "next/navigation";
import formatTime from "@/utils/formatTime";
import copyToClipboard from "@/utils/copyToClipboard";
import { useGlobalSnackbarStore } from "@/store/useGlobalSnackbarStore";

const PostCard = ({
postAttachUrls,
Expand Down Expand Up @@ -65,8 +66,24 @@ const PostCard = ({
[currentUser]
);

const CLIENT_BASE_URL = process.env.NEXT_PUBLIC_CLIENT_BASE_URL;
const fireToast = useGlobalSnackbarStore(({ fireToast }) => fireToast);
const copyToClipboardHander = async () => {
await copyToClipboard(
`${CLIENT_BASE_URL}${POST_DETAIL(id, String(postNo))}`,
{
onSuccess: () => {
fireToast("게시물 주소가 복사되었습니다");
},
onError: () => {
fireToast("게시물 주소 복사에 실패했습니다");
},
}
);
};

return (
<Card sx={{ display: "flex", gap: 2, py:2 }} elevation={0}>
<Card sx={{ display: "flex", gap: 2, py: 2 }} elevation={0}>
<Link href={USER_PAGE(createdBy)}>
<UserAvatar
src={profileImgUrls[0]?.attachUrl}
Expand Down Expand Up @@ -169,7 +186,11 @@ const PostCard = ({
</Box>
<Typography variant="label">{likeCount ?? 0}</Typography>
</ButtonBase>
<ButtonBase data-testid="shareBtn" aria-label="share">
<ButtonBase
data-testid="shareBtn"
aria-label="share"
onClick={copyToClipboardHander}
>
<ShareIcon />
<Typography variant="label">공유</Typography>
</ButtonBase>
jobkaeHenry marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
29 changes: 29 additions & 0 deletions client/src/utils/copyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 브라우저 클립보드에 Text 를 복사시키는 핸들러, HTTPS || Localhost 에서만 동작
* @param content 복사시킬 내용
* @param onError 에러시 사용할 함수
* @param onSuccess 성공시 사용할 함수
* @param onSettle 성공/실패 여부에 관계없이 완료후 사용할 함수
*/

interface CopyToClipboardOptions {
onError?: (err: unknown) => void;
onSuccess?: () => void;
onSettle?: () => void;
}

const copyToClipboard = async (
content: string,
{ onError, onSettle, onSuccess }: CopyToClipboardOptions
) => {
try {
await navigator.clipboard.writeText(content);
onSuccess && onSuccess();
} catch (err) {
onError && onError(err);
} finally {
onSettle && onSettle();
}
};

export default copyToClipboard;
jobkaeHenry marked this conversation as resolved.
Show resolved Hide resolved