Skip to content

web search done right

optic descriptions and ability to choose optic from frontpage

Mikkel Denker5ba12367d5a7
diff --git a/core/src/api/index.rs b/core/src/api/index.rs
index a9a90dfd..a4058b6a 100644
--- a/core/src/api/index.rs
+++ b/core/src/api/index.rs
@@ -14,16 +14,23 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use super::HtmlTemplate;
+use super::{
+ optics::{OpticLink, DEFAULT_OPTICS},
+ HtmlTemplate,
+};
use askama::Template;
use axum::response::IntoResponse;
#[allow(clippy::unused_async)]
pub async fn route() -> impl IntoResponse {
- let template = IndexTemplate {};
+ let template = IndexTemplate {
+ default_optics: DEFAULT_OPTICS.to_vec(),
+ };
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "index.html")]
-struct IndexTemplate {}
+struct IndexTemplate {
+ default_optics: Vec<OpticLink>,
+}
diff --git a/core/src/api/optics.rs b/core/src/api/optics.rs
index ae2e1173..0c3919a1 100644
--- a/core/src/api/optics.rs
+++ b/core/src/api/optics.rs
@@ -23,14 +23,17 @@ pub const DEFAULT_OPTICS: [OpticLink; 3] = [
name: "Copycats removal",
url:
"https://raw.githubusercontent.com/StractOrg/sample-optics/main/copycats_removal.optic",
+ description: "Remove common copycat websites from search results.",
},
OpticLink {
name: "Hacker News",
url: "https://raw.githubusercontent.com/StractOrg/sample-optics/main/hacker_news.optic",
+ description: "Only return results from websites that are popular on Hacker News.",
},
OpticLink {
name: "Discussions",
url: "https://raw.githubusercontent.com/StractOrg/sample-optics/main/discussions.optic",
+ description: "Only return results from forums or similar types of QA pages.",
},
];
@@ -38,6 +41,7 @@ pub const DEFAULT_OPTICS: [OpticLink; 3] = [
pub struct OpticLink {
pub name: &'static str,
pub url: &'static str,
+ pub description: &'static str,
}
#[allow(clippy::unused_async)]
diff --git a/frontend/src/components/OpticSelector.astro b/frontend/src/components/OpticSelector.astro
index 5b698f85..dad8e174 100644
--- a/frontend/src/components/OpticSelector.astro
+++ b/frontend/src/components/OpticSelector.astro
@@ -1,9 +1,13 @@
---
export interface Props {
- current_optic_url: string;
default_optics: string;
+ search_on_change: boolean;
}
+const { search_on_change } = Astro.props;
+
+const onChange = search_on_change ? "document.getElementById('searchbar-form').submit()" : "";
+
import { askama } from "$/askama";
---
@@ -12,26 +16,29 @@ import { askama } from "$/askama";
form="searchbar-form"
id="optics-selector"
name="optic"
- onchange="document.getElementById('searchbar-form').submit()"
+ onchange={onChange}
class="styled-selector m-0"
>
- <option value="">Optic</option>
+ <option value="">No Optic</option>
{
askama.for_(
"optic in default_optics",
- <option value="{{ optic.url }}">{askama`optic.name`}</option>
+ <option value="{{ optic.url }}" title="{{ optic.description }}">{askama`optic.name`}</option>
)
}
</select>
</div>
<script>
- const optics: {url: string, name: string}[] = JSON.parse(window.localStorage.getItem("optics") || "[]");
+ const optics: {url: string, name: string, description: string}[] = JSON.parse(window.localStorage.getItem("optics") || "[]");
for (const optic of optics) {
const option = document.createElement("option");
+
option.value = optic.url;
option.text = optic.name;
+ option.setAttribute("title", optic.description);
+
document.getElementById("optics-selector")!.appendChild(option);
}
diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro
index 033aa863..21881510 100644
--- a/frontend/src/pages/index.astro
+++ b/frontend/src/pages/index.astro
@@ -3,6 +3,7 @@ import Footer from "../components/Footer.astro";
import Header from "../components/Header.astro";
import Searchbar from "../components/Searchbar.astro";
import Layout from "../layouts/Layout.astro";
+import OpticSelector from "../components/OpticSelector.astro";
---
<Layout title="Stract">
@@ -16,6 +17,9 @@ import Layout from "../layouts/Layout.astro";
<img src="/images/biglogo.svg" class="h-full w-full" />
</div>
<Searchbar autofocus />
+ <div class="mt-2 text-gray-600 flex space-x-2">
+ <div>Customise your search with an <a class="underline font-medium" href="/settings">optic</a>:</div><OpticSelector default_optics="default_optics" search_on_change={false}/>
+ </div>
</div>
</div>
diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astro
index 1706959b..1104f2c4 100644
--- a/frontend/src/pages/search.astro
+++ b/frontend/src/pages/search.astro
@@ -32,8 +32,8 @@ import { askama } from "../askama";
</div>
<div class="flex space-x-2">
<OpticSelector
- current_optic_url="current_optic_url"
default_optics="default_optics"
+ search_on_change={true}
/>
<div class="select-region flex h-full flex-col justify-center">
<select
diff --git a/frontend/src/pages/settings/index.astro b/frontend/src/pages/settings/index.astro
index fa5d351e..a6dd7ffc 100644
--- a/frontend/src/pages/settings/index.astro
+++ b/frontend/src/pages/settings/index.astro
@@ -27,23 +27,26 @@ import { askama } from "../../askama";
</div>
<div
class="mt-16"
- x-data="{ items: $persist([]).as('optics'), name: '', url: '' }"
+ x-data="{ items: $persist([]).as('optics'), name: '', description: '', url: '' }"
>
- <div class="flex w-full justify-between pl-5 pr-5">
+ <div class="flex w-full justify-between px-5 h-10">
<input id="name" type="text" placeholder="Name" x-model="name" />
<input id="url" type="text" placeholder="Url" x-model="url" />
<button
- x-on:click="if (name != '' && url != '') {items.push({ name, url }); name = url = ''}"
- class="h-10 w-20 rounded-full border-0 bg-brand text-sm text-white"
+ x-on:click="if (name != '' && url != '') {items.push({ name, description, url }); name = url = description = ''}"
+ class="w-20 rounded-full border-0 bg-brand text-sm text-white"
>Add</button
>
</div>
+ <div class="flex w-full px-5 h-10 mt-2">
+ <input id="description" type="text" placeholder="Description" x-model="description" />
+ </div>
<div class="mt-5">
<div class="grid w-full space-y-5" id="optics-list">
- <div class="flex">
- <div class="w-10"></div>
- <div class="flex-1 font-medium">Name</div>
- <div class="flex-1 font-medium">Url</div>
+ <div class="flex pl-10">
+ <div class="flex-1 font-medium mr-5">Name</div>
+ <div class="flex-1 font-medium mr-5">Description</div>
+ <div class="flex-1 font-medium">Link</div>
</div>
{
askama.for_("optic in default_optics", () => (
@@ -51,8 +54,9 @@ import { askama } from "../../askama";
<div class="mr-5 w-5">
<img src="/images/disabled-delete.svg" class="h-5 w-5" />
</div>
- <div class="flex-1 text-sm">{askama`optic.name $ {{random.words}}`}</div>
- <div class="flex-1 text-sm">{askama`optic.url $ {{internet.url}}`}</div>
+ <div class="flex-1 text-sm mr-5">{askama`optic.name $ {{random.words}}`}</div>
+ <div class="flex-1 text-sm mr-5">{askama`optic.description $ {{random.words}}`}</div>
+ <div class="flex-1 text-sm"><a href={askama`optic.url $ {{internet.url}}`}>Source</a></div>
</div>
))
}
@@ -65,9 +69,12 @@ import { askama } from "../../askama";
x-on:click="items.splice(index, 1)"
/>
</div>
- <div class="user-optic flex-1 text-sm" x-text="item.name">
+ <div class="user-optic flex-1 text-sm mr-5" x-text="item.name">
+ </div>
+ <div class="user-optic flex-1 text-sm mr-5" x-text="item.description">
</div>
- <div class="user-optic flex-1 text-sm" x-text="item.url">
+ <div class="user-optic flex-1 text-sm">
+ <a x-bind:href="item.url">Source</a>
</div>
</div>
</template>
@@ -77,6 +84,12 @@ import { askama } from "../../askama";
</div>
</SettingsLayout>
+<style>
+ a {
+ @apply underline font-medium;
+ }
+</style>
+
<script>
import "$/alpine";
</script>
\ No newline at end of file
diff --git a/frontend/src/pages/settings/privacy.astro b/frontend/src/pages/settings/privacy.astro
index d820914e..76030984 100644
--- a/frontend/src/pages/settings/privacy.astro
+++ b/frontend/src/pages/settings/privacy.astro
@@ -28,7 +28,7 @@ import SettingsLayout from "../../layouts/Settings.astro";
<style>
a {
- @apply text-blue-600;
+ @apply underline font-medium;
}
</style>