mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-06 04:26:13 +01:00
Add share button
This commit is contained in:
@@ -42,7 +42,7 @@ export default async function GroupLayout({
|
|||||||
|
|
||||||
<div className="flex gap-2 justify-between">
|
<div className="flex gap-2 justify-between">
|
||||||
<GroupTabs groupId={groupId} />
|
<GroupTabs groupId={groupId} />
|
||||||
<ShareButton groupId={groupId} />
|
<ShareButton group={group} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { CopyButton } from '@/components/copy-button'
|
import { CopyButton } from '@/components/copy-button'
|
||||||
|
import { ShareUrlButton } from '@/components/share-url-button'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { Input } from '@/components/ui/input'
|
import { Input } from '@/components/ui/input'
|
||||||
import {
|
import {
|
||||||
@@ -7,14 +8,15 @@ import {
|
|||||||
PopoverTrigger,
|
PopoverTrigger,
|
||||||
} from '@/components/ui/popover'
|
} from '@/components/ui/popover'
|
||||||
import { env } from '@/lib/env'
|
import { env } from '@/lib/env'
|
||||||
|
import { Group } from '@prisma/client'
|
||||||
import { Share } from 'lucide-react'
|
import { Share } from 'lucide-react'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
groupId: string
|
group: Group
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ShareButton({ groupId }: Props) {
|
export function ShareButton({ group }: Props) {
|
||||||
const url = `${env.NEXT_PUBLIC_BASE_URL}/groups/${groupId}`
|
const url = `${env.NEXT_PUBLIC_BASE_URL}/groups/${group.id}`
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popover>
|
<Popover>
|
||||||
@@ -31,6 +33,10 @@ export function ShareButton({ groupId }: Props) {
|
|||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Input className="flex-1" defaultValue={url} readOnly />
|
<Input className="flex-1" defaultValue={url} readOnly />
|
||||||
<CopyButton text={url} />
|
<CopyButton text={url} />
|
||||||
|
<ShareUrlButton
|
||||||
|
text={`Join my group ${group.name} on Spliit`}
|
||||||
|
url={url}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
<strong>Warning!</strong> Every person with the group URL will be able
|
<strong>Warning!</strong> Every person with the group URL will be able
|
||||||
|
|||||||
44
src/components/share-url-button.tsx
Normal file
44
src/components/share-url-button.tsx
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { Button } from '@/components/ui/button'
|
||||||
|
import { Share } from 'lucide-react'
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
text: string
|
||||||
|
url: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ShareUrlButton({ url, text }: Props) {
|
||||||
|
const canShare = useCanShare(url, text)
|
||||||
|
if (!canShare) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="icon"
|
||||||
|
variant="secondary"
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
if (navigator.share) {
|
||||||
|
navigator.share({ text, url })
|
||||||
|
} else {
|
||||||
|
console.log('Sharing is not available', { text, url })
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Share className="w-4 h-4" />
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function useCanShare(url: string, text: string) {
|
||||||
|
const [canShare, setCanShare] = useState<boolean | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCanShare(
|
||||||
|
navigator.share !== undefined && navigator.canShare({ url, text }),
|
||||||
|
)
|
||||||
|
}, [text, url])
|
||||||
|
|
||||||
|
return canShare
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user