From 0bd9a170098e1b328a6e5c15c98a06b67ad99b39 Mon Sep 17 00:00:00 2001 From: SleeplessOne1917 Date: Sun, 21 May 2023 18:17:56 +0000 Subject: [PATCH] Add web share for browsers that have it enabled (#1029) Co-authored-by: Dessalines --- src/assets/symbols.svg | 7 +++++++ src/shared/components/post/post-listing.tsx | 23 ++++++++++++++++++++- src/shared/utils.ts | 10 +++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/assets/symbols.svg b/src/assets/symbols.svg index 2fb8eac..72214ea 100644 --- a/src/assets/symbols.svg +++ b/src/assets/symbols.svg @@ -251,5 +251,12 @@ + + + + + + + diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index cfaa81e..fd9b883 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -22,7 +22,7 @@ import { SavePost, TransferCommunity, } from "lemmy-js-client"; -import { getExternalHost } from "../../env"; +import { getExternalHost, getHttpBase } from "../../env"; import { i18n } from "../../i18next"; import { BanType, PostFormParams, PurgeType } from "../../interfaces"; import { UserService, WebSocketService } from "../../services"; @@ -32,6 +32,7 @@ import { amMod, canAdmin, canMod, + canShare, futureDaysToUnixTime, hostname, isAdmin, @@ -46,6 +47,7 @@ import { numToSI, relTags, setupTippy, + share, showScores, wsClient, } from "../../utils"; @@ -567,9 +569,19 @@ export class PostListing extends Component { commentsLine(mobile = false) { let post = this.props.post_view.post; + return (
{this.commentsButton} + {canShare() && ( + + )} {!post.local && ( { this.setState({ showEdit: false }); } + handleShare(i: PostListing) { + const { name, body, id } = i.props.post_view.post; + share({ + title: name, + text: body?.slice(0, 50), + url: `${getHttpBase()}/post/${id}`, + }); + } + handleShowReportDialog(i: PostListing) { i.setState({ showReportDialog: !i.state.showReportDialog }); } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index b5b39c4..2e18e2b 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -1593,3 +1593,13 @@ export function isAuthPath(pathname: string) { pathname ); } + +export function canShare() { + return isBrowser() && !!navigator.canShare; +} + +export function share(shareData: ShareData) { + if (isBrowser()) { + navigator.share(shareData); + } +} -- 2.44.1