Spaces:
Running
Running
File size: 12,701 Bytes
1904e4c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
import { useState, useEffect } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Calendar as CalendarComponent } from "@/components/ui/calendar";
import { cn } from "@/lib/utils";
import { format } from "date-fns";
import { DateRange } from "@/types";
import { RetrievedSource } from "@/services/apiService";
import { storage, STORAGE_KEYS } from "@/lib/storage";
import {
Search,
ArrowUpDown,
Calendar,
FileText,
ChevronDown,
ChevronUp,
ExternalLink
} from "lucide-react";
interface SourcesModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
}
export const SourcesModal = ({ open, onOpenChange }: SourcesModalProps) => {
const [searchTerm, setSearchTerm] = useState("");
const [selectedSource, setSelectedSource] = useState<string>("");
const [selectedCategory, setSelectedCategory] = useState<string>("");
const [date, setDate] = useState<DateRange>({
from: undefined,
to: undefined,
});
const [sortBy, setSortBy] = useState<string>("date-desc");
const [sources, setSources] = useState<RetrievedSource[]>([]);
const [sourceTypes, setSourceTypes] = useState<string[]>([]);
const [categories, setCategories] = useState<string[]>([]);
// Load sources from storage
useEffect(() => {
if (open) {
const storedSources = storage.get<RetrievedSource[]>(STORAGE_KEYS.SOURCES) || [];
setSources(storedSources);
// Extract unique source types and categories
const types = Array.from(new Set(storedSources.map(s => s.metadata?.source).filter(Boolean)));
setSourceTypes(types as string[]);
const cats = Array.from(new Set(storedSources.map(s => {
// For this example, we'll use the first word of the content as a mock category
const firstWord = s.content_snippet.split(' ')[0];
return firstWord.length > 3 ? firstWord : "General";
})));
setCategories(cats);
}
}, [open]);
// Filter sources based on search term, source, category, and date
const filteredSources = sources.filter(source => {
const matchesSearch = !searchTerm ||
source.content_snippet.toLowerCase().includes(searchTerm.toLowerCase()) ||
(source.metadata?.source || "").toLowerCase().includes(searchTerm.toLowerCase());
const matchesSource = !selectedSource || selectedSource === "All" || source.metadata?.source === selectedSource;
// Mock category matching based on first word of content
const sourceCategory = source.content_snippet.split(' ')[0].length > 3 ?
source.content_snippet.split(' ')[0] : "General";
const matchesCategory = !selectedCategory || selectedCategory === "All" || sourceCategory === selectedCategory;
let matchesDate = true;
if (date.from && source.metadata?.ruling_date) {
matchesDate = matchesDate && new Date(source.metadata.ruling_date) >= date.from;
}
if (date.to && source.metadata?.ruling_date) {
matchesDate = matchesDate && new Date(source.metadata.ruling_date) <= date.to;
}
return matchesSearch && matchesSource && matchesCategory && matchesDate;
});
// Sort sources
const sortedSources = [...filteredSources].sort((a, b) => {
if (sortBy === "date-desc") {
return new Date(b.metadata?.ruling_date || "").getTime() -
new Date(a.metadata?.ruling_date || "").getTime();
} else if (sortBy === "date-asc") {
return new Date(a.metadata?.ruling_date || "").getTime() -
new Date(b.metadata?.ruling_date || "").getTime();
} else if (sortBy === "relevance-desc") {
return b.content_snippet.length - a.content_snippet.length;
}
return 0;
});
const resetFilters = () => {
setSearchTerm("");
setSelectedSource("");
setSelectedCategory("");
setDate({ from: undefined, to: undefined });
};
const [expandedSources, setExpandedSources] = useState<Record<number, boolean>>({});
const toggleSource = (index: number) => {
setExpandedSources(prev => ({
...prev,
[index]: !prev[index]
}));
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="w-full max-w-4xl">
<DialogHeader>
<DialogTitle className="flex items-center text-xl">
<FileText className="mr-2 h-5 w-5" />
Knowledge Sources
</DialogTitle>
</DialogHeader>
<div>
<div className="flex flex-col md:flex-row md:items-center justify-between mb-2">
<div className="flex flex-col md:flex-row space-y-2 md:space-y-0 md:space-x-2">
<Select value={sortBy} onValueChange={setSortBy}>
<SelectTrigger className="w-[180px]">
<div className="flex items-center">
<ArrowUpDown className="mr-2 h-3.5 w-3.5 text-muted-foreground" />
<span>Sort By</span>
</div>
</SelectTrigger>
<SelectContent>
<SelectItem value="date-desc">Date (Newest)</SelectItem>
<SelectItem value="date-asc">Date (Oldest)</SelectItem>
<SelectItem value="relevance-desc">Relevance</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="bg-accent/30 rounded-lg p-1 mb-2">
<div className="flex flex-col md:flex-row space-y-1 md:space-y-0 md:space-x-2">
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4" />
<Input
type="text"
placeholder="Search sources..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10"
/>
</div>
<Select value={selectedSource} onValueChange={setSelectedSource}>
<SelectTrigger className="w-full md:w-[180px]">
<span className="truncate">
{selectedSource || "All Sources"}
</span>
</SelectTrigger>
<SelectContent>
<SelectItem value="All">All Sources</SelectItem>
{sourceTypes.map(type => (
<SelectItem key={type} value={type}>{type}</SelectItem>
))}
</SelectContent>
</Select>
<Select value={selectedCategory} onValueChange={setSelectedCategory}>
<SelectTrigger className="w-full md:w-[180px]">
<span className="truncate">
{selectedCategory || "All Categories"}
</span>
</SelectTrigger>
<SelectContent>
<SelectItem value="All">All Categories</SelectItem>
{categories.map(category => (
<SelectItem key={category} value={category}>{category}</SelectItem>
))}
</SelectContent>
</Select>
<Popover>
<PopoverTrigger asChild>
<Button variant="outline" className="w-full md:w-[180px] justify-start text-left">
<Calendar className="mr-2 h-4 w-4" />
<span>
{date.from || date.to ? (
<>
{date.from ? format(date.from, "LLL dd, y") : "From"} - {" "}
{date.to ? format(date.to, "LLL dd, y") : "To"}
</>
) : (
"Date Range"
)}
</span>
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<CalendarComponent
mode="range"
selected={date}
onSelect={(value: DateRange | undefined) => {
if (value) setDate(value);
}}
className="p-3"
/>
</PopoverContent>
</Popover>
<Button
variant="outline"
className="md:w-auto"
onClick={resetFilters}
>
Reset
</Button>
</div>
</div>
<div className="max-h-[45dvh] md:max-h-[60vh] overflow-y-auto space-y-4">
{sortedSources.length > 0 ? (
sortedSources.map((source, index) => (
<div key={index} className="border rounded-lg overflow-hidden transition-all duration-300">
<div className="p-4 bg-card">
<div className="flex items-start justify-between">
<div>
<h3 className="font-medium">
{source.metadata?.source || "Source"}
</h3>
<div className="flex items-center mt-1 text-sm text-muted-foreground">
<FileText className="h-3.5 w-3.5 mr-1.5" />
<span>{source.metadata?.source || "Unknown Source"}</span>
{source.metadata?.ruling_date && (
<>
<span className="mx-1.5">•</span>
<Calendar className="h-3.5 w-3.5 mr-1.5" />
<span>{new Date(source.metadata.ruling_date).toLocaleDateString()}</span>
</>
)}
</div>
</div>
<Button
variant="ghost"
size="sm"
onClick={() => toggleSource(index)}
className="p-0 h-8 w-8 hover:bg-accent/10"
>
{expandedSources[index] ? (
<ChevronUp className="h-4 w-4" />
) : (
<ChevronDown className="h-4 w-4" />
)}
</Button>
</div>
<div className={expandedSources[index] ? "" : "max-h-10 md:max-h-16 overflow-hidden relative"}>
<p className="text-sm text-muted-foreground mt-2">
{source.content_snippet}
</p>
{!expandedSources[index] && (
<div className="absolute bottom-0 left-0 right-0 h-12 bg-gradient-to-t from-card to-transparent"></div>
)}
</div>
{expandedSources[index] && (
<div className="mt-4 text-sm flex justify-end">
<Button variant="link" size="sm" className="h-8 p-0 text-primary">
<ExternalLink className="h-3 w-3 mr-1" />
View source
</Button>
</div>
)}
</div>
</div>
))
) : (
<div className="text-center p-8">
<div className="w-16 h-16 mx-auto mb-4 rounded-full bg-muted/30 flex items-center justify-center">
<FileText className="h-8 w-8 text-muted-foreground" />
</div>
<h3 className="text-lg font-medium mb-2">No sources found</h3>
<p className="text-muted-foreground">
{sources.length > 0
? "No sources match your current search criteria. Try adjusting your filters."
: "Chat with Insight AI to get information with source citations."}
</p>
<Button
variant="outline"
className="mt-4"
onClick={resetFilters}
>
Reset Filters
</Button>
</div>
)}
</div>
</div>
</DialogContent>
</Dialog>
);
};
|