From 2c994cd1a7f8a9892b57aa3e5e2cab1ffb013c0b Mon Sep 17 00:00:00 2001 From: Clinton Lunn Date: Tue, 26 Nov 2024 20:00:09 -0500 Subject: [PATCH] refactor: reuse SharePageUrlButton --- .../components/SharePageURLButton.tsx | 4 +- .../maps/TileHandlers/CragContent.tsx | 7 +++- src/components/ui/ShareButton.tsx | 42 ------------------- 3 files changed, 8 insertions(+), 45 deletions(-) delete mode 100644 src/components/ui/ShareButton.tsx diff --git a/src/app/(default)/components/SharePageURLButton.tsx b/src/app/(default)/components/SharePageURLButton.tsx index 1233ef03d..43500a25b 100644 --- a/src/app/(default)/components/SharePageURLButton.tsx +++ b/src/app/(default)/components/SharePageURLButton.tsx @@ -10,7 +10,9 @@ import { ControlledTooltip } from '@/components/ui/Tooltip' */ export const SharePageURLButton: React.FC<{ path: string, name: string }> = ({ path, name }) => { const slug = getFriendlySlug(name) - const url = `https://openbeta.io/${path}/${slug}` + const baseUrl = process.env.NEXT_PUBLIC_BASE_URL != null ? process.env.NEXT_PUBLIC_BASE_URL : 'http://localhost:3000' + const optionalSlug = slug !== '' ? `/${slug}` : '' + const url = `${baseUrl}${path}${optionalSlug}` const [clicked, setClicked] = useState(false) diff --git a/src/components/maps/TileHandlers/CragContent.tsx b/src/components/maps/TileHandlers/CragContent.tsx index 4ff46a907..4363d6323 100644 --- a/src/components/maps/TileHandlers/CragContent.tsx +++ b/src/components/maps/TileHandlers/CragContent.tsx @@ -4,11 +4,14 @@ import { getAreaPageFriendlyUrl } from '@/js/utils' import { EntityIcon } from '@/app/(default)/editArea/[slug]/general/components/AreaItem' import { BaseDrawerContent } from './Drawer' import { MiniCarousel } from '../CardGallery' -import { ShareButton } from '../../ui/ShareButton' +import { SharePageURLButton } from '@/app/(default)/components/SharePageURLButton' +import { usePathname } from 'next/navigation' export const CragDrawerContent: React.FC = ({ id, areaName, climbs, content: { description }, media }) => { const friendlyUrl = getAreaPageFriendlyUrl(id, areaName) const editUrl = `/editArea/${id}/general` + const pathname = `${usePathname()}${window.location.search}` + return ( <> = ({ id, areaNam heading={{areaName}} subheading={} cta={Edit area} - share={} + share={} >
{description == null || description.trim() === '' diff --git a/src/components/ui/ShareButton.tsx b/src/components/ui/ShareButton.tsx deleted file mode 100644 index b43bf0010..000000000 --- a/src/components/ui/ShareButton.tsx +++ /dev/null @@ -1,42 +0,0 @@ -'use client' - -import { Share } from '@phosphor-icons/react' -import { usePathname, useSearchParams } from 'next/navigation' -import React from 'react' -import { toast } from 'react-toastify' - -const ShareButton: React.FC = () => { - const pathname = usePathname() - const searchParams = useSearchParams() - - const handleShareClick = (): void => { - const fullUrl = `${window.location.origin}${pathname}${searchParams.toString() !== '' ? '?' + searchParams.toString() : ''}` - - if (typeof navigator.clipboard === 'undefined') { - toast.error('Clipboard API is not supported in your browser') - return - } - - navigator.clipboard.writeText(fullUrl) - .then(() => { - toast.success('URL copied to clipboard!') - }) - .catch((error) => { - console.error('Error copying to clipboard:', error) - toast.error('Failed to copy URL to clipboard') - }) - } - - return ( - - ) -} - -export { ShareButton }