import useGetCardFromMyLibrary from '@/features/cards/lib/queries/useGetCardFromMyLibrary';
import {
Anchor,
Card,
Group,
Stack,
Tooltip,
Text,
Image,
Textarea,
Button,
Flex,
Input,
VisuallyHidden,
} from '@mantine/core';
import { UrlCard, User } from '@semble/types';
import { Fragment, useState } from 'react';
import useUpdateNote from '../../lib/mutations/useUpdateNote';
import { notifications } from '@mantine/notifications';
import useRemoveCardFromLibrary from '@/features/cards/lib/mutations/useRemoveCardFromLibrary';
import { BsExclamation } from 'react-icons/bs';
import { LinkAvatar } from '@/components/link/MantineLink';
import { isBotAccount } from '@/features/platforms/bluesky/lib/utils/account';
import BotLabel from '@/features/profile/components/botLabel/BotLabel';
interface Props {
onClose: () => void;
note: UrlCard['note'];
cardContent: UrlCard['cardContent'];
cardAuthor?: User;
domain: string;
}
export default function NoteCardModalContent(props: Props) {
const cardStatus = useGetCardFromMyLibrary({ url: props.cardContent.url });
const isMyCard = props.cardAuthor?.id === cardStatus.data.card?.author.id;
const [note, setNote] = useState(isMyCard ? props.note?.text : '');
const [editMode, setEditMode] = useState(false);
const [showDeleteWarning, setShowDeleteWarning] = useState(false);
const MAX_NOTE_LENGTH = 500;
const removeNote = useRemoveCardFromLibrary();
const updateNote = useUpdateNote();
const handleDeleteNote = () => {
if (!isMyCard || !props.note) return;
removeNote.mutate(props.note.id, {
onError: () => {
notifications.show({
message: 'Could not delete note',
position: 'top-center',
color: 'red',
title: 'Error',
loading: false,
autoClose: false,
withCloseButton: true,
icon: ,
});
},
onSettled: () => {
props.onClose();
},
});
};
const handleUpdateNote = () => {
if (!props.note || !note) {
props.onClose();
return;
}
if (props.note.text === note) {
props.onClose();
return;
}
updateNote.mutate(
{
cardId: props.note?.id,
note: note,
},
{
onError: () => {
notifications.show({
message: 'Could not update note',
position: 'top-center',
color: 'red',
title: 'Error',
loading: false,
autoClose: false,
withCloseButton: true,
icon: ,
});
},
onSettled: () => {
setEditMode(false);
},
},
);
};
if (editMode) {
return (
Your note
{note?.length ?? 0} / {MAX_NOTE_LENGTH}
);
}
return (
{props.cardAuthor && (
{props.cardAuthor.name}
{isBotAccount(props.cardAuthor) && }
)}
{props.note && {props.note.text}}
{props.cardContent.imageUrl && (
)}
{props.cardContent.title && (
{props.cardContent.title}
)}
e.stopPropagation()}
>
{props.domain}
{isMyCard && (
{showDeleteWarning ? (
Delete note?
) : (
)}
)}
);
}