-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from RyKilleen/web-search
feat: add web search options for google, duckduckgo, and stack overflow
- Loading branch information
Showing
13 changed files
with
291 additions
and
169 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { appWindow } from "@tauri-apps/api/window"; | ||
import { useEffect, useState } from "react"; | ||
import { Autocomplete } from "../Autocomplete"; | ||
import { AppEvents, getConfig } from "../../utils"; | ||
import createWorkflowSource from "./workflow-source"; | ||
import createSettingsSource from "./settings-source"; | ||
import createWebSearchSource, { searchEngines } from "./websearch-source"; | ||
|
||
const focusSearchBar = () => { | ||
let input = document.querySelector(".aa-Input") as HTMLElement | null; | ||
input?.focus(); | ||
}; | ||
|
||
function getQueryPattern(query: string, flags = "i") { | ||
const pattern = new RegExp( | ||
`(${query | ||
.trim() // Trim leading and ending whitespace | ||
.toLowerCase() // convert to lower case | ||
.split(" ") // Split on spaces for multiple commands | ||
.map((token) => `^${token}`) // Map over the resulting array and create Regex_ | ||
.join("|")})`, // Join those expressions with an OR | | ||
flags | ||
); | ||
|
||
return pattern; | ||
} | ||
|
||
// function highlight(text: string, pattern: RegExp) { | ||
// // Split the text based on the pattern | ||
// const tokens = text.split(pattern); | ||
|
||
// // Map over the split text and test against the pattern | ||
// return tokens.map((token) => { | ||
// // If the pattern matches the text, wrap the text in <mark> | ||
// if (!pattern.test("") && pattern.test(token)) { | ||
// return <mark>{token}</mark>; | ||
// } | ||
|
||
// // return the token back to the array | ||
// return token; | ||
// }); | ||
// } | ||
|
||
const Omnibar = () => { | ||
const [suggestions, setSuggestions] = useState<string[]>([]); | ||
async function setStoredConfigChoices() { | ||
let state = await getConfig(); | ||
setSuggestions(state.workflows.map((wf) => wf.name)); | ||
} | ||
useEffect(() => { | ||
const unlisten1 = appWindow.listen( | ||
AppEvents.OmnibarFocused, | ||
focusSearchBar | ||
); | ||
const unlisten2 = appWindow.listen( | ||
AppEvents.AppStateUpdated, | ||
setStoredConfigChoices | ||
); | ||
|
||
return () => { | ||
unlisten1(); | ||
unlisten2(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
setStoredConfigChoices(); | ||
}, []); | ||
|
||
return ( | ||
<div style={{ background: "rgb(0 0 0 / 0%)" }}> | ||
<form> | ||
<Autocomplete | ||
placeholder="" | ||
openOnFocus | ||
autoFocus | ||
defaultActiveItemId={0} | ||
getSources={({ query }: { query: string }) => { | ||
const pattern = getQueryPattern(query); | ||
const webSearchSource = createWebSearchSource({ query }); | ||
const defaultSources = [ | ||
createWorkflowSource({ suggestions, pattern }), | ||
createSettingsSource({ pattern }), | ||
webSearchSource, | ||
]; | ||
const searchEngineCodes = searchEngines.map((se) => se.shortCode); | ||
const isWebSearch = | ||
query.includes("?") && | ||
searchEngineCodes.some((v: string) => query.includes(v)); | ||
|
||
return isWebSearch ? [webSearchSource] : defaultSources; | ||
}} | ||
/> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Omnibar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { invoke } from "@tauri-apps/api"; | ||
import { Commands } from "../../utils"; | ||
import { Action } from "../Autocomplete"; | ||
|
||
const createSettingsSource = ({ pattern }: { pattern: RegExp }) => ({ | ||
sourceId: "settings", | ||
getItemInputValue({ item }: { item: any }) { | ||
return item.label; | ||
}, | ||
getItems({ state }: { state: any }) { | ||
return [{ label: "Settings", action: Commands.OpenSettings }].filter( | ||
({ label }) => pattern.test(label) | ||
); | ||
//TODO: add result highlighting | ||
// .map((action) => ({ | ||
// ...action, | ||
// highlighted: highlight(action.label, pattern), | ||
// })); | ||
}, | ||
// Run this code when item is selected | ||
onSelect(params: any) { | ||
// item is the full item data | ||
// setQuery is a hook to set the query state | ||
const { item, setQuery } = params; | ||
|
||
invoke(item.action); | ||
setQuery(""); | ||
}, | ||
// Templates for Header of this source and Items in this source | ||
templates: { | ||
header() { | ||
return <h2>Settings</h2>; | ||
}, | ||
item({ item }: { item: any }) { | ||
return <Action hit={item} />; | ||
}, | ||
}, | ||
}); | ||
|
||
export default createSettingsSource; |
Oops, something went wrong.