Spaces:
Running
Running
File size: 8,997 Bytes
16b7924 |
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
import {
ActionIcon,
Card,
Center,
Divider,
Drawer,
type DrawerProps,
Group,
ScrollArea,
Stack,
Tabs,
Text,
TextInput,
Tooltip,
} from "@mantine/core";
import {
IconChartBar,
IconClock,
IconHistory,
IconPin,
IconPinFilled,
IconSearch,
IconTrash,
IconTrashX,
IconX,
} from "@tabler/icons-react";
import { usePubSub } from "create-pubsub/react";
import { useEffect, useState } from "react";
import { useSearchHistory } from "../../../hooks/useSearchHistory";
import type { SearchEntry } from "../../../modules/history";
import { settingsPubSub } from "../../../modules/pubSub";
import { formatRelativeTime } from "../../../modules/stringFormatters";
import SearchStats from "../../Analytics/SearchStats";
interface HistoryDrawerProps extends Omit<DrawerProps, "children"> {
onSearchSelect?: (entry: SearchEntry) => void;
}
export default function HistoryDrawer({
onSearchSelect,
...drawerProps
}: HistoryDrawerProps) {
const [searchQuery, setSearchQuery] = useState("");
const [activeTab, setActiveTab] = useState<string | null>("history");
const [pendingDeleteId, setPendingDeleteId] = useState<number | null>(null);
const [settings] = usePubSub(settingsPubSub);
const {
filteredSearches,
groupedSearches,
togglePin,
deleteEntry,
searchHistory,
} = useSearchHistory({ limit: 100, enableGrouping: true });
useEffect(() => {
searchHistory(searchQuery);
}, [searchQuery, searchHistory]);
const handleSearchSelect = (search: SearchEntry) => {
if (onSearchSelect) {
onSearchSelect(search);
}
};
const handlePin = (searchId: number) => {
togglePin(searchId);
};
const handleDelete = (searchId: number) => {
deleteEntry(searchId);
setPendingDeleteId(null);
};
const handleDeleteClick = (searchId: number) => {
if (pendingDeleteId === searchId) {
handleDelete(searchId);
} else {
setPendingDeleteId(searchId);
}
};
const cancelDelete = () => {
setPendingDeleteId(null);
};
const renderSearchItem = (search: SearchEntry, index: number) => (
<Card
key={search.id || index}
p="sm"
radius="md"
style={{ cursor: "pointer" }}
className="hover:bg-gray-50 dark:hover:bg-gray-800"
onClick={() => handleSearchSelect(search)}
>
<Group justify="space-between" align="flex-start" wrap="nowrap">
<Stack gap={2} style={{ flex: 1, minWidth: 0 }}>
<Tooltip label={search.query} multiline withArrow>
<Text size="sm" fw={search.isPinned ? 600 : 400} truncate>
{search.query}
</Text>
</Tooltip>
<Text
size="xs"
c="dimmed"
style={{ display: "flex", alignItems: "center", gap: 4 }}
>
<IconClock size={12} />
{formatRelativeTime(search.timestamp)}
</Text>
</Stack>
<Group gap="xs" style={{ flexShrink: 0 }}>
<ActionIcon
variant="subtle"
color={search.isPinned ? "blue" : "gray"}
size="sm"
onClick={(e) => {
e.stopPropagation();
if (search.id) handlePin(search.id);
}}
aria-label={search.isPinned ? "Unpin search" : "Pin search"}
>
{search.isPinned ? (
<IconPinFilled size={14} />
) : (
<IconPin size={14} />
)}
</ActionIcon>
<ActionIcon
variant="subtle"
color={pendingDeleteId === search.id ? "red" : "default"}
size="sm"
onClick={(e) => {
e.stopPropagation();
if (search.id) handleDeleteClick(search.id);
}}
onMouseLeave={() => {
if (pendingDeleteId === search.id) {
cancelDelete();
}
}}
aria-label={
pendingDeleteId === search.id
? "Click again to confirm delete"
: "Delete search"
}
style={{
transition: "all 0.2s ease",
}}
>
{pendingDeleteId === search.id ? (
<IconTrashX size={14} />
) : (
<IconTrash size={14} />
)}
</ActionIcon>
</Group>
</Group>
</Card>
);
const renderGroupedSearches = () => {
const pinnedSearches = filteredSearches.filter((search) => search.isPinned);
const unpinnedSearches = filteredSearches.filter(
(search) => !search.isPinned,
);
const content = [];
if (pinnedSearches.length > 0) {
content.push(
<Stack key="pinned" gap="xs">
<Text size="sm" fw={600} c="blue" tt="uppercase">
Pinned
</Text>
{pinnedSearches.map(renderSearchItem)}
<Divider my="sm" />
</Stack>,
);
}
if (
Object.keys(groupedSearches).length === 0 ||
unpinnedSearches.length === 0
) {
content.push(...unpinnedSearches.map(renderSearchItem));
} else {
const unpinnedGrouped = unpinnedSearches.reduce(
(acc, search) => {
for (const [period, searches] of Object.entries(groupedSearches)) {
if (searches.some((s) => s.id === search.id)) {
if (!acc[period]) acc[period] = [];
acc[period].push(search);
break;
}
}
return acc;
},
{} as Record<string, typeof unpinnedSearches>,
);
Object.entries(unpinnedGrouped).forEach(([period, searches]) => {
if (searches.length > 0) {
content.push(
<Stack key={period} gap="xs">
<Text size="sm" fw={600} c="dimmed" tt="uppercase">
{period}
</Text>
{searches.map(renderSearchItem)}
</Stack>,
);
}
});
}
return content;
};
if (!settings.enableHistory) {
return (
<Drawer {...drawerProps} position="left" size="md" title="Search History">
<Center h={200}>
<Stack align="center" gap="xs">
<Text c="dimmed">Search history is disabled</Text>
<Text size="sm" c="dimmed">
Enable it in settings to see your recent searches
</Text>
</Stack>
</Center>
</Drawer>
);
}
return (
<Drawer
{...drawerProps}
position="left"
size="md"
title={
<Group gap="xs">
<Text fw={600}>Search History & Analytics</Text>
</Group>
}
>
<Tabs value={activeTab} onChange={setActiveTab}>
<Tabs.List grow>
<Tabs.Tab value="history" leftSection={<IconHistory size={16} />}>
History
</Tabs.Tab>
<Tabs.Tab value="analytics" leftSection={<IconChartBar size={16} />}>
Analytics
</Tabs.Tab>
</Tabs.List>
<Tabs.Panel value="history" pt="md">
<Stack gap="md">
<TextInput
placeholder="Filter history..."
value={searchQuery}
onChange={(event) => setSearchQuery(event.currentTarget.value)}
leftSection={<IconSearch size={16} />}
rightSection={
searchQuery && (
<ActionIcon
variant="subtle"
color="gray"
size="sm"
onClick={() => setSearchQuery("")}
>
<IconX size={14} />
</ActionIcon>
)
}
size="sm"
style={{ flex: 1 }}
/>
<ScrollArea.Autosize
mah="calc(100vh - 190px)"
styles={{ content: { minWidth: 0 } }}
>
{filteredSearches.length === 0 ? (
<Center py="xl">
<Stack align="center" gap="xs">
<IconSearch size={32} color="gray" />
<Text c="dimmed">
{searchQuery
? "No matching searches found"
: "No search history yet"}
</Text>
{!searchQuery && (
<Text size="sm" c="dimmed" ta="center">
Your recent searches will appear here
</Text>
)}
</Stack>
</Center>
) : (
<Stack gap="xs">{renderGroupedSearches()}</Stack>
)}
</ScrollArea.Autosize>
</Stack>
</Tabs.Panel>
<Tabs.Panel value="analytics" pt="md">
<ScrollArea.Autosize mah="calc(100vh - 140px)">
<SearchStats period="all" compact />
</ScrollArea.Autosize>
</Tabs.Panel>
</Tabs>
</Drawer>
);
}
|