From 935aaecfba4bcf7dc89c5cada6a7aa1a6e075087 Mon Sep 17 00:00:00 2001 From: WhistlingZephyr <90854121+WhistlingZephyr@users.noreply.github.com> Date: Sat, 21 Oct 2023 04:58:08 +0530 Subject: [PATCH] feat: remove Brave bangs --- src/backends/brave.ts | 28 ------------------ src/helpers/bang.ts | 18 +++--------- src/helpers/import-export.ts | 54 ++++++++++------------------------ src/pages/background.ts | 4 +-- src/pages/options/settings.tsx | 23 +-------------- tests/backends.test.ts | 9 ------ 6 files changed, 23 insertions(+), 113 deletions(-) delete mode 100644 src/backends/brave.ts diff --git a/src/backends/brave.ts b/src/backends/brave.ts deleted file mode 100644 index ffd0b23..0000000 --- a/src/backends/brave.ts +++ /dev/null @@ -1,28 +0,0 @@ -import Backend from '../models/backend'; -import type {Bang} from '../models/backend'; -import {withFormatting, withScheme} from '../utils/url'; - -type BraveBang = { - bang: string; - meta: { - scheme: string; - netloc: string; - hostname: string; - favicon: string; - path: string; - }; - category: string; - sub_category: string; - title: string; - url: string; -}; - -export default class Brave extends Backend { - url = 'https://search.brave.com/api/bang'; - bangReplacement = '{query}'; - formatBang = (bang: BraveBang): Bang => ({ - shortcut: bang.bang, - domain: withFormatting(withScheme(bang.meta.hostname, 'https')), - url: withFormatting(withScheme(bang.url, 'https')), - }); -} diff --git a/src/helpers/bang.ts b/src/helpers/bang.ts index d0c30ac..8b813bf 100644 --- a/src/helpers/bang.ts +++ b/src/helpers/bang.ts @@ -1,4 +1,3 @@ -import Brave from '../backends/brave'; import DuckDuckGo from '../backends/ddg'; import type {BangConfig, CustomBang} from '../models/backend'; import ConfigBangs from '../models/config-bangs'; @@ -7,16 +6,8 @@ import {isUrl} from '../utils/url'; import {engineName} from './search'; import time from '@/utils/time'; -export type Backend = DuckDuckGo | Brave; -export type BackendId = 'brave' | 'ddg'; -let backend: DuckDuckGo | Brave; - -export const backendId = new ConfigValue( - 'backend', - 'ddg', - value => value === 'brave' || value === 'ddg', - loadBackend, -); +export type Backend = DuckDuckGo; +let backend: DuckDuckGo; export function getBackendInstance(): Backend { return backend; @@ -69,13 +60,12 @@ export const bangConfig: BangConfig = { ), }; -export async function loadBackend(id: string): Promise { +export async function loadBackend(): Promise { if (backend) { backend.unhook(); } - backend = - id === 'brave' ? new Brave(bangConfig) : new DuckDuckGo(bangConfig); + backend = new DuckDuckGo(bangConfig); await backend.fetch(1000); backend.hook(); } diff --git a/src/helpers/import-export.ts b/src/helpers/import-export.ts index 240e451..c506bc8 100644 --- a/src/helpers/import-export.ts +++ b/src/helpers/import-export.ts @@ -1,4 +1,4 @@ -import {backendId, bangConfig} from './bang'; +import {bangConfig} from './bang'; import {type BangConfig, type CustomBang} from '@/models/backend'; export type ConfigData = Record< @@ -9,32 +9,26 @@ export type ConfigData = Record< }; export async function getConfig(): Promise { - const result: ConfigData = { - ...Object.fromEntries( - await Promise.all( - Object.entries(bangConfig).map(async ([key, config]) => [ - key, - await config.getValue(), - ]), - ), + const result: ConfigData = Object.fromEntries( + await Promise.all( + Object.entries(bangConfig).map(async ([key, config]) => [ + key, + await config.getValue(), + ]), ), - backendId: await backendId.getValue(), - }; + ); return result; } export async function getDefaultConfig(): Promise { - const result: ConfigData = { - ...Object.fromEntries( - await Promise.all( - Object.entries(bangConfig).map(async ([key, config]) => [ - key, - await config.getDefaultValue(), - ]), - ), + const result: ConfigData = Object.fromEntries( + await Promise.all( + Object.entries(bangConfig).map(async ([key, config]) => [ + key, + await config.getDefaultValue(), + ]), ), - backendId: await backendId.getDefaultValue(), - }; + ); return result; } @@ -65,19 +59,6 @@ export async function validateConfig( ); } - if (key === 'backendId') { - if (await backendId.validate(value as string)) { - return; - } - - return ( - 'Invalid value for key ' + - JSON.stringify(key) + - ': ' + - JSON.stringify(value) - ); - } - return 'Invalid key ' + JSON.stringify(key); }), ) @@ -98,10 +79,7 @@ export async function updateConfig( return ( await Promise.all( Object.entries(config).map(async ([key, value]) => - (await (key === 'backendId' - ? backendId - : bangConfig[key as keyof BangConfig] - ).updateValue( + (await bangConfig[key as keyof BangConfig].updateValue( // @ts-expect-error Seems like TypeScript won't allow this easily. value, validate, diff --git a/src/pages/background.ts b/src/pages/background.ts index f5d628c..c59d111 100644 --- a/src/pages/background.ts +++ b/src/pages/background.ts @@ -1,5 +1,5 @@ -import {loadBackend, backendId} from '../helpers/bang'; +import {loadBackend} from '../helpers/bang'; (async (): Promise => { - loadBackend(await backendId.getValue()); + loadBackend(); })(); diff --git a/src/pages/options/settings.tsx b/src/pages/options/settings.tsx index df2e2d2..3d853a9 100644 --- a/src/pages/options/settings.tsx +++ b/src/pages/options/settings.tsx @@ -5,17 +5,12 @@ import Button from '@/components/button'; import Page from '@/components/page'; import Select from '@/components/select'; import Input from '@/components/input'; -import {backendId, bangConfig, bangConfig as config} from '@/helpers/bang'; +import {bangConfig, bangConfig as config} from '@/helpers/bang'; import {listEngines} from '@/helpers/search'; import {usePromise} from '@/hooks/use-promise'; import {useConfig} from '@/hooks/use-config'; import time from '@/utils/time'; -const databases = [ - {value: 'ddg', label: 'DuckDuckGo'}, - {value: 'brave', label: 'Brave'}, -]; - export default function SettingsPage(): JSX.Element { const [callbacks, setCallbacks] = useState void>>( new Map(), @@ -27,7 +22,6 @@ export default function SettingsPage(): JSX.Element { bangConfig.engineName, setCallbacks, ); - const [backend, setBackendState] = useConfig(backendId, setCallbacks); const [luckyBangUrl, setLuckyBangUrlState] = useConfig( config.luckyBangUrl, setCallbacks, @@ -83,7 +77,6 @@ export default function SettingsPage(): JSX.Element {
{enginesList && currentEngine && - backend && luckyBangUrl && siteFormat && bangPrefix && @@ -105,20 +98,6 @@ export default function SettingsPage(): JSX.Element { callback={setCurrentEngineState} defaultItem={currentEngine} /> - { testCommon(runTester); relativeBangs(runTester); }); - -describe('Brave bangs', () => { - const backend = new Brave(bangConfig); - const runTester = createTester(backend); - fetchBackend(backend); - registerCleanup(); - testCommon(runTester); -});