Skip to content

Commit

Permalink
feat: remove Brave bangs
Browse files Browse the repository at this point in the history
  • Loading branch information
WhistlingZephyr committed Oct 20, 2023
1 parent e2647a4 commit 935aaec
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 113 deletions.
28 changes: 0 additions & 28 deletions src/backends/brave.ts

This file was deleted.

18 changes: 4 additions & 14 deletions src/helpers/bang.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -69,13 +60,12 @@ export const bangConfig: BangConfig = {
),
};

export async function loadBackend(id: string): Promise<void> {
export async function loadBackend(): Promise<void> {
if (backend) {
backend.unhook();
}

backend =
id === 'brave' ? new Brave(bangConfig) : new DuckDuckGo(bangConfig);
backend = new DuckDuckGo(bangConfig);
await backend.fetch(1000);
backend.hook();
}
54 changes: 16 additions & 38 deletions src/helpers/import-export.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -9,32 +9,26 @@ export type ConfigData = Record<
};

export async function getConfig(): Promise<ConfigData> {
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<ConfigData> {
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;
}

Expand Down Expand Up @@ -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);
}),
)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {loadBackend, backendId} from '../helpers/bang';
import {loadBackend} from '../helpers/bang';

(async (): Promise<void> => {
loadBackend(await backendId.getValue());
loadBackend();
})();
23 changes: 1 addition & 22 deletions src/pages/options/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<symbol, () => void>>(
new Map(),
Expand All @@ -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,
Expand Down Expand Up @@ -83,7 +77,6 @@ export default function SettingsPage(): JSX.Element {
<div className={styles.settingsContainer}>
{enginesList &&
currentEngine &&
backend &&
luckyBangUrl &&
siteFormat &&
bangPrefix &&
Expand All @@ -105,20 +98,6 @@ export default function SettingsPage(): JSX.Element {
callback={setCurrentEngineState}
defaultItem={currentEngine}
/>
<Select
items={databases}
label="Bang database to use"
getItemLabel={(item): string => item.label}
getItemValue={(item): string =>
item?.value ?? 'Invalid'
}
callback={(item): void => {
setBackendState(item.value);
}}
defaultItem={databases.find(
item => item.value === backend,
)}
/>
<Input
label="(Super) lucky bang URL"
notes={[
Expand Down
9 changes: 0 additions & 9 deletions tests/backends.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import superLuckyBangs from './backend/super-lucky-bangs';
import superMixedSiteBangs from './backend/super-mixed-site-bangs';
import {type Backend, bangConfig} from '@/helpers/bang';
import DuckDuckGo from '@/backends/ddg';
import Brave from '@/backends/brave';

vi.mock(
'../src/models/config-bangs.ts',
Expand Down Expand Up @@ -74,11 +73,3 @@ describe('DuckDuckGo bangs', () => {
testCommon(runTester);
relativeBangs(runTester);
});

describe('Brave bangs', () => {
const backend = new Brave(bangConfig);
const runTester = createTester(backend);
fetchBackend(backend);
registerCleanup();
testCommon(runTester);
});

0 comments on commit 935aaec

Please sign in to comment.