import { useState } from "react";
import { Keyboard, Pressable, ScrollView, TextInput, View } from "react-native";
import BookmarkTextMarkdown from "@/components/bookmarks/BookmarkTextMarkdown";
import { Button } from "@/components/ui/Button";
import { Text } from "@/components/ui/Text";
import { useToast } from "@/components/ui/Toast";
import { useColorScheme } from "nativewind";
import { useUpdateBookmark } from "@karakeep/shared-react/hooks/bookmarks";
import { BookmarkTypes, ZBookmark } from "@karakeep/shared/types/bookmarks";
interface BookmarkTextViewProps {
bookmark: ZBookmark;
}
export default function BookmarkTextView({ bookmark }: BookmarkTextViewProps) {
if (bookmark.content.type !== BookmarkTypes.TEXT) {
throw new Error("Wrong content type rendered");
}
const { toast } = useToast();
const { colorScheme } = useColorScheme();
const [isEditing, setIsEditing] = useState(false);
const initialText = bookmark.content.text;
const [content, setContent] = useState(initialText);
const { mutate, isPending } = useUpdateBookmark({
onError: () => {
toast({
message: "Something went wrong",
variant: "destructive",
});
},
onSuccess: () => {
setIsEditing(false);
toast({
message: "Text updated successfully",
showProgress: false,
});
},
});
const handleSave = () => {
mutate({
bookmarkId: bookmark.id,
text: content,
});
};
const handleDiscard = () => {
setContent(initialText);
setIsEditing(false);
Keyboard.dismiss();
};
if (isEditing) {
return (
);
}
return (
setIsEditing(true)}>
{content.trim() === "" && (
Tap to add text...
)}
);
}