-
Notifications
You must be signed in to change notification settings - Fork 40
/
home.ts
156 lines (144 loc) · 4.28 KB
/
home.ts
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
import {
CLITemplate,
Home,
type HomeDispatchedSearchResult,
type HomeProvider,
type HomeRegistration,
type HomeSearchListenerRequest,
type HomeSearchListenerResponse,
type HomeSearchResponse
} from "@openfin/workspace";
import { getHelpSearchEntries, getSearchResults, initializeSources, itemSelection } from "./sources";
/**
* Register with the home component.
* @param id The id to register the provider with.
* @param title The title to use for the home registration.
* @param icon The icon to use for the home registration.
* @returns The registration details for home.
*/
export async function register(id: string, title: string, icon: string): Promise<HomeRegistration> {
console.log("Initializing home.");
let lastResponse: HomeSearchListenerResponse;
/**
* The callback fired when the user types in the home query.
* @param request The request object from the home component.
* @param response The response to use for async updates.
* @returns The results to display in home.
*/
async function onUserInput(
request: HomeSearchListenerRequest,
response: HomeSearchListenerResponse
): Promise<HomeSearchResponse> {
const queryLower = request.query.toLowerCase();
// If the query starts with a ? treat this as a help request
// so we don't have any additional entries to show
if (queryLower.startsWith("?")) {
return { results: await getHelpSearchEntries() };
}
if (lastResponse !== undefined) {
lastResponse.close();
}
lastResponse = response;
lastResponse.open();
if (queryLower.startsWith("/loading")) {
// Example command showing the default loading template
// Selecting the template will show a loaded page
return {
results: [
{
key: "loading-0001",
title: "Example loading indicator",
label: "Information",
actions: [],
data: {},
template: CLITemplate.Loading,
templateContent: ""
}
]
};
} else if (queryLower.startsWith("/error")) {
// Example command showing the default error template
// Selecting or reloading the template will show a loaded page
return {
results: [
{
key: "error-0001",
title: "Example error indicator",
label: "Information",
actions: [],
data: {},
template: CLITemplate.Error,
templateContent: ""
}
]
};
}
return getSearchResults(request.query, [], lastResponse, {
isSuggestion: request.context?.isSuggestion ?? false
});
}
/**
* The callback fired when a selection is made in home.
* @param result The item that was selected in home.
*/
async function onSelection(result: HomeDispatchedSearchResult): Promise<void> {
if (result.data !== undefined) {
let handled = false;
if (result.action.trigger === "user-action" && result.key === "loading-0001") {
lastResponse.revoke("loading-0001");
lastResponse.respond([
{
key: "content-0001",
title: "Example Content",
label: "Information",
actions: [],
data: {},
template: CLITemplate.SimpleText,
templateContent: "Result loaded"
}
]);
handled = true;
} else if (
result.key === "error-0001" &&
(result.action.trigger === "user-action" || result.action.trigger === "reload")
) {
lastResponse.revoke("error-0001");
lastResponse.respond([
{
key: "content-0001",
title: "Example Content",
label: "Information",
actions: [],
data: {},
template: CLITemplate.SimpleText,
templateContent: "Result loaded"
}
]);
handled = true;
} else if (result.action.trigger === "user-action" && result.key === "content-0001") {
lastResponse.revoke("content-0001");
handled = true;
}
if (!handled) {
await itemSelection(result, lastResponse);
}
}
}
// Important to note we enable the dispatchFocusEvents flag
// which means we receive `focus-change` events in the onSelection
// callback allowing us to lazy load a template
const homeProvider: HomeProvider = {
id,
title,
icon,
onUserInput,
onResultDispatch: onSelection,
dispatchFocusEvents: true
};
const homeRegistration = await Home.register(homeProvider);
console.log("Home configured.");
console.log(homeRegistration);
// Initialize all the data sources
await initializeSources(homeRegistration);
return homeRegistration;
}