import React, { useState } from 'react' import { Dialog, DialogTitle, DialogContent, DialogHeader, DialogTrigger, DialogDescription } from '../ui/dialog'; import { Button, ButtonProps } from '../ui/button'; import { DropdownMenuItem } from '../ui/dropdown-menu'; import { ShareLinkFilters } from '@/types/shareLink'; import { generateShareLink } from '@/handlers/api/shareLink.handler'; import { createShareKey, getShareKey } from '@/handlers/api/share-key.handler'; import { Input } from '../ui/input'; import { Label } from '@radix-ui/react-label'; import { Switch } from '../ui/switch'; import { Select, SelectValue, SelectContent, SelectItem, SelectTrigger } from '../ui/select'; interface ShareAssetsTriggerProps { filters: ShareLinkFilters buttonProps?: ButtonProps asDropdownItem?: boolean children?: React.ReactNode } export default function ShareAssetsTrigger({ filters, buttonProps, asDropdownItem, children }: ShareAssetsTriggerProps) { const [generatedLink, setGeneratedLink] = useState(null); const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const [copied, setCopied] = useState(false); const [config, setConfig] = useState>({ expiresIn: "never" }); const [endpointConfigured, setEndpointConfigured] = useState(null); const [shareKeyExists, setShareKeyExists] = useState(null); const [creatingKey, setCreatingKey] = useState(false); const [keyError, setKeyError] = useState(null); const checkShareKey = async () => { const data = await getShareKey(); setEndpointConfigured(data.endpointConfigured); setShareKeyExists(data.exists); }; const handleCreateKey = async () => { setCreatingKey(true); setKeyError(null); try { await createShareKey(); setShareKeyExists(true); } catch (err: any) { setKeyError(err.message ?? 'Failed to create share key'); } setCreatingKey(false); }; const handleReset = () => { setConfig({ expiresIn: "never" }); setGeneratedLink(null); } const handleGenerate = async () => { setLoading(true); setErrorMessage(null); return generateShareLink({ ...filters, ...config }).then(({ link }) => { setGeneratedLink(link); }).catch((err) => { setErrorMessage(err.message); }).finally(() => { setLoading(false); }); } const handleCopy = async () => { if (!generatedLink) return; try { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(generatedLink); } else { const textArea = document.createElement('textarea'); textArea.value = generatedLink; textArea.style.position = 'fixed'; textArea.style.left = '-9999px'; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); } setCopied(true); setTimeout(() => { setCopied(false); }, 2000); } catch { setErrorMessage('Failed to copy link'); } } return ( { if (open) checkShareKey(); }}> {asDropdownItem ? ( e.preventDefault()} className="gap-2"> {children} ) : ( )} Share Assets Generate a share link for the selected assets (either bunch of albums or bunch of people) and share it with your friends and family. {endpointConfigured === false && (

POWER_TOOLS_ENDPOINT_URL is not set. Add it to your environment variables to enable share links.

)} {endpointConfigured && shareKeyExists === false && (

Serving thumbnails on share links requires a dedicated API key. Create one now with minimal permissions.

{keyError &&

{keyError}

}
)} {errorMessage &&
{errorMessage}
} {generatedLink ?

This is a stateless link, it will not work if you leave the page. Which means it cannot be expired.

: (

Show the list of people in the shared photos

setConfig({ ...config, p: !!checked })} />

Should the link expire after the selected time

)}
) }