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

feat: implement share feature #67

Merged
merged 1 commit into from
Apr 21, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

certificates
41 changes: 40 additions & 1 deletion src/components/common/navigation-bar/navigation-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
"use client";
import React from "react";
import Logo from "../logo/logo";
import Link from "next/link";
import { usePathname } from "next/navigation";
import Toast from "@/components/common/toast/toast";
import { toast } from "sonner";

const ShareData = {
title: "Payout",
text: "Self-check Your Dividend Portfolio",
};

function validateReportUrl(url: string) {
const pattern = /^\/report\/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/;
return pattern.test(url);
}

const NavigationBar = () => {
const pathname = usePathname();

const isShareSupported = () => {
// navigator.canShare()는 옵션 데이터가 제공되면 그 데이터를 검사합니다.
return navigator.canShare && navigator.canShare({ ...ShareData, url: window.location.href });
};

const handleShareClick = async () => {
try {
if (isShareSupported()) {
navigator.share({ ...ShareData, url: window.location.href });
} else {
await navigator.clipboard.writeText(window.location.href);
toast.custom((t) => <Toast t={t} title={`copied`} />);
}
} catch (e) {
toast.custom((t) => <Toast t={t} title={`Sharing not supported or data not shareable.`} />);
}
};

return (
<div className="flex h-11 w-full shrink-0 items-center justify-start bg-transparent px-5">
<div className="flex h-11 w-full shrink-0 items-center justify-between bg-transparent px-5">
<Link href="/">
<Logo />
</Link>
{validateReportUrl(pathname) && (
<div onClick={handleShareClick} className="cursor-pointer text-body3 text-grey-900">
Share
</div>
)}
</div>
);
};
Expand Down
Loading