Skip to content

Commit

Permalink
fix: lift functions, remove magic string
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Oct 25, 2024
1 parent 44d717b commit 860cf0f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
22 changes: 12 additions & 10 deletions components/brave_new_tab_ui/components/search/EngineContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ const EngineContext = React.createContext<Engine>({
setEngineConfig: () => { },
})

const searchEngineConfig = () => {
const localStorageValue = localStorage.getItem(ENABLED_SEARCH_ENGINES_KEY);
if (localStorageValue) {
return JSON.parse(localStorageValue);
}
return {
// Default to enabling Brave Search
[braveSearchHost]: true
};
}

export function EngineContextProvider(props: React.PropsWithChildren<{}>) {
const [lastUsed, setLastUsed] = useNewTabPref('lastUsedNtpSearchEngine')
const lastLocalStorage = localStorage.getItem(LAST_SEARCH_ENGINE_KEY)
const [config, setConfig] = React.useState<Record<string, boolean>>(() => {
const localStorageValue = localStorage.getItem(ENABLED_SEARCH_ENGINES_KEY);
if (localStorageValue) {
return JSON.parse(localStorageValue);
}
return {
// Default to enabling Brave Search
[braveSearchHost]: true
};
});
const [config, setConfig] = React.useState<Record<string, boolean>>(searchEngineConfig);

const setLastNtpSearchEngine = React.useCallback((engine: SearchEngineInfo) => {
setLastUsed(engine.host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ const Hr = styled.hr`
`

const CheckboxText = styled.span`flex: 1`;
const hasEnabledEngine = (config: Record<string, boolean>) => Object.keys(config).some(key => config[key])

export default function SearchSettings() {
const { result: engines = [] } = usePromise(() => searchEnginesPromise, [])
const [showSearchBox, setShowSearchBox] = useNewTabPref('showSearchBox')
const { setEngineConfig, engineConfig } = useEngineContext()
const hasEnabledEngine = (config: Record<string, boolean>) => Object.keys(config).some(key => config[key])

React.useEffect(() => {
if (!hasEnabledEngine(engineConfig)) {
Expand Down
3 changes: 2 additions & 1 deletion components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Utils
import { debounce } from '../../common/debounce'
import { loadTimeData } from '../../common/loadTimeData'
import { braveSearchHost } from '../components/search/config'

export const keyName = 'new-tab-data'

Expand All @@ -28,7 +29,7 @@ export const defaultState: NewTab.State = {
showRewards: false,
showBraveTalk: false,
showSearchBox: true,
lastUsedNtpSearchEngine: "search.brave.com",
lastUsedNtpSearchEngine: braveSearchHost,
promptEnableSearchSuggestions: true,
searchSuggestionsEnabled: false,
showBitcoinDotCom: false,
Expand Down

0 comments on commit 860cf0f

Please sign in to comment.