import { Plus, X, Image as ImageIcon, FileText } from "lucide-react" import { useState } from "react" import { Button } from "../../ui/button" import { TFile, TFolder } from "@/lib/types" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { Input } from "@/components/ui/input" import { ContextTab } from "./types" import { ContextTabsProps } from "./types" // Ignore certain folders and files from the file tree import { ignoredFiles, ignoredFolders } from "./lib/ignored-paths" export default function ContextTabs({ contextTabs, onRemoveTab, className, files = [], onFileSelect, }: ContextTabsProps & { className?: string }) { // State for preview tab const [previewTab, setPreviewTab] = useState(null) const [searchQuery, setSearchQuery] = useState("") // Allow preview for images and code selections from editor const togglePreview = (tab: ContextTab) => { if (!tab.lineRange && tab.type !== "image") { return; } // Toggle preview for images and code selections from editor if (previewTab?.id === tab.id) { setPreviewTab(null) } else { setPreviewTab(tab) } } // Remove tab from context when clicking on X const handleRemoveTab = (id: string) => { if (previewTab?.id === id) { setPreviewTab(null) } onRemoveTab(id) } // Get all files from the file tree to search for context const getAllFiles = (items: (TFile | TFolder)[]): TFile[] => { return items.reduce((acc: TFile[], item) => { // Add file if it's not ignored if (item.type === "file" && !ignoredFiles.some((pattern: string) => item.name.endsWith(pattern.replace('*', '')) || item.name === pattern )) { acc.push(item) // Add all files from folder if it's not ignored } else if (item.type === "folder" && !ignoredFolders.some((folder: string) => folder === item.name)) { acc.push(...getAllFiles(item.children)) } return acc }, []) } // Get all files from the file tree to search for context when adding context const allFiles = getAllFiles(files) const filteredFiles = allFiles.filter(file => file.name.toLowerCase().includes(searchQuery.toLowerCase()) ) return (
{/* Add context tab button */} {/* Add context tab popover */}
setSearchQuery(e.target.value)} className="flex-1" />
{filteredFiles.map((file) => ( ))}
{/* Add context tab button */} {contextTabs.length === 0 && (
Add Context
)} {/* Render context tabs */} {contextTabs.map((tab) => (
togglePreview(tab)} > {tab.type === "image" && } {tab.name}
))}
{/* Preview Section */} {previewTab && (
{previewTab.type === "image" ? ( {previewTab.name} ) : previewTab.lineRange && ( <>
Lines {previewTab.lineRange.start}-{previewTab.lineRange.end}
                  {previewTab.content}
                
)} {/* Render file context tab */} {previewTab.type === "file" && (
                {previewTab.content}
              
)}
)}
) }