Skip to content

Commit 16121ff

Browse files
committed
Added option to set secondary search provider
1 parent 2c0491a commit 16121ff

File tree

9 files changed

+79
-25
lines changed

9 files changed

+79
-25
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### v2.3.0 (TBA)
22
- Added custom theme editor ([#246](https://github.com/pawelmalak/flame/issues/246))
3+
- Added option to set secondary search provider ([#295](https://github.com/pawelmalak/flame/issues/295))
34
- Fixed bug where pressing Enter with empty search bar would redirect to search results ([#325](https://github.com/pawelmalak/flame/issues/325))
45
- Fixed bug where user could create empty app or bookmark which was causing page to go blank ([#332](https://github.com/pawelmalak/flame/issues/332))
56
- Added new theme: Mint

client/src/components/SearchBar/SearchBar.tsx

+16-11
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,22 @@ export const SearchBar = (props: Props): JSX.Element => {
6464
};
6565

6666
const searchHandler = (e: KeyboardEvent<HTMLInputElement>) => {
67-
const { isLocal, search, query, isURL, sameTab, rawQuery } = searchParser(
68-
inputRef.current.value
69-
);
67+
const {
68+
isLocal,
69+
encodedURL,
70+
primarySearch,
71+
secondarySearch,
72+
isURL,
73+
sameTab,
74+
rawQuery,
75+
} = searchParser(inputRef.current.value);
7076

7177
if (isLocal) {
72-
setLocalSearch(search);
78+
setLocalSearch(encodedURL);
7379
}
7480

7581
if (e.code === 'Enter' || e.code === 'NumpadEnter') {
76-
if (!query.prefix) {
82+
if (!primarySearch.prefix) {
7783
// Prefix not found -> emit notification
7884
createNotification({
7985
title: 'Error',
@@ -91,21 +97,20 @@ export const SearchBar = (props: Props): JSX.Element => {
9197
redirectUrl(bookmarkSearchResult[0].bookmarks[0].url, sameTab);
9298
} else {
9399
// no local results -> search the internet with the default search provider if query is not empty
94-
95100
if (!/^ *$/.test(rawQuery)) {
96-
let template = query.template;
101+
let template = primarySearch.template;
97102

98-
if (query.prefix === 'l') {
99-
template = 'https://duckduckgo.com/?q=';
103+
if (primarySearch.prefix === 'l') {
104+
template = secondarySearch.template;
100105
}
101106

102-
const url = `${template}${search}`;
107+
const url = `${template}${encodedURL}`;
103108
redirectUrl(url, sameTab);
104109
}
105110
}
106111
} else {
107112
// Valid query -> redirect to search results
108-
const url = `${query.template}${search}`;
113+
const url = `${primarySearch.template}${encodedURL}`;
109114
redirectUrl(url, sameTab);
110115
}
111116
} else if (e.code === 'Escape') {

client/src/components/Settings/GeneralSettings/GeneralSettings.tsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const GeneralSettings = (): JSX.Element => {
167167
{/* === SEARCH OPTIONS === */}
168168
<SettingsHeadline text="Search" />
169169
<InputGroup>
170-
<label htmlFor="defaultSearchProvider">Default search provider</label>
170+
<label htmlFor="defaultSearchProvider">Primary search provider</label>
171171
<select
172172
id="defaultSearchProvider"
173173
name="defaultSearchProvider"
@@ -186,6 +186,34 @@ export const GeneralSettings = (): JSX.Element => {
186186
</select>
187187
</InputGroup>
188188

189+
{formData.defaultSearchProvider === 'l' && (
190+
<InputGroup>
191+
<label htmlFor="secondarySearchProvider">
192+
Secondary search provider
193+
</label>
194+
<select
195+
id="secondarySearchProvider"
196+
name="secondarySearchProvider"
197+
value={formData.secondarySearchProvider}
198+
onChange={(e) => inputChangeHandler(e)}
199+
>
200+
{[...queries, ...customQueries].map((query: Query, idx) => {
201+
const isCustom = idx >= queries.length;
202+
203+
return (
204+
<option key={idx} value={query.prefix}>
205+
{isCustom && '+'} {query.name}
206+
</option>
207+
);
208+
})}
209+
</select>
210+
<span>
211+
Will be used when "Local search" is primary search provider and
212+
there are not any local results
213+
</span>
214+
</InputGroup>
215+
)}
216+
189217
<InputGroup>
190218
<label htmlFor="searchSameTab">
191219
Open search results in the same tab

client/src/interfaces/Config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface Config {
1717
hideCategories: boolean;
1818
hideSearch: boolean;
1919
defaultSearchProvider: string;
20+
secondarySearchProvider: string;
2021
dockerApps: boolean;
2122
dockerHost: string;
2223
kubernetesApps: boolean;

client/src/interfaces/Forms.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface WeatherForm {
1010

1111
export interface GeneralForm {
1212
defaultSearchProvider: string;
13+
secondarySearchProvider: string;
1314
searchSameTab: boolean;
1415
pinAppsByDefault: boolean;
1516
pinCategoriesByDefault: boolean;

client/src/interfaces/SearchResult.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export interface SearchResult {
44
isLocal: boolean;
55
isURL: boolean;
66
sameTab: boolean;
7-
search: string;
8-
query: Query;
7+
encodedURL: string;
8+
primarySearch: Query;
9+
secondarySearch: Query;
910
rawQuery: string;
1011
}

client/src/utility/searchParser.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { queries } from './searchQueries.json';
2-
import { Query, SearchResult } from '../interfaces';
2+
import { SearchResult } from '../interfaces';
33
import { store } from '../store/store';
44
import { isUrlOrIp } from '.';
55

@@ -8,8 +8,13 @@ export const searchParser = (searchQuery: string): SearchResult => {
88
isLocal: false,
99
isURL: false,
1010
sameTab: false,
11-
search: '',
12-
query: {
11+
encodedURL: '',
12+
primarySearch: {
13+
name: '',
14+
prefix: '',
15+
template: '',
16+
},
17+
secondarySearch: {
1318
name: '',
1419
prefix: '',
1520
template: '',
@@ -25,27 +30,37 @@ export const searchParser = (searchQuery: string): SearchResult => {
2530
// Match prefix and query
2631
const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
2732

33+
// Extract prefix
2834
const prefix = splitQuery ? splitQuery[1] : config.defaultSearchProvider;
2935

30-
const search = splitQuery
36+
// Encode url
37+
const encodedURL = splitQuery
3138
? encodeURIComponent(splitQuery[2])
3239
: encodeURIComponent(searchQuery);
3340

34-
const query = [...queries, ...customQueries].find(
35-
(q: Query) => q.prefix === prefix
36-
);
41+
// Find primary search engine template
42+
const findProvider = (prefix: string) => {
43+
return [...queries, ...customQueries].find((q) => q.prefix === prefix);
44+
};
45+
46+
const primarySearch = findProvider(prefix);
47+
const secondarySearch = findProvider(config.secondarySearchProvider);
3748

38-
// If search provider was found
39-
if (query) {
40-
result.query = query;
41-
result.search = search;
49+
// If search providers were found
50+
if (primarySearch) {
51+
result.primarySearch = primarySearch;
52+
result.encodedURL = encodedURL;
4253

4354
if (prefix === 'l') {
4455
result.isLocal = true;
4556
} else {
4657
result.sameTab = config.searchSameTab;
4758
}
4859

60+
if (secondarySearch) {
61+
result.secondarySearch = secondarySearch;
62+
}
63+
4964
return result;
5065
}
5166

client/src/utility/templateObjects/configTemplate.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const configTemplate: Config = {
1717
hideCategories: false,
1818
hideSearch: false,
1919
defaultSearchProvider: 'l',
20+
secondarySearchProvider: 'd',
2021
dockerApps: false,
2122
dockerHost: 'localhost',
2223
kubernetesApps: false,

utils/init/initialConfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"hideCategories": false,
1616
"hideSearch": false,
1717
"defaultSearchProvider": "l",
18+
"secondarySearchProvider": "d",
1819
"dockerApps": false,
1920
"dockerHost": "localhost",
2021
"kubernetesApps": false,

0 commit comments

Comments
 (0)