Skip to content

Commit 9aa0966

Browse files
BinlogoFolyd
authored andcommitted
feat: support dropdown to switch search type
1 parent 2934521 commit 9aa0966

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

raycast-extension/src/index.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,33 @@ import { useState, useEffect } from "react";
33
import { initHeadlessOmnibox } from "./headless.js";
44
import { HeadlessOmnibox } from "omnibox-js";
55

6+
type SearchType = { id: string; name: string, prefixQueryEvent: string };
7+
8+
function SearchTypeDropdown(props: { searchTypes: SearchType[]; onSearchTypeChange: (newValue: string) => void }) {
9+
const { searchTypes, onSearchTypeChange } = props;
10+
return (
11+
<List.Dropdown
12+
tooltip="Select Search Type"
13+
storeValue={true}
14+
onChange={(newValue) => {
15+
onSearchTypeChange(newValue);
16+
}}
17+
>
18+
<List.Dropdown.Section>
19+
{searchTypes.map((searchType) => (
20+
<List.Dropdown.Item key={searchType.id} title={searchType.name} value={searchType.prefixQueryEvent} />
21+
))}
22+
</List.Dropdown.Section>
23+
</List.Dropdown>
24+
);
25+
}
26+
627
export default function Command() {
28+
const [searchText, setSearchText] = useState("");
729
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
830
const [isLoading, setIsLoading] = useState(true);
31+
32+
const [prefixQueryEvent, setPrefixQueryEvent] = useState("");
933
const [headless, setHeadless] = useState<HeadlessOmnibox | null>(null);
1034

1135
useEffect(() => {
@@ -18,6 +42,8 @@ export default function Command() {
1842
}, []);
1943

2044
const handleSearch = async (text: string) => {
45+
setSearchText(text);
46+
2147
if (!headless) return;
2248

2349
if (text.length === 0) {
@@ -26,7 +52,12 @@ export default function Command() {
2652
}
2753

2854
setIsLoading(true);
29-
const { results } = await headless.search(text);
55+
const { results } = await headless.search(prefixQueryEvent + text);
56+
// Remove pagination tip from the first result
57+
const paginationTipRegex = /\s\|\sPage\s\[\d+\/\d+\],\sappend\s'.+?'\sto\s?page\sdown/;
58+
if (results.length > 0) {
59+
results[0].description = results[0].description.replace(paginationTipRegex, "");
60+
}
3061
setSearchResults(
3162
results.map((result) => ({
3263
name: result.content,
@@ -37,8 +68,26 @@ export default function Command() {
3768
setIsLoading(false);
3869
};
3970

71+
const searchTypes: SearchType[] = [
72+
{ id: "1", name: "Std", prefixQueryEvent: "" },
73+
{ id: "2", name: "Docs", prefixQueryEvent: "!" },
74+
{ id: "3", name: "Crates", prefixQueryEvent: "!!" },
75+
{ id: "4", name: "Repository", prefixQueryEvent: "!!!" },
76+
];
77+
const onSearchTypeChange = (newValue: string) => {
78+
setPrefixQueryEvent(newValue);
79+
handleSearch(searchText);
80+
};
81+
4082
return (
41-
<List isLoading={isLoading} onSearchTextChange={handleSearch} searchBarPlaceholder="Search..." throttle>
83+
<List
84+
isLoading={isLoading}
85+
searchText={searchText}
86+
onSearchTextChange={handleSearch}
87+
searchBarPlaceholder="Search..."
88+
searchBarAccessory={<SearchTypeDropdown searchTypes={searchTypes} onSearchTypeChange={onSearchTypeChange} />}
89+
throttle
90+
>
4291
{searchResults.map((searchResult) => (
4392
<SearchListItem key={searchResult.url} searchResult={searchResult} />
4493
))}

0 commit comments

Comments
 (0)