diff --git a/packages/shared/src/components/ShareBar.spec.tsx b/packages/shared/src/components/ShareBar.spec.tsx index 7daee6962c2..d257fbf0fac 100644 --- a/packages/shared/src/components/ShareBar.spec.tsx +++ b/packages/shared/src/components/ShareBar.spec.tsx @@ -133,6 +133,20 @@ describe('ShareBar Test Suite:', () => { ); }); + it('should open the share to squad composer with the wrapped post when sharing a share without commentary', async () => { + const post = { ...sharePost, title: undefined }; + renderComponent(true, squads, post); + const btn = await screen.findByTestId(`social-share-@${squads[0].handle}`); + + btn.click(); + await waitFor(() => { + expect(screen.getByRole('textbox', { name: 'Link URL' })).toHaveValue( + post.sharedPost!.permalink, + ); + }); + expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument(); + }); + it('should render the Facebook button and navigate to correct link', async () => { renderComponent(); const btn = await screen.findByTestId(`social-share-Facebook`); diff --git a/packages/shared/src/components/ShareBar.tsx b/packages/shared/src/components/ShareBar.tsx index 0260faa68d9..71753a3c8b2 100644 --- a/packages/shared/src/components/ShareBar.tsx +++ b/packages/shared/src/components/ShareBar.tsx @@ -3,6 +3,7 @@ import React, { useContext, useMemo, useState } from 'react'; import classNames from 'classnames'; import { CopyIcon, FacebookIcon, TwitterIcon, WhatsappIcon } from './icons'; import type { Post } from '../graphql/posts'; +import { getPostToShare } from '../graphql/posts'; import { useCopyPostLink } from '../hooks/useCopyPostLink'; import { getShareLink, ShareProvider } from '../lib/share'; import { useLogContext } from '../contexts/LogContext'; @@ -85,11 +86,12 @@ export default function ShareBar({ const onShareToSquad = (squad: Squad) => { logEvent(postLogEvent(LogEvent.StartShareToSquad, post)); + const postToShare = getPostToShare(post); openModal({ type: LazyModal.SmartComposer, props: { - initialUrl: post.permalink, - preview: post, + initialUrl: postToShare.permalink, + preview: postToShare, initialSquadId: squad.id, onPosted: () => logEvent(postLogEvent(LogEvent.ShareToSquad, post)), }, diff --git a/packages/shared/src/components/modals/ShareModal.spec.tsx b/packages/shared/src/components/modals/ShareModal.spec.tsx index 485abdd3c49..f09123148b1 100644 --- a/packages/shared/src/components/modals/ShareModal.spec.tsx +++ b/packages/shared/src/components/modals/ShareModal.spec.tsx @@ -5,7 +5,7 @@ import React from 'react'; import nock from 'nock'; import { useRouter } from 'next/router'; import ShareModal from './ShareModal'; -import Post from '../../../__tests__/fixture/post'; +import Post, { sharePost } from '../../../__tests__/fixture/post'; import { getFacebookShareLink } from '../../lib/share'; import { Origin } from '../../lib/log'; import Comment from '../../../__tests__/fixture/comment'; @@ -38,6 +38,7 @@ const renderComponent = ( loggedIn = true, hasSquads = true, comment?: typeof defaultComment, + post = defaultPost, ): RenderResult => { return render( { ); }); + it('should open the share to squad composer with the wrapped post when sharing a share without commentary', async () => { + const post = { ...sharePost, title: undefined, private: false }; + renderComponent(true, true, undefined, post); + const btn = await screen.findByTestId(`social-share-@${squads[0].handle}`); + fireEvent.click(btn); + expect( + await screen.findByRole('textbox', { name: 'Link URL' }), + ).toHaveValue(post.sharedPost!.permalink); + expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument(); + }); + it('should render the Facebook button and navigate to the correct link', async () => { renderComponent(false); const btn = await screen.findByTestId(`social-share-Facebook`); diff --git a/packages/shared/src/components/post/focus/DiscussionShareRow.spec.tsx b/packages/shared/src/components/post/focus/DiscussionShareRow.spec.tsx index b0b627669a6..81135c64726 100644 --- a/packages/shared/src/components/post/focus/DiscussionShareRow.spec.tsx +++ b/packages/shared/src/components/post/focus/DiscussionShareRow.spec.tsx @@ -3,7 +3,7 @@ import { render, screen, waitFor } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import React from 'react'; import nock from 'nock'; -import Post from '../../../../__tests__/fixture/post'; +import Post, { sharePost } from '../../../../__tests__/fixture/post'; import loggedUser from '../../../../__tests__/fixture/loggedUser'; import { generateTestSquad } from '../../../../__tests__/fixture/squads'; import { settingsContext } from '../../../../__tests__/helpers/boot'; @@ -48,6 +48,7 @@ const squads = Array.from({ length: 6 }, (_, index) => const renderComponent = ( withSquads = true, integrations?: UserIntegration[], + post = defaultPost, ): RenderResult => { const client = new QueryClient(); @@ -72,7 +73,7 @@ const renderComponent = ( - + @@ -96,6 +97,20 @@ describe('DiscussionShareRow', () => { ); }); + it('seeds the composer with the wrapped post when the post is a share without commentary', async () => { + const post = { ...sharePost, title: undefined }; + renderComponent(true, undefined, post); + + screen.getByRole('button', { name: 'Share to Web team 0' }).click(); + + await waitFor(() => { + expect(screen.getByRole('textbox', { name: 'Link URL' })).toHaveValue( + post.sharedPost!.permalink, + ); + }); + expect(screen.getByText(post.sharedPost!.title)).toBeInTheDocument(); + }); + it('caps the inline squads at four', () => { renderComponent(); diff --git a/packages/shared/src/components/post/focus/DiscussionShareRow.tsx b/packages/shared/src/components/post/focus/DiscussionShareRow.tsx index c9107c3beeb..bfed0a74908 100644 --- a/packages/shared/src/components/post/focus/DiscussionShareRow.tsx +++ b/packages/shared/src/components/post/focus/DiscussionShareRow.tsx @@ -2,6 +2,7 @@ import type { ReactElement } from 'react'; import React from 'react'; import classNames from 'classnames'; import type { Post } from '../../../graphql/posts'; +import { getPostToShare } from '../../../graphql/posts'; import type { Squad } from '../../../graphql/sources'; import { Button } from '../../buttons/Button'; import { ButtonSize, ButtonVariant } from '../../buttons/common'; @@ -71,11 +72,12 @@ export const DiscussionShareRow = ({ const onShareToSquad = (squad: Squad) => { logEvent(postLogEvent(LogEvent.StartShareToSquad, post)); + const postToShare = getPostToShare(post); openModal({ type: LazyModal.SmartComposer, props: { - initialUrl: post.permalink, - preview: post, + initialUrl: postToShare.permalink, + preview: postToShare, initialSquadId: squad.id, onPosted: () => logEvent(postLogEvent(LogEvent.ShareToSquad, post)), }, diff --git a/packages/shared/src/components/widgets/SocialShare.tsx b/packages/shared/src/components/widgets/SocialShare.tsx index ccde425288b..96cbb9de917 100644 --- a/packages/shared/src/components/widgets/SocialShare.tsx +++ b/packages/shared/src/components/widgets/SocialShare.tsx @@ -2,6 +2,7 @@ import type { Dispatch, ReactElement, SetStateAction } from 'react'; import React, { useContext } from 'react'; import { ShareProvider, addLogQueryParams } from '../../lib/share'; import type { Post } from '../../graphql/posts'; +import { getPostToShare } from '../../graphql/posts'; import type { FeedItemPosition } from '../../lib/feed'; import { postLogEvent } from '../../lib/feed'; import { ActiveFeedContext } from '../../contexts'; @@ -64,6 +65,7 @@ export const SocialShare = ({ const { logOpts } = useContext(ActiveFeedContext); const { openNativeSharePost } = useSharePost(Origin.Share); const [squadToShare, setSquadToShare] = shareToSquadState; + const postToShare = getPostToShare(post); const emailTitle = post?.title ? `Via Daily.Dev: ${post.title}` : 'Via Daily.Dev'; @@ -118,8 +120,8 @@ export const SocialShare = ({ {squadToShare && ( // using lazy modal would result to dep-cycle since this whole component is used in the share modal { expect(isShareLikePost(post)).toBeTruthy(); }); }); + +describe('getPostToShare', () => { + const sharedPost = { id: 'p2', title: 'Shared', image: 'image' } as Post; + + it('should resolve a share without commentary to the post it wraps', () => { + const post = { + id, + type: PostType.Share, + title: undefined, + sharedPost, + } as Post; + + expect(getPostToShare(post)).toBe(sharedPost); + }); + + it('should keep a share with commentary', () => { + const post = { id, type: PostType.Share, title: 'Read this', sharedPost }; + + expect(getPostToShare(post as Post)).toBe(post); + }); + + it('should keep any other post', () => { + const post = { id, type: PostType.Article, title: 'Article' } as Post; + + expect(getPostToShare(post)).toBe(post); + }); +}); diff --git a/packages/shared/src/graphql/posts.ts b/packages/shared/src/graphql/posts.ts index 15d67e86cd6..1027ced21bc 100644 --- a/packages/shared/src/graphql/posts.ts +++ b/packages/shared/src/graphql/posts.ts @@ -138,6 +138,19 @@ export const getReadArticleHref = ( ): string | undefined => getPostReadTarget(post).target?.permalink ?? post.permalink; +/** + * A share without commentary has nothing of its own to reshare, so resharing + * it shares the post it wraps, the same way the API resolves it. + */ +export const getPostToShare = < + T extends Pick, +>( + post: T, +): T | SharedPost => + post.type === PostType.Share && !post.title && post.sharedPost + ? post.sharedPost + : post; + export const getPostTitle = ( post: | {