11"use client" ;
2- import { useEffect , useState } from "react" ;
2+ import { useEffect , useRef , useState } from "react" ;
33import { Typography , Breadcrumbs , Button , Grid , Card , CardContent , Box } from "@mui/material" ;
44import FolderCopyIcon from "@mui/icons-material/FolderCopy" ;
55import TopicIcon from "@mui/icons-material/Topic" ;
@@ -13,6 +13,7 @@ import { Locale } from "../../..";
1313import type { PlaylistInterface , SermonInterface } from "@churchapps/helpers" ;
1414import { ElementInterface } from "../../helpers" ;
1515import { EnvironmentHelper } from "../../helpers/EnvironmentHelper" ;
16+ import { shareLink } from "../../helpers/shareLink" ;
1617
1718interface Props {
1819 churchId : string ;
@@ -28,6 +29,8 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
2829 const [ sermons , setSermons ] = useState < SermonInterface [ ] > ( [ ] ) ;
2930 const [ activeSermons , setActiveSermons ] = useState < SermonInterface [ ] > ( [ ] ) ;
3031 const [ activeVideo , setActiveVideo ] = useState < SermonInterface > ( ) ;
32+ const [ shareStatus , setShareStatus ] = useState < string > ( "" ) ;
33+ const shareTimer = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
3134
3235 const answers : any = element ?. answers || { } ;
3336 const layout = ( answers . layout as string ) || "browse" ;
@@ -48,6 +51,8 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
4851 } ) ;
4952 } , [ churchId ] ) ;
5053
54+ useEffect ( ( ) => ( ) => { if ( shareTimer . current ) clearTimeout ( shareTimer . current ) ; } , [ ] ) ;
55+
5156 const getFilteredData = ( id ?: string ) => {
5257 if ( ! id ) return ;
5358 const filteredData = sermons . filter ( ( item ) => item . playlistId === id ) ;
@@ -63,11 +68,29 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
6368
6469 const thumbFor = ( item : SermonInterface ) => item . thumbnail || AppearanceHelper . getLogo ( appearance , "/images/logo.png" , "/images/logo.png" , "#FFF" ) ;
6570
71+ // Uploaded sermon audio is private in S3, so downloads go through the Api redirect that signs the link.
72+ const downloadHref = ( item : SermonInterface ) => ApiHelper . getConfig ( "ContentApi" ) ?. url + "/sermons/public/download/" + churchId + "/" + item . id ;
73+
74+ const handleShare = async ( item : SermonInterface ) => {
75+ const url = window . location . origin + "/sermons/" + item . id ;
76+ const result = await shareLink ( { title : item . title || "" , text : `Check out this sermon: "${ item . title } " ${ url } ` , url } ) ;
77+ const text = result === "copied" ? Locale . label ( "sermon.linkCopied" , "Link copied" ) : result === "failed" ? Locale . label ( "sermon.unableToShare" , "Unable to share" ) : "" ;
78+ if ( ! text ) return ;
79+ setShareStatus ( text ) ;
80+ if ( shareTimer . current ) clearTimeout ( shareTimer . current ) ;
81+ shareTimer . current = setTimeout ( ( ) => setShareStatus ( "" ) , 3000 ) ;
82+ } ;
83+
6684 const renderAudio = ( item : SermonInterface ) => (
6785 < div className = "sermonAudio" >
6886 < img src = { item . thumbnail || activePlaylist ?. thumbnail || thumbFor ( item ) } alt = { item . title } loading = "lazy" />
6987 < h2 > { item . title } </ h2 >
7088 < audio controls autoPlay preload = "none" src = { item . audioUrl } data-testid = "sermon-audio" />
89+ < div className = "podcastActions" >
90+ < a className = "podcastDownload" data-testid = "sermon-download" href = { downloadHref ( item ) } aria-label = { Locale . label ( "sermon.download" , "Download" ) + " " + ( item . title || "" ) } > { Locale . label ( "sermon.download" , "Download" ) } </ a >
91+ < button type = "button" className = "podcastShare" data-testid = "sermon-share" aria-label = { Locale . label ( "sermon.share" , "Share" ) + " " + ( item . title || "" ) } onClick = { ( ) => handleShare ( item ) } > { Locale . label ( "sermon.share" , "Share" ) } </ button >
92+ { shareStatus && < span className = "podcastShareStatus" role = "status" data-testid = "sermon-share-status" > { shareStatus } </ span > }
93+ </ div >
7194 { item . description && < p > { item . description } </ p > }
7295 </ div >
7396 ) ;
0 commit comments