Skip to content
Merged
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
42 changes: 20 additions & 22 deletions packages/app/ui/components/BareChatInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { JSONContent, Story, pathToCite } from '@tloncorp/api/urbit';
import {
Attachment,
JSONToInlines,
LinkAttachment,
REF_REGEX,
createDevLogger,
diaryMixedToJSON,
Expand Down Expand Up @@ -66,6 +65,16 @@ const bareChatInputLogger = createDevLogger('bareChatInput', false);

const DEFAULT_KEYBOARD_HEIGHT = 300;

function normalizePreviewUrl(url: string) {
try {
const parsedUrl = new URL(url);
parsedUrl.hash = '';
return parsedUrl.toString();
} catch {
return url;
}
}

function useKeyboardHeight(maxInputHeightBasic: number) {
const [maxInputHeight, setMaxInputHeight] = useState(maxInputHeightBasic);

Expand Down Expand Up @@ -558,34 +567,23 @@ function BareChatInput(
const matches = textOutsideCodeBlocks.match(urlRegex) || [];

// Normalize URLs (remove hash) and deduplicate
const currentUrls = [
...new Set(
matches.map((url) => {
try {
const parsedUrl = new URL(url);
parsedUrl.hash = '';
return parsedUrl.toString();
} catch {
return url;
}
})
),
];
const currentUrls = [...new Set(matches.map(normalizePreviewUrl))];

const prevUrls = prevUrlsRef.current;
const currentAttachments = attachmentsRef.current;
const linksByUrl = new Map(
attachmentsRef.current
.filter((a) => a.type === 'link')
.map((a) => [normalizePreviewUrl(a.url), a] as const)
);

// Find URLs that were removed from text
const removedUrls = prevUrls.filter((url) => !currentUrls.includes(url));

// Find URLs that were added to text
const addedUrls = currentUrls.filter((url) => !prevUrls.includes(url));
const addedUrls = currentUrls.filter(
(url) => !prevUrls.includes(url) && !linksByUrl.has(url)
);

// Remove attachments for URLs no longer in text
removedUrls.forEach((url) => {
const attachment = currentAttachments.find(
(a): a is LinkAttachment => a.type === 'link' && a.url === url
);
const attachment = linksByUrl.get(url);
if (attachment) {
bareChatInputLogger.log('removing stale link attachment', { url });
removeAttachment(attachment);
Expand Down
Loading