Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/shared/src/components/post/write/ShareLink.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FormEventHandler, ReactElement } from 'react';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import z from 'zod';
import classNames from 'classnames';
import { useRouter } from 'next/router';
Expand All @@ -15,6 +15,7 @@ import useSourcePostModeration from '../../../hooks/source/useSourcePostModerati
import type { SourcePostModeration } from '../../../graphql/squads';
import { usePrompt } from '../../../hooks/usePrompt';
import { useWritePostContext } from '../../../contexts';
import { TextField } from '../../fields/TextField';

interface ShareLinkProps {
squad?: Squad;
Expand Down Expand Up @@ -53,6 +54,9 @@ export function ShareLink({
const [commentary, setCommentary] = useState(
fetchedPost?.sharedPost ? fetchedPost?.title : fetchedPost?.content,
);
const [title, setTitle] = useState(
fetchedPost?.sharedPost?.title || moderated?.title || '',
);
const {
getLinkPreview,
isLoadingPreview,
Expand All @@ -70,6 +74,13 @@ export function ShareLink({
image: moderated.image,
}),
});

// Sync title with preview when it loads/changes
useEffect(() => {
if (preview?.title && !title) {
setTitle(preview.title);
}
}, [preview?.title, title]);
const { onUpdatePostModeration, isPending: isPostingModeration } =
useSourcePostModeration({
onSuccess: async () => push(squad?.permalink),
Expand Down Expand Up @@ -123,10 +134,10 @@ export function ShareLink({
return null;
}

const { title, image } = preview;
const { image } = preview;
const externalLink = preview.finalUrl ?? preview.url;

if (!title) {
if (!preview.title) {
displayToast('Invalid link');
return null;
}
Expand All @@ -136,7 +147,7 @@ export function ShareLink({
commentary,
content: '',
externalLink,
title,
title: title || preview.title,
...(isImageValid && { imageUrl: image }),
};

Expand Down Expand Up @@ -164,6 +175,17 @@ export function ShareLink({
/>
)}

<TextField
className={{ container: 'w-full' }}
inputId="title"
name="title"
label="Title"
placeholder="Edit the title (optional)"
value={title}
onInput={(e) => setTitle(e.currentTarget.value)}
maxLength={250}
/>

<MarkdownInput
initialContent={commentary || fetchedPost?.title || ''}
enabledCommand={{ mention: true }}
Expand Down