import React, { useState, useRef, useEffect } from 'react'; import { DataItem, ContentBlock } from '../types'; import { Button } from './Button'; import { TipTapEditor } from './TipTapEditor'; import { BlockEditorModal } from './BlockEditorModal'; import { IconPicker } from './IconPicker'; import { ArrowLeft, Download, ExternalLink, Trash2, Edit2, Plus, Save, X, Check, Copy, Sparkles, Link as LinkIcon, ClipboardCheck } from 'lucide-react'; import { ConfirmDialog } from './ConfirmDialog'; import { Editor } from '@tiptap/react'; import { FishAudioPlayer } from './FishAudioPlayer'; import { MarkdownRenderer } from './MarkdownRenderer'; interface DetailViewProps { item: DataItem; onBack: () => void; onUpdate: (data: Omit) => void; onDelete: (id: string) => void; onDuplicate?: () => void; onTagClick?: (tag: string) => void; isEditable?: boolean; } export const DetailView: React.FC = ({ item, onBack, onUpdate, onDelete, onDuplicate, isEditable = false }) => { const getFishAudioId = (url: string | undefined) => { if (!url) return null; const match = url.match(/fish\.audio\/(?:[a-z]{2}\/)?(?:app\/)?m\/([a-f0-9]+)/i); return match ? match[1] : null; }; const [isEditMode, setIsEditMode] = useState(false); const [editedItem, setEditedItem] = useState(item); const [editingBlockId, setEditingBlockId] = useState(null); const [showEditTab, setShowEditTab] = useState(true); const [categoryInput, setCategoryInput] = useState(''); const [blockEditor, setBlockEditor] = useState<{ isOpen: boolean; type: 'quote' | 'code'; }>({ isOpen: false, type: 'quote' }); const activeEditorRef = useRef(null); const summaryEditorRef = useRef(null); const summaryTextareaRef = useRef(null); const [pendingModalOpen, setPendingModalOpen] = useState(false); const getCardClasses = (style?: 'bordered' | 'transparent' | 'filled') => { switch (style) { case 'filled': return 'bg-surface border border-white/5 shadow-xl'; case 'transparent': return 'bg-transparent border-none shadow-none'; case 'bordered': default: return 'bg-transparent border border-white/10 shadow-none'; // Default to "bordered" as per latest "transparent" refined look } }; const [confirmDialog, setConfirmDialog] = useState<{ isOpen: boolean; title: string; message: string; onConfirm: () => void; }>({ isOpen: false, title: '', message: '', onConfirm: () => { } }); // AI Modal State removed // Auto-resize summary textarea useEffect(() => { if (summaryTextareaRef.current) { summaryTextareaRef.current.style.height = 'auto'; summaryTextareaRef.current.style.height = summaryTextareaRef.current.scrollHeight + 'px'; } }, [editedItem.summary, isEditMode]); // Effect to open modal once editor is ready if pending useEffect(() => { if (pendingModalOpen && activeEditorRef.current && editingBlockId) { setBlockEditor(prev => ({ ...prev, isOpen: true })); setPendingModalOpen(false); } }, [pendingModalOpen, editingBlockId, activeEditorRef.current]); const handleSaveChanges = () => { onUpdate({ icon: editedItem.icon, title: editedItem.title, summary: editedItem.summary, content: editedItem.content || '', tags: editedItem.tags, categories: editedItem.categories, blocks: editedItem.blocks, visitUrl: editedItem.visitUrl, downloadUrl: editedItem.downloadUrl, linkGroups: editedItem.linkGroups, cardStyle: editedItem.cardStyle }); setIsEditMode(false); }; const handleCancelEdit = () => { setEditedItem(item); setIsEditMode(false); setEditingBlockId(null); setEditingBlockId(null); }; const handleAddBlockImmediately = (blockType: 'markdown' | 'raw-markdown' = 'markdown') => { const newBlock: ContentBlock = { id: Date.now().toString(), type: blockType, content: blockType === 'raw-markdown' ? '' : { type: 'doc', content: [ { type: 'paragraph', } ] } }; setEditedItem(prev => ({ ...prev, blocks: [...(prev.blocks || []), newBlock] })); setEditingBlockId(newBlock.id); }; const handleBlockUpdate = (blockId: string, content: any) => { setEditedItem(prev => ({ ...prev, blocks: prev.blocks?.map(b => b.id === blockId ? { ...b, content } : b) || [] })); }; const handleDeleteBlock = (blockId: string) => { setConfirmDialog({ isOpen: true, title: 'Eliminar Bloque', message: '¿Estás seguro de que quieres eliminar este bloque de contenido? Esta acción no se puede deshacer.', onConfirm: () => { setEditedItem(prev => ({ ...prev, blocks: prev.blocks?.filter(b => b.id !== blockId) || [] })); } }); }; const handleBlockDoubleClick = (type: 'code' | 'quote', index: number, blockId: string) => { // Switch to edit mode for this block const block = editedItem.blocks?.find(b => b.id === blockId); if (block) { setEditingBlockId(blockId); setBlockEditor({ isOpen: false, type }); // Will open in effect setPendingModalOpen(true); } }; if (isEditMode) { return (
setEditedItem(prev => ({ ...prev, icon }))} />
setEditedItem(prev => ({ ...prev, title: content }))} isEditable={true} showToolbarOnFocus={true} className="px-3 py-2 min-h-[46px]" dense={true} />
{ // Only update state if it comes from internal changes that usually sync automaticallly, // but here we just keep the simple binding. setEditedItem(prev => ({ ...prev, summary: content })); }} isEditable={true} editorRef={summaryEditorRef} showToolbarOnFocus={true} className="px-3 py-2 min-h-[40px]" dense={true} />
{(editedItem.categories || []).map((cat, idx) => ( {cat} ))}
setCategoryInput(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); const val = categoryInput.trim(); if (val && !(editedItem.categories || []).includes(val)) { setEditedItem(prev => ({ ...prev, categories: [...(prev.categories || []), val] })); setCategoryInput(''); } } }} className="w-full bg-background border border-white/10 rounded-lg p-3 text-white focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors" placeholder="Escribe una categoría y presiona Enter..." />
setEditedItem(prev => ({ ...prev, tags: e.target.value.split(',').map(t => t.trim()).filter(Boolean) }))} className="w-full bg-background border border-white/10 rounded-lg p-3 text-white focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors" placeholder="tag1, tag2, tag3" />
setEditedItem(prev => ({ ...prev, visitUrl: e.target.value }))} className="w-full bg-background border border-white/10 rounded-lg p-3 text-white focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors" placeholder="https://..." />
setEditedItem(prev => ({ ...prev, downloadUrl: e.target.value }))} className="w-full bg-background border border-white/10 rounded-lg p-3 text-white focus:border-primary focus:ring-1 focus:ring-primary outline-none transition-colors" placeholder="https://..." />
{(editedItem.linkGroups || []).map((group) => (
setEditedItem(prev => ({ ...prev, linkGroups: prev.linkGroups?.map(g => g.id === group.id ? { ...g, title: e.target.value } : g) }))} placeholder="Título del grupo de enlaces..." className="w-full bg-transparent border-b border-white/10 p-2 text-white outline-none focus:border-primary text-sm font-medium mb-2" />
setEditedItem(prev => ({ ...prev, linkGroups: prev.linkGroups?.map(g => g.id === group.id ? { ...g, visitUrl: e.target.value } : g) }))} placeholder="https://..." className="w-full bg-surface border border-white/10 rounded p-2 text-white focus:border-primary outline-none text-xs" />
setEditedItem(prev => ({ ...prev, linkGroups: prev.linkGroups?.map(g => g.id === group.id ? { ...g, downloadUrl: e.target.value } : g) }))} placeholder="https://..." className="w-full bg-surface border border-white/10 rounded p-2 text-white focus:border-primary outline-none text-xs" />
))}
{editedItem.blocks && editedItem.blocks.map((block, index) => (
Bloque {index + 1} {block.type === 'markdown' && ( Rich Text )} {block.type === 'raw-markdown' && ( Markdown )}
{block.type === 'raw-markdown' ? (