web search done right
toggle visibility of optics
Mikkel Denker23a6fc7cd217
diff --git a/frontend/src/lib/components/OpticSelector.svelte b/frontend/src/lib/components/OpticSelector.svelteindex b882376f..cfc9aa69 100644--- a/frontend/src/lib/components/OpticSelector.svelte+++ b/frontend/src/lib/components/OpticSelector.svelte@@ -1,17 +1,26 @@<script lang="ts">- import { DEFAULT_OPTICS } from '$lib/optics';- import { opticsStore } from '$lib/stores';+ import { DEFAULT_OPTICS, opticKey } from '$lib/optics';+ import { opticsStore, opticsShowStore } from '$lib/stores';import Select from './Select.svelte';import { derived } from 'svelte/store';export let searchOnChange: boolean;export let selected = '';+ for (const optic of DEFAULT_OPTICS) {+ if ($opticsShowStore[opticKey(optic)] !== undefined) continue;++ opticsShowStore.update(($opticsShow) => ({+ ...$opticsShow,+ [opticKey(optic)]: optic.shown,+ }));+ }+const optics = derived(opticsStore, ($optics) => [...$optics, ...DEFAULT_OPTICS]);$: options = [{ value: '', label: 'No Optic' },- ...$optics.map((optic) => ({ value: optic.url, label: optic.name })),+ ...$optics.filter((optic) => $opticsShowStore[opticKey(optic)]).map((optic) => ({ value: optic.url, label: optic.name })),];</script>diff --git a/frontend/src/lib/optics.ts b/frontend/src/lib/optics.tsindex 15f7292e..34118218 100644--- a/frontend/src/lib/optics.ts+++ b/frontend/src/lib/optics.ts@@ -4,46 +4,60 @@ export type OpticOption = {description: string;};+export type DefaultOpticOption = OpticOption & {+ shown: boolean;+};++export const opticKey = (optic: OpticOption) => optic.name + "_" + optic.url;++export const DEFAULT_OPTICS = [{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/copycats_removal.optic',description: 'Remove common copycat websites from search results.',name: 'Copycats removal',+ shown: true,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/hacker_news.optic',description: 'Only return results from websites that are popular on Hacker News.',name: 'Hacker News',+ shown: false,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/discussions.optic',description: 'Only return results from forums or similar types of QA pages.',name: 'Discussions',+ shown: true,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/10k_short.optic',description: 'Remove the top 10,000 most popular websites from search results.',name: '10K Short',+ shown: false,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/indieweb_blogroll.optic',description:'Search only in the indieweb + a list of blogs from blogroll.org and some hand-picked blogs from hackernews.',name: 'Indieweb & blogroll',+ shown: true,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/devdocs.optic',description:'Only return results from some of the developer documentation sites listed on devdocs.io. This is a non-exhaustive list.',name: 'Devdocs',+ shown: false,},{url: 'https://raw.githubusercontent.com/StractOrg/sample-optics/main/academic.optic',description:'Search exclusively in academic sites (.edu, .ac.uk, arxiv.org etc.). This is a non-exhaustive list.',name: 'Academic',+ shown: true,},-] satisfies OpticOption[];+] satisfies DefaultOpticOption[];/*** Fetces the given `opticUrl` if allowed. The rules for which are alloweddiff --git a/frontend/src/lib/stores.ts b/frontend/src/lib/stores.tsindex ec797412..16ec5aef 100644--- a/frontend/src/lib/stores.ts+++ b/frontend/src/lib/stores.ts@@ -45,6 +45,9 @@ export const postSearchStore = writableLocalStorage<boolean>(POST_SEARCH_KEY, faconst OPTICS_KEY = 'optics';export const opticsStore = writableLocalStorage<OpticOption[]>(OPTICS_KEY, []);+const OPTICS_SHOW_KEY = 'opticsShow';+export const opticsShowStore = writableLocalStorage<Record<string, boolean>>(OPTICS_SHOW_KEY, {});+const ALLOW_STATS_KEY = 'allowStats';export const allowStatsStore = writableLocalStorage<boolean>(ALLOW_STATS_KEY, true);diff --git a/frontend/src/routes/settings/optics/+page.svelte b/frontend/src/routes/settings/optics/+page.svelteindex 82fcfef8..aba1ecd1 100644--- a/frontend/src/routes/settings/optics/+page.svelte+++ b/frontend/src/routes/settings/optics/+page.svelte@@ -1,8 +1,10 @@<script lang="ts">import MinusCircle from '~icons/heroicons/minus-circle';+ import EyeSlash from "~icons/heroicons/eye-slash";+ import Eye from "~icons/heroicons/eye";import Button from '$lib/components/Button.svelte';- import { opticsStore } from '$lib/stores';- import { DEFAULT_OPTICS, fetchRemoteOptic, type OpticOption } from '$lib/optics';+ import { opticsShowStore, opticsStore } from '$lib/stores';+ import { DEFAULT_OPTICS, fetchRemoteOptic, opticKey, type OpticOption } from '$lib/optics';import { derived } from 'svelte/store';import Callout from '$lib/components/Callout.svelte';@@ -20,7 +22,12 @@fetch,});- opticsStore.update(($optics) => [...$optics, { name, url, description }]);+ opticsShowStore.update(($opticsShow) => ({+ ...$opticsShow,+ [opticKey({ name, url, description })]: true,+ }));+ opticsStore.update(($optics) => [...$optics, { name, url, description, shown: true }]);+name = '';url = '';description = '';@@ -31,10 +38,24 @@console.error(e);}};+const removeOptic = (optic: OpticOption) => () => {opticsStore.update(($optics) => $optics.filter((o) => o != optic));+ opticsShowStore.update(($opticsShow) => {+ const { [opticKey(optic)]: _, ...rest } = $opticsShow;+ return rest;+ });};+ for (const optic of DEFAULT_OPTICS) {+ if ($opticsShowStore[opticKey(optic)] !== undefined) continue;++ opticsShowStore.update(($opticsShow) => ({+ ...$opticsShow,+ [opticKey(optic)]: optic.shown,+ }));+ }+const optics = derived(opticsStore, ($optics) => [...$optics.map((optic) => ({optic,@@ -105,7 +126,8 @@</Callout>{/if}<div class="mt-5">- <div class="grid w-full grid-cols-[auto_1fr_2fr_auto] gap-5" id="optics-list">+ <div class="grid w-full grid-cols-[auto_auto_1fr_2fr_auto] gap-5" id="optics-list">+ <span /><span /><div class="flex-1 font-medium">Name</div><div class="flex-1 font-medium">Description</div>@@ -117,9 +139,18 @@disabled={!removable}><MinusCircle- class="transition group-enabled:text-error group-enabled:group-hover:text-error"+ class="transition text-neutral group-enabled:text-error group-enabled:group-hover:text-error"/></button>+ <label class="hover:cursor-pointer">+ <input type="checkbox" bind:checked={$opticsShowStore[opticKey(optic)]} class="hidden peer" />+ <EyeSlash+ class="transition text-neutral inline-flex peer-checked:hidden"+ />+ <Eye+ class="transition text-neutral peer-checked:inline-flex hidden"+ />+ </label><div class="text-sm">{optic.name}</div><div class="text-sm">{optic.description}diff --git a/frontend/src/routes/settings/sites/+page.svelte b/frontend/src/routes/settings/sites/+page.svelteindex 3e0628bb..8470c4bc 100644--- a/frontend/src/routes/settings/sites/+page.svelte+++ b/frontend/src/routes/settings/sites/+page.svelte@@ -39,6 +39,7 @@const unrankSite = (site: string) => () => {siteRankingsStore.update(($rankings) => ({ ...$rankings, [site]: void 0 }));};+const clearAndExport =({ clear }: { clear: boolean }) =>async () => {