|
| 1 | +/** |
| 2 | + * Smart Lists component |
| 3 | + * Provides auto-populated lists based on user-defined criteria |
| 4 | + */ |
| 5 | + |
| 6 | +import type { EntityType } from "@bibgraph/types"; |
| 7 | +import { |
| 8 | + ActionIcon, |
| 9 | + Badge, |
| 10 | + Box, |
| 11 | + Button, |
| 12 | + Card, |
| 13 | + Group, |
| 14 | + Modal, |
| 15 | + Select, |
| 16 | + SimpleGrid, |
| 17 | + Stack, |
| 18 | + Text, |
| 19 | + TextInput, |
| 20 | + Title, |
| 21 | + Tooltip, |
| 22 | +} from "@mantine/core"; |
| 23 | +import { |
| 24 | + IconBulb, |
| 25 | + IconCheck, |
| 26 | + IconEdit, |
| 27 | + IconInfoCircle, |
| 28 | + IconRefresh, |
| 29 | + IconX, |
| 30 | +} from "@tabler/icons-react"; |
| 31 | +import { useState } from "react"; |
| 32 | + |
| 33 | +import { ICON_SIZE } from '@/config/style-constants'; |
| 34 | + |
| 35 | +/** |
| 36 | + * Smart list criteria types |
| 37 | + */ |
| 38 | +export type SmartListCriteriaType = |
| 39 | + | 'entity-type' |
| 40 | + | 'publication-year' |
| 41 | + | 'citation-count' |
| 42 | + | 'recent-bookmarks' |
| 43 | + | 'tag-filter'; |
| 44 | + |
| 45 | +/** |
| 46 | + * Smart list criteria definition |
| 47 | + */ |
| 48 | +export interface SmartListCriteria { |
| 49 | + id: string; |
| 50 | + name: string; |
| 51 | + description: string; |
| 52 | + type: SmartListCriteriaType; |
| 53 | + color: string; |
| 54 | + icon: React.ReactNode; |
| 55 | + // Configuration fields |
| 56 | + config?: { |
| 57 | + entityType?: EntityType; |
| 58 | + year?: number; |
| 59 | + minCitations?: number; |
| 60 | + tag?: string; |
| 61 | + days?: number; |
| 62 | + }; |
| 63 | + // Query parameters for fetching |
| 64 | + queryParams?: Record<string, string | number | boolean>; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Predefined smart list templates |
| 69 | + */ |
| 70 | +const PREDEFINED_SMART_LISTS: SmartListCriteria[] = [ |
| 71 | + { |
| 72 | + id: 'my-papers-2024', |
| 73 | + name: 'My Papers from 2024', |
| 74 | + description: 'All works I authored or bookmarked in 2024', |
| 75 | + type: 'publication-year', |
| 76 | + color: 'blue', |
| 77 | + icon: <IconBulb size={ICON_SIZE.MD} />, |
| 78 | + config: { year: 2024 }, |
| 79 | + }, |
| 80 | + { |
| 81 | + id: 'highly-cited', |
| 82 | + name: 'Highly Cited Works', |
| 83 | + description: 'Works with more than 100 citations', |
| 84 | + type: 'citation-count', |
| 85 | + color: 'orange', |
| 86 | + icon: <IconBulb size={ICON_SIZE.MD} />, |
| 87 | + config: { minCitations: 100 }, |
| 88 | + }, |
| 89 | + { |
| 90 | + id: 'recent-bookmarks', |
| 91 | + name: 'Recent Bookmarks', |
| 92 | + description: 'Works I bookmarked in the last 30 days', |
| 93 | + type: 'recent-bookmarks', |
| 94 | + color: 'green', |
| 95 | + icon: <IconBulb size={ICON_SIZE.MD} />, |
| 96 | + config: { days: 30 }, |
| 97 | + }, |
| 98 | +]; |
| 99 | + |
| 100 | +interface SmartListsProps { |
| 101 | + onCreateSmartList: (criteria: SmartListCriteria) => void; |
| 102 | + onClose: () => void; |
| 103 | +} |
| 104 | + |
| 105 | +export const SmartLists = ({ onCreateSmartList, onClose }: SmartListsProps) => { |
| 106 | + const [showCreateModal, setShowCreateModal] = useState(false); |
| 107 | + const [customName, setCustomName] = useState(''); |
| 108 | + const [customType, setCustomType] = useState<SmartListCriteriaType>('entity-type'); |
| 109 | + const [refreshing, setRefreshing] = useState<string | null>(null); |
| 110 | + |
| 111 | + const handleRefresh = async (criteria: SmartListCriteria) => { |
| 112 | + setRefreshing(criteria.id); |
| 113 | + // Simulate refresh - in real implementation, this would re-run the query |
| 114 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 115 | + setRefreshing(null); |
| 116 | + }; |
| 117 | + |
| 118 | + const handleCreateSmartList = (criteria: SmartListCriteria) => { |
| 119 | + onCreateSmartList(criteria); |
| 120 | + onClose(); |
| 121 | + }; |
| 122 | + |
| 123 | + const handleCreateCustom = () => { |
| 124 | + if (!customName.trim()) return; |
| 125 | + |
| 126 | + const newCriteria: SmartListCriteria = { |
| 127 | + id: `custom-${Date.now()}`, |
| 128 | + name: customName, |
| 129 | + description: `Custom ${customType} smart list`, |
| 130 | + type: customType, |
| 131 | + color: 'grape', |
| 132 | + icon: <IconBulb size={ICON_SIZE.MD} />, |
| 133 | + }; |
| 134 | + |
| 135 | + onCreateSmartList(newCriteria); |
| 136 | + setCustomName(''); |
| 137 | + setShowCreateModal(false); |
| 138 | + }; |
| 139 | + |
| 140 | + return ( |
| 141 | + <Stack gap="lg"> |
| 142 | + {/* Header */} |
| 143 | + <Group justify="space-between" align="flex-start"> |
| 144 | + <div> |
| 145 | + <Title order={3}>Smart Lists</Title> |
| 146 | + <Text size="sm" c="dimmed"> |
| 147 | + Auto-populated lists based on your criteria |
| 148 | + </Text> |
| 149 | + </div> |
| 150 | + <ActionIcon |
| 151 | + variant="subtle" |
| 152 | + onClick={onClose} |
| 153 | + aria-label="Close smart lists" |
| 154 | + > |
| 155 | + <IconX size={ICON_SIZE.MD} /> |
| 156 | + </ActionIcon> |
| 157 | + </Group> |
| 158 | + |
| 159 | + {/* Predefined Smart Lists */} |
| 160 | + <Box> |
| 161 | + <Text fw={500} mb="md">Predefined Smart Lists</Text> |
| 162 | + <SimpleGrid cols={{ base: 1, sm: 2 }} spacing="md"> |
| 163 | + {PREDEFINED_SMART_LISTS.map((criteria) => ( |
| 164 | + <Card |
| 165 | + key={criteria.id} |
| 166 | + shadow="sm" |
| 167 | + padding="md" |
| 168 | + radius="md" |
| 169 | + withBorder |
| 170 | + style={{ borderColor: criteria.color }} |
| 171 | + > |
| 172 | + <Stack gap="sm"> |
| 173 | + {/* Header */} |
| 174 | + <Group justify="space-between" align="flex-start"> |
| 175 | + <Box c={criteria.color}> |
| 176 | + {criteria.icon} |
| 177 | + </Box> |
| 178 | + <Badge color={criteria.color} variant="light"> |
| 179 | + {criteria.type} |
| 180 | + </Badge> |
| 181 | + </Group> |
| 182 | + |
| 183 | + {/* Name and Description */} |
| 184 | + <div> |
| 185 | + <Text fw={600} size="sm">{criteria.name}</Text> |
| 186 | + <Text size="xs" c="dimmed">{criteria.description}</Text> |
| 187 | + </div> |
| 188 | + |
| 189 | + {/* Actions */} |
| 190 | + <Group gap="xs"> |
| 191 | + <Button |
| 192 | + size="xs" |
| 193 | + fullWidth |
| 194 | + leftSection={<IconCheck size={14} />} |
| 195 | + onClick={() => handleCreateSmartList(criteria)} |
| 196 | + > |
| 197 | + Create List |
| 198 | + </Button> |
| 199 | + <Tooltip label="Refresh criteria"> |
| 200 | + <ActionIcon |
| 201 | + size="sm" |
| 202 | + variant="light" |
| 203 | + loading={refreshing === criteria.id} |
| 204 | + onClick={() => void handleRefresh(criteria)} |
| 205 | + > |
| 206 | + <IconRefresh size={14} /> |
| 207 | + </ActionIcon> |
| 208 | + </Tooltip> |
| 209 | + </Group> |
| 210 | + </Stack> |
| 211 | + </Card> |
| 212 | + ))} |
| 213 | + </SimpleGrid> |
| 214 | + </Box> |
| 215 | + |
| 216 | + {/* Custom Smart List */} |
| 217 | + <Card |
| 218 | + shadow="sm" |
| 219 | + padding="md" |
| 220 | + radius="md" |
| 221 | + withBorder |
| 222 | + style={{ borderStyle: 'dashed' }} |
| 223 | + > |
| 224 | + <Group justify="space-between"> |
| 225 | + <div> |
| 226 | + <Text fw={500}>Create Custom Smart List</Text> |
| 227 | + <Text size="xs" c="dimmed"> |
| 228 | + Define your own criteria for auto-populated lists |
| 229 | + </Text> |
| 230 | + </div> |
| 231 | + <Button |
| 232 | + variant="light" |
| 233 | + leftSection={<IconEdit size={14} />} |
| 234 | + onClick={() => setShowCreateModal(true)} |
| 235 | + > |
| 236 | + Create Custom |
| 237 | + </Button> |
| 238 | + </Group> |
| 239 | + </Card> |
| 240 | + |
| 241 | + {/* Create Custom Smart List Modal */} |
| 242 | + <Modal |
| 243 | + opened={showCreateModal} |
| 244 | + onClose={() => setShowCreateModal(false)} |
| 245 | + title="Create Custom Smart List" |
| 246 | + size="md" |
| 247 | + > |
| 248 | + <Stack gap="md"> |
| 249 | + <TextInput |
| 250 | + label="List Name" |
| 251 | + placeholder="My custom smart list" |
| 252 | + value={customName} |
| 253 | + onChange={(e) => setCustomName(e.target.value)} |
| 254 | + required |
| 255 | + /> |
| 256 | + |
| 257 | + <Select |
| 258 | + label="Criteria Type" |
| 259 | + placeholder="Select criteria type" |
| 260 | + data={[ |
| 261 | + { value: 'entity-type', label: 'Entity Type' }, |
| 262 | + { value: 'publication-year', label: 'Publication Year' }, |
| 263 | + { value: 'citation-count', label: 'Citation Count' }, |
| 264 | + { value: 'recent-bookmarks', label: 'Recent Bookmarks' }, |
| 265 | + { value: 'tag-filter', label: 'Tag Filter' }, |
| 266 | + ]} |
| 267 | + value={customType} |
| 268 | + onChange={(value) => setCustomType(value as SmartListCriteriaType)} |
| 269 | + required |
| 270 | + /> |
| 271 | + |
| 272 | + <Group gap="xs" align="center"> |
| 273 | + <IconInfoCircle size={12} color="var(--mantine-color-dimmed)" /> |
| 274 | + <Text size="xs" c="dimmed"> |
| 275 | + Smart lists automatically update when new entities match your criteria. |
| 276 | + </Text> |
| 277 | + </Group> |
| 278 | + |
| 279 | + <Group justify="flex-end" gap="xs"> |
| 280 | + <Button |
| 281 | + variant="subtle" |
| 282 | + onClick={() => setShowCreateModal(false)} |
| 283 | + > |
| 284 | + Cancel |
| 285 | + </Button> |
| 286 | + <Button |
| 287 | + onClick={handleCreateCustom} |
| 288 | + disabled={!customName.trim()} |
| 289 | + > |
| 290 | + Create Smart List |
| 291 | + </Button> |
| 292 | + </Group> |
| 293 | + </Stack> |
| 294 | + </Modal> |
| 295 | + </Stack> |
| 296 | + ); |
| 297 | +}; |
0 commit comments