-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathindex.jsx
More file actions
170 lines (159 loc) · 5.16 KB
/
Copy pathindex.jsx
File metadata and controls
170 lines (159 loc) · 5.16 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
import React, { useState, useRef, useEffect } from "react";
import styles from "./styles.module.css";
const COMMAND_CATEGORIES = {
"Create Record Commands": [
"Create Record - Contact",
"Create Record - Lead",
"Create Record - Opportunity",
"Create Record - Task",
"Create Record - Account",
"Create Record - Any",
],
"Update Record Commands": [
"Update Record - Lead",
"Update Record - Contact",
"Update Record - Opportunity",
"Update Record - Task",
"Update Record - Account",
"Update Record - Any",
],
"Get Record by ID Commands": [
"Get Record - Lead",
"Get Record - Contact",
"Get Record - Opportunity",
"Get Record - Task",
"Get Record - Account",
"Get Record - Any",
],
"Search Commands": [
"Search Record - Lead",
"Search Record - Contact",
"Search Record - Opportunity",
"Search Record - Task",
"Search Record - Account",
"Search Record - Any",
],
"Get Records Commands": [
"Get Record by View ID - Lead",
"Get Record by View ID - Contact",
"Get Record by View ID - Opportunity",
"Get Record by View ID - Task",
"Get Record by View ID - Account",
"Get Record by View ID - Any",
],
"Create Custom Fields Commands": [
"Create Custom Field - Lead",
"Create Custom Field - Contact",
"Create Custom Field - Opportunity",
"Create Custom Field - Task",
"Create Custom Field - Account",
"Create Custom Field - Any",
],
"Query Commands": [
"Write SOQL Query",
],
"Object Management Commands": [
"Create Custom Object"
],
"Metadata Commands": [
"Describe Action Schema"
],
"Custom Action": [
"Custom Action"
]
};
export default function CommandDropdown({ onSelect }) {
const [isOpen, setIsOpen] = useState(false);
const [selectedCommand, setSelectedCommand] = useState("");
const [searchQuery, setSearchQuery] = useState("");
const [filteredCommands, setFilteredCommands] = useState(COMMAND_CATEGORIES);
const [dropdownPosition, setDropdownPosition] = useState("bottom");
const dropdownRef = useRef(null);
const menuRef = useRef(null);
useEffect(() => {
if (isOpen && dropdownRef.current && menuRef.current) {
const dropdownRect = dropdownRef.current.getBoundingClientRect();
const menuHeight = menuRef.current.offsetHeight;
const spaceBelow = window.innerHeight - dropdownRect.bottom;
const spaceAbove = dropdownRect.top;
if (spaceBelow < menuHeight && spaceAbove > menuHeight) {
setDropdownPosition("top");
} else {
setDropdownPosition("bottom");
}
}
}, [isOpen]);
useEffect(() => {
if (searchQuery.trim() === "") {
setFilteredCommands(COMMAND_CATEGORIES);
} else {
const newFiltered = Object.entries(COMMAND_CATEGORIES).reduce(
(acc, [category, commands]) => {
const filtered = commands.filter((command) =>
command.toLowerCase().includes(searchQuery.toLowerCase())
);
if (filtered.length > 0) acc[category] = filtered;
return acc;
},
{}
);
setFilteredCommands(newFiltered);
}
}, [searchQuery]);
const toggleDropdown = () => {
setIsOpen(!isOpen);
setSearchQuery(""); // Reset search when reopening
};
const handleSelect = (command) => {
setSelectedCommand(command);
onSelect(command);
setIsOpen(false);
};
return (
<div className={styles.container} ref={dropdownRef}>
<div className={styles.dropdownHeader} onClick={toggleDropdown}>
{selectedCommand || "Select a command..."}
<span className={`${styles.arrow} ${isOpen ? styles.open : ""}`}>▾</span>
</div>
{isOpen && (
<div
className={`${styles.dropdownMenu} ${dropdownPosition === "top" ? styles.menuTop : styles.menuBottom}`}
ref={menuRef}
>
{/* Fixed Search Box */}
<div className={styles.fixedSearchContainer}>
<input
type="text"
className={styles.searchInput}
placeholder="Search from 39 commands..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
/>
</div>
<ul className={styles.commandList}>
{Object.entries(filteredCommands).length > 0 ? (
Object.entries(filteredCommands).map(([category, commands]) => (
<li key={category} className={styles.category}>
<span className={styles.categoryTitle}>{category}</span>
<ul className={styles.commands}>
{commands.map((command) => (
<li
key={command}
className={`${styles.commandItem} ${selectedCommand === command ? styles.selected : ""}`}
onClick={() => handleSelect(command)}
>
{command}
</li>
))}
</ul>
</li>
))
) : (
<li className={styles.noResults}>No commands found</li>
)}
</ul>
</div>
)}
</div>
);
}