-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSidebarDatamodelView.tsx
More file actions
227 lines (204 loc) · 10.6 KB
/
Copy pathSidebarDatamodelView.tsx
File metadata and controls
227 lines (204 loc) · 10.6 KB
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
import { EntityType, GroupType } from "@/lib/Types";
import { useTouch } from '../ui/hybridtooltop';
import { useSidebarDispatch } from '@/contexts/SidebarContext';
import { cn } from "@/lib/utils";
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "@radix-ui/react-collapsible";
import { Slot } from "@radix-ui/react-slot";
import { ExternalLink, Puzzle, Search, X } from "lucide-react";
import { useState, useEffect } from "react";
import { Input } from "@/components/ui/input";
import { useDatamodelView, useDatamodelViewDispatch } from "@/contexts/DatamodelViewContext";
import { useDatamodelData } from "@/contexts/DatamodelDataContext";
interface ISidebarDatamodelViewProps {
}
interface INavItemProps {
group: GroupType,
}
export const SidebarDatamodelView = ({ }: ISidebarDatamodelViewProps) => {
const isTouch = useTouch();
const dispatch = useSidebarDispatch();
const { currentSection, currentGroup, scrollToSection } = useDatamodelView();
const dataModelDispatch = useDatamodelViewDispatch();
const { groups } = useDatamodelData();
const [searchTerm, setSearchTerm] = useState("");
const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
const setOpen = (state: boolean) => {
dispatch({ type: "SET_OPEN", payload: state })
}
// Search functionality
const handleSearch = (term: string) => {
setSearchTerm(term);
if (term.trim()) {
const newExpandedGroups = new Set<string>();
groups.forEach(group => {
const hasMatchingEntity = group.Entities.some(entity =>
entity.SchemaName.toLowerCase().includes(term.toLowerCase()) ||
entity.DisplayName.toLowerCase().includes(term.toLowerCase())
);
if (hasMatchingEntity) {
newExpandedGroups.add(group.Name);
}
});
setExpandedGroups(newExpandedGroups);
} else {
setExpandedGroups(new Set());
}
};
const clearSearch = () => {
setSearchTerm("");
setExpandedGroups(new Set());
};
const isEntityMatch = (entity: EntityType) => {
if (!searchTerm.trim()) return false;
return entity.SchemaName.toLowerCase().includes(searchTerm.toLowerCase()) ||
entity.DisplayName.toLowerCase().includes(searchTerm.toLowerCase());
};
const highlightText = (text: string, searchTerm: string) => {
if (!searchTerm.trim()) return text;
const regex = new RegExp(`(${searchTerm})`, 'gi');
const parts = text.split(regex);
return parts.map((part, index) =>
regex.test(part) ?
<mark key={index} className="bg-yellow-200 text-yellow-900 px-0.5 rounded">{part}</mark> :
part
);
};
const handleGroupClick = (groupName: string) => {
dataModelDispatch({ type: "SET_CURRENT_GROUP", payload: groupName });
};
const handleSectionClick = (sectionId: string) => {
dataModelDispatch({ type: 'SET_LOADING', payload: true });
dataModelDispatch({ type: 'SET_CURRENT_SECTION', payload: sectionId });
if (scrollToSection) {
scrollToSection(sectionId);
}
if (isTouch) { setOpen(false); }
clearSearch();
};
const NavItem = ({ group }: INavItemProps) => {
const isCurrentGroup = currentGroup?.toLowerCase() === group.Name.toLowerCase();
const shouldExpand = expandedGroups.has(group.Name);
const [isExpanded, setIsExpanded] = useState(false)
useEffect(() => {
if (searchTerm.trim()) {
setIsExpanded(shouldExpand);
} else {
setIsExpanded(isCurrentGroup);
}
}, [isCurrentGroup, shouldExpand, searchTerm])
return (
<Collapsible
open={isExpanded}
onOpenChange={setIsExpanded}
className={`group/collapsible ${isCurrentGroup ? "bg-sidebar-accent border-blue-500" : "border-transparent"} rounded-md transition-colors w-full m-0 pl-2`}
>
<div className="relative flex w-full min-w-0 flex-col p-0">
<Slot
className={cn(
"duration-200 flex h-8 shrink-0 items-center bg-white rounded-md text-xs font-semibold text-sidebar-foreground/80 outline-none ring-sidebar-ring transition-all focus-visible:ring-2 cursor-pointer w-full",
isCurrentGroup ? "bg-blue-100 text-blue-900" : "hover:bg-sidebar-accent hover:text-sidebar-primary"
)}
>
<CollapsibleTrigger
className={cn(
"flex items-center w-full gap-2 rounded-md p-1 transition-colors cursor-pointer text-left min-h-8 border border-gray-100",
isCurrentGroup ? "bg-blue-100 text-blue-900" : "bg-transparent text-sidebar-foreground/80 hover:bg-sidebar-accent hover:text-sidebar-primary",
"focus-visible:ring-2 focus-visible:ring-sidebar-ring outline-none"
)}
data-state={isExpanded ? 'open' : 'closed'}
>
<span className="flex-1 font-medium text-sm text-left truncate">{group.Name}</span>
<p className="ml-auto font-semibold text-xs opacity-70">{group.Entities.length}</p>
<a
className={cn(
"p-1 rounded focus:outline-none focus:ring-2 focus:ring-blue-400",
currentSection?.toLowerCase() === (group.Entities[0]?.SchemaName?.toLowerCase())
? "bg-blue-100 text-blue-900"
: "hover:bg-gray-200 text-gray-400 hover:text-blue-700"
)}
onClick={e => {
e.stopPropagation();
handleGroupClick(group.Name);
if (group.Entities.length > 0) handleSectionClick(group.Entities[0].SchemaName);
}}
aria-label={`Link to first entity in ${group.Name}`}
tabIndex={0}
>
<ExternalLink className="w-4 h-4" onClick={e => {
e.preventDefault();
handleGroupClick(group.Name);
if (group.Entities.length > 0) handleSectionClick(group.Entities[0].SchemaName);
}} />
</a>
</CollapsibleTrigger>
</Slot>
<CollapsibleContent>
<div className="flex flex-col w-full gap-1 py-1">
{group.Entities.map(entity => {
const isCurrentSection = currentSection?.toLowerCase() === entity.SchemaName.toLowerCase()
const isMatch = isEntityMatch(entity);
// If searching and this entity doesn't match, don't render it
if (searchTerm.trim() && !isMatch) {
return null;
}
return (
<button
className={cn(
"flex items-center gap-2 rounded-full px-3 py-1 cursor-pointer transition-colors text-xs font-medium",
"hover:bg-blue-50 hover:text-blue-900 text-sidebar-foreground/60",
isCurrentSection ? "bg-blue-100 text-blue-900" : "",
isMatch ? "ring-1 ring-yellow-300" : ""
)}
key={entity.SchemaName}
onClick={() => {
handleGroupClick(group.Name)
handleSectionClick(entity.SchemaName)
}}
>
{entity.IconBase64 ? <img className="h-4 w-4" src={`data:image/svg+xml;base64,${entity.IconBase64}`} alt="icon" /> : <Puzzle className="w-4 h-4" />}
<span className="truncate">
{isMatch ? highlightText(entity.DisplayName, searchTerm) : entity.DisplayName}
</span>
</button>
)
})}
</div>
</CollapsibleContent>
</div>
</Collapsible>
)
}
return (
<div className="flex flex-col">
{/* Search Bar */}
<div className="px-2 pb-3">
<div className="relative mt-2">
<Search className="absolute left-2 top-1/2 transform -translate-y-1/2 w-4 h-4 text-gray-400" />
<Input
type="text"
placeholder="Search tables..."
aria-label="Search tables"
value={searchTerm}
onChange={(e) => handleSearch(e.target.value)}
className="pl-8 pr-8 h-8 text-xs"
/>
{searchTerm && (
<button
onClick={clearSearch}
className="absolute right-2 top-1/2 transform -translate-y-1/2 p-0.5 rounded hover:bg-gray-100"
>
<X className="w-3 h-3 text-gray-400" />
</button>
)}
</div>
</div>
<div className='h-full gap-1 flex flex-col max-w-48 overflow-y-auto overflow-x-hidden'>
{
groups.map((group) =>
<NavItem key={group.Name} group={group} />
)
}
</div>
</div>
);
}