web search done right
Frontend chores (#104)
Oliver Bøving2d990093eb0c
diff --git a/frontend/src/app.css b/frontend/src/app.cssindex bd4293ee..ed374080 100644--- a/frontend/src/app.css+++ b/frontend/src/app.css@@ -3,14 +3,12 @@@tailwind utilities;@layer base {-:root,html {@apply theme-light bg-base-100 text-base-content transition-colors duration-300;}@media (prefers-color-scheme: dark) {-:root,html {@apply theme-dark;@@ -22,4 +20,4 @@::after {@apply border-base-400;}-}\ No newline at end of file+}diff --git a/frontend/src/app.d.ts b/frontend/src/app.d.tsindex 5195f903..bc139e0f 100644--- a/frontend/src/app.d.ts+++ b/frontend/src/app.d.ts@@ -5,7 +5,9 @@ import 'unplugin-icons/types/svelte';declare global {namespace App {// interface Error {}- // interface Locals {}+ interface Locals {+ form?: FormData;+ }interface PageData {globals?: {title?: string;diff --git a/frontend/src/lib/api/index.ts b/frontend/src/lib/api/index.tsindex a22f7ca8..4373f702 100644--- a/frontend/src/lib/api/index.ts+++ b/frontend/src/lib/api/index.ts@@ -16,10 +16,7 @@ export const api = {return { data: data.then((res) => res[200]), cancel };},- similarSites: (- props: Props<'/beta/api/webgraph/host/similar', 'post'>,- options?: ApiOptions,- ) => {+ similarSites: (props: Props<'/beta/api/webgraph/host/similar', 'post'>, options?: ApiOptions) => {const { data, cancel } = send('/beta/api/webgraph/host/similar','post',diff --git a/frontend/src/lib/components/OpticSelector.svelte b/frontend/src/lib/components/OpticSelector.svelteindex cfc9aa69..0215ddbc 100644--- a/frontend/src/lib/components/OpticSelector.svelte+++ b/frontend/src/lib/components/OpticSelector.svelte@@ -20,7 +20,9 @@$: options = [{ value: '', label: 'No Optic' },- ...$optics.filter((optic) => $opticsShowStore[opticKey(optic)]).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/components/PostSearchSelect.svelte b/frontend/src/lib/components/PostSearchSelect.svelteindex 816da6f0..674dcdc1 100644--- a/frontend/src/lib/components/PostSearchSelect.svelte+++ b/frontend/src/lib/components/PostSearchSelect.svelte@@ -9,5 +9,5 @@</script><div class="noscript:hidden">- <Select bind:value={$postSearchStore} class="text-neutral-focus" {options} />-</div>\ No newline at end of file+ <Select bind:value={$postSearchStore} class="text-neutral-focus" {options} />+</div>diff --git a/frontend/src/lib/components/Searchbar.svelte b/frontend/src/lib/components/Searchbar.svelteindex 489aefe5..1d048ea2 100644--- a/frontend/src/lib/components/Searchbar.svelte+++ b/frontend/src/lib/components/Searchbar.svelte@@ -81,7 +81,12 @@let hasFocus = autofocus;</script>-<form action="/search" class="flex w-full justify-center" id="searchbar-form" method={$postSearchStore ? "POST" : "GET"}>+<form+ action="/search"+ class="flex w-full justify-center"+ id="searchbar-form"+ method={$postSearchStore ? 'POST' : 'GET'}+><input type="hidden" value={$safeSearchStore ? 'true' : 'false'} name="ss" /><input type="hidden" value={$compressedRanked} name="sr" id="siteRankingsUuid" />diff --git a/frontend/src/lib/optics.ts b/frontend/src/lib/optics.tsindex 34118218..45e1b319 100644--- a/frontend/src/lib/optics.ts+++ b/frontend/src/lib/optics.ts@@ -8,8 +8,7 @@ export type DefaultOpticOption = OpticOption & {shown: boolean;};-export const opticKey = (optic: OpticOption) => optic.name + "_" + optic.url;-+export const opticKey = (optic: OpticOption) => optic.name + '_' + optic.url;export const DEFAULT_OPTICS = [{diff --git a/frontend/src/lib/search.ts b/frontend/src/lib/search.tsindex e01cf9f6..0bd42b73 100644--- a/frontend/src/lib/search.ts+++ b/frontend/src/lib/search.ts@@ -1,33 +1,34 @@-import type { Region } from "./api";-import { decompressRanked, type RankedSites } from "./rankings";+import type { Region } from './api';+import { decompressRanked, type RankedSites } from './rankings';export type SearchParams = {- query: string;- currentPage: number;- optic: string | undefined;- selectedRegion: Region | undefined;- safeSearch: boolean;- compressedSiteRankings: string | null;- siteRankings: RankedSites | undefined;- };-- export const extractSearchParams = (searchParams: URLSearchParams | FormData): SearchParams => {- const query = searchParams.get('q') as string | undefined ?? '';- const currentPage = parseInt(searchParams.get('p') as string | undefined ?? '1') || 1;- const optic = searchParams.get('optic') as string | undefined || void 0;- const selectedRegion = (searchParams.get('gl') as string | undefined || void 0) as Region | undefined;- const safeSearch = searchParams.get('ss') as string | undefined == 'true';- const compressedSiteRankings = searchParams.get('sr') as string | undefined || null;- const siteRankings = compressedSiteRankings ? decompressRanked(compressedSiteRankings) : void 0;-- return {- query,- currentPage,- optic,- selectedRegion,- safeSearch,- compressedSiteRankings,- siteRankings,- };+ query: string;+ currentPage: number;+ optic: string | undefined;+ selectedRegion: Region | undefined;+ safeSearch: boolean;+ compressedSiteRankings: string | null;+ siteRankings: RankedSites | undefined;+};++export const extractSearchParams = (searchParams: URLSearchParams | FormData): SearchParams => {+ const query = (searchParams.get('q') as string | undefined) ?? '';+ const currentPage = parseInt((searchParams.get('p') as string | undefined) ?? '1') || 1;+ const optic = (searchParams.get('optic') as string | undefined) || void 0;+ const selectedRegion = ((searchParams.get('gl') as string | undefined) || void 0) as+ | Region+ | undefined;+ const safeSearch = (searchParams.get('ss') as string | undefined) == 'true';+ const compressedSiteRankings = (searchParams.get('sr') as string | undefined) || null;+ const siteRankings = compressedSiteRankings ? decompressRanked(compressedSiteRankings) : void 0;++ return {+ query,+ currentPage,+ optic,+ selectedRegion,+ safeSearch,+ compressedSiteRankings,+ siteRankings,};-\ No newline at end of file+};diff --git a/frontend/src/routes/search/+page.server.ts b/frontend/src/routes/search/+page.server.tsindex 6d5cadab..c7a6d321 100644--- a/frontend/src/routes/search/+page.server.ts+++ b/frontend/src/routes/search/+page.server.ts@@ -1,19 +1,18 @@import { extractSearchParams, type SearchParams } from '$lib/search';import type { Actions } from './$types';-export const load = async ({locals}) => {- // @ts-ignore- const searchParams: SearchParams | undefined = (locals["form"] && extractSearchParams(locals['form'])) || undefined;- return {form: searchParams};-}-+export const load = async ({ locals }) => {+ const searchParams: SearchParams | undefined =+ (locals['form'] && extractSearchParams(locals['form'])) || undefined;+ return { form: searchParams };+};+export const actions: Actions = {- default: async (event) => {- const { request } = event;+ default: async (event) => {+ const { request } = event;- // @ts-ignore- event.locals.form = await request.formData();+ event.locals.form = await request.formData();- return { success: true };- }-} satisfies Actions;\ No newline at end of file+ return { success: true };+ },+} satisfies Actions;diff --git a/frontend/src/routes/search/+page.ts b/frontend/src/routes/search/+page.tsindex 968447a2..6897e9f4 100644--- a/frontend/src/routes/search/+page.ts+++ b/frontend/src/routes/search/+page.ts@@ -66,4 +66,4 @@ export const load: PageLoad = async (req) => {header: { divider: true },}),};-};\ No newline at end of file+};diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelteindex 540cc1c4..f1b487a3 100644--- a/frontend/src/routes/settings/+page.svelte+++ b/frontend/src/routes/settings/+page.svelte@@ -23,7 +23,7 @@title: 'POST Search Requests',description: 'Send search queries using POST instead of GET',type: 'post',- }+ },] as const;</script>@@ -32,7 +32,7 @@<h1 class="text-2xl font-medium">Preferences</h1><div class="flex flex-col space-y-5">{#each settings as setting}- <div class="flex flex-col sm:flex-row sm:justify-between pr-1">+ <div class="flex flex-col pr-1 sm:flex-row sm:justify-between"><div class="flex flex-col"><h2 class="text-lg">{setting.title}</h2><p>{setting.description}</p>@@ -42,7 +42,10 @@{#if setting.type == 'safe-search'}<div class="flex space-x-4 pr-2">{#each safeSearchOptions as [name, state]}- <label class="flex cursor-pointer flex-col items-center" for="safe-search-{state}">+ <label+ class="flex cursor-pointer flex-col items-center"+ for="safe-search-{state}"+ ><inputtype="radio"name="safe-search"@@ -56,7 +59,7 @@</div>{:else if setting.type == 'theme'}<ThemeSelect />- {:else if setting.type == "post"}+ {:else if setting.type == 'post'}<PostSearchSelect />{/if}</div>diff --git a/frontend/src/routes/settings/optics/+page.svelte b/frontend/src/routes/settings/optics/+page.svelteindex aba1ecd1..ca12e05f 100644--- a/frontend/src/routes/settings/optics/+page.svelte+++ b/frontend/src/routes/settings/optics/+page.svelte@@ -1,7 +1,7 @@<script lang="ts">import MinusCircle from '~icons/heroicons/minus-circle';- import EyeSlash from "~icons/heroicons/eye-slash";- import Eye from "~icons/heroicons/eye";+ import EyeSlash from '~icons/heroicons/eye-slash';+ import Eye from '~icons/heroicons/eye';import Button from '$lib/components/Button.svelte';import { opticsShowStore, opticsStore } from '$lib/stores';import { DEFAULT_OPTICS, fetchRemoteOptic, opticKey, type OpticOption } from '$lib/optics';@@ -38,7 +38,7 @@console.error(e);}};-+const removeOptic = (optic: OpticOption) => () => {opticsStore.update(($optics) => $optics.filter((o) => o != optic));opticsShowStore.update(($opticsShow) => {@@ -139,17 +139,17 @@disabled={!removable}><MinusCircle- class="transition text-neutral group-enabled:text-error group-enabled:group-hover:text-error"+ class="text-neutral transition 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 class="flex w-6 items-start hover:cursor-pointer">+ <input+ type="checkbox"+ bind:checked={$opticsShowStore[opticKey(optic)]}+ class="peer hidden"/>+ <EyeSlash class="inline-flex text-neutral transition peer-checked:hidden" />+ <Eye class="hidden text-neutral transition peer-checked:inline-flex" /></label><div class="text-sm">{optic.name}</div><div class="text-sm">