-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalSearchResultsView.tsx
229 lines (209 loc) · 7.61 KB
/
GlobalSearchResultsView.tsx
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
import { List, ActionPanel, Action, Icon, showToast, Toast, getPreferenceValues } from "@raycast/api";
import { useState, useEffect } from "react";
import { useExec } from "@raycast/utils";
import { exec } from "child_process";
import { promisify } from "util";
// Define minimal Org type.
type Org = {
username: string;
alias: string;
instanceUrl: string;
};
// Type for each search record returned from the FIND query.
type SearchRecord = {
Id: string;
Name: string;
attributes: {
type: string;
};
};
// Overall structure of the FIND query result.
type SearchResult = {
result: {
searchRecords: SearchRecord[];
};
};
// Type for a searchable object (from EntityDefinition query)
type SearchableObject = {
QualifiedApiName: string;
Label: string;
DurableId: string;
};
// Define Preferences interface for your extension.
interface Preferences {
searchLimit: string;
}
export default function GlobalSearchResultsView({ org }: { org: Org }) {
const targetOrg = org.alias || org.username;
const preferences = getPreferenceValues<Preferences>();
const limitValue = parseInt(preferences.searchLimit, 10) || 50;
const [searchText, setSearchText] = useState("");
const [records, setRecords] = useState<SearchRecord[]>([]);
const [searchableObjects, setSearchableObjects] = useState<SearchableObject[]>([]);
const [selectedType, setSelectedType] = useState("all");
// Step 1: Retrieve Searchable Objects from Salesforce
const searchableQuery = `sf data query --query "SELECT QualifiedApiName, Label, DurableId FROM EntityDefinition WHERE IsSearchable = true AND IsQueryable = true AND IsCustomSetting = false AND IsDeprecatedAndHidden = false ORDER BY QualifiedApiName" --target-org "${targetOrg}" --json`;
const { data: searchableData } = useExec(searchableQuery, [], { shell: true });
useEffect(() => {
async function parseSearchableObjects() {
if (searchableData) {
try {
const parsed = JSON.parse(searchableData);
const objs: SearchableObject[] = parsed.result.records || [];
const filteredObjs = objs.filter(
(obj) => !["AssetRelationship", "AssignmentRule"].includes(obj.QualifiedApiName),
);
const customObjects = filteredObjs.filter((obj) => obj.DurableId !== obj.QualifiedApiName);
setSearchableObjects(customObjects);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
await showToast({
style: Toast.Style.Failure,
title: "Failed to parse searchable objects",
message: errorMessage,
});
}
}
}
parseSearchableObjects();
}, [searchableData]);
// Step 2: Build a Dynamic FIND Query
const standardFragments = [
"Account(Id,Name)",
"Contact(Id,Name)",
"Opportunity(Id,Name)",
"Campaign(Id,Name)",
"Case(Id,CaseNumber)",
"Product2(Id,Name)",
];
const customFragments =
searchableObjects.length > 0 ? searchableObjects.map((obj) => `${obj.QualifiedApiName}(Id,Name)`) : [];
const returningClause = standardFragments.concat(customFragments).join(", ");
const trimmedSearchText = searchText.trim();
const shouldSearch = trimmedSearchText.length > 0;
const sanitizedSearchText = trimmedSearchText.replace(/[^\w\s.@]/g, "");
const findQuery =
shouldSearch && returningClause
? `FIND {${sanitizedSearchText}} IN ALL FIELDS RETURNING ${returningClause} LIMIT ${limitValue}`
: "";
const queryCommand =
shouldSearch && findQuery.length > 0
? `sf data search --query "${findQuery}" --target-org "${targetOrg}" --json`
: `echo '{ "result": { "searchRecords": [] } }'`;
// Step 3: Execute the FIND Query
const { isLoading, data, revalidate } = useExec(queryCommand, [], { shell: true });
useEffect(() => {
async function parseSearchResults() {
if (data) {
try {
const parsed: SearchResult = JSON.parse(data);
setRecords(parsed.result.searchRecords || []);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
await showToast({
style: Toast.Style.Failure,
title: "Failed to parse search results",
message: errorMessage,
});
}
} else {
setRecords([]);
}
}
parseSearchResults();
}, [data]);
const filteredRecords =
selectedType === "all" ? records : records.filter((record) => record.attributes.type === selectedType);
// Step 4: Action Handlers
async function handleOpenRecord(record: SearchRecord) {
try {
const relativeRecordPath = `/lightning/r/${record.attributes.type}/${record.Id}/view`;
const execPromise = promisify(exec);
await execPromise(`sf org open -p "${relativeRecordPath}" --target-org "${targetOrg}"`);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
await showToast({
style: Toast.Style.Failure,
title: "Failed to open record",
message: errorMessage,
});
}
}
async function handleSearchInBrowser() {
if (!trimmedSearchText) {
await showToast({
style: Toast.Style.Failure,
title: "Please enter a search term",
});
return;
}
const payload = {
componentDef: "forceSearch:searchPageDesktop",
attributes: {
term: trimmedSearchText,
scopeMap: { type: "TOP_RESULTS" },
groupId: "DEFAULT",
},
state: {},
};
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString("base64");
const fullUrl = `${org.instanceUrl}/one/one.app#${encodedPayload}`;
try {
const execPromise = promisify(exec);
await execPromise(`sf org open -p "${fullUrl}" --target-org "${targetOrg}"`);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
await showToast({
style: Toast.Style.Failure,
title: "Global Search failed",
message: errorMessage,
});
}
}
return (
<List
isLoading={isLoading}
searchText={searchText}
onSearchTextChange={setSearchText}
throttle
navigationTitle="Salesforce Global Search"
searchBarAccessory={
<List.Dropdown tooltip="Filter By SObject" onChange={setSelectedType} value={selectedType}>
<List.Dropdown.Item title="All" value="all" />
{Array.from(new Set(records.map((r) => r.attributes.type)))
.sort()
.map((type) => (
<List.Dropdown.Item key={type} title={type} value={type} />
))}
</List.Dropdown>
}
actions={
<ActionPanel>
<Action title="Search in Browser" icon={Icon.Globe} onAction={handleSearchInBrowser} />
<Action title="Reload" icon={Icon.ArrowClockwise} onAction={() => revalidate()} />
</ActionPanel>
}
>
<List.Section title="Search Results">
{filteredRecords.map((record) => (
<List.Item
key={record.Id}
title={record.Name}
subtitle={record.attributes.type}
icon={Icon.Document}
actions={
<ActionPanel>
<Action
title={`Open ${record.attributes.type} Record`}
icon={Icon.Globe}
onAction={() => handleOpenRecord(record)}
/>
<Action title="Search in Browser" icon={Icon.Globe} onAction={handleSearchInBrowser} />
</ActionPanel>
}
/>
))}
</List.Section>
</List>
);
}