Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite scroll for search results #494

Merged
merged 8 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const IMAGE_WIDTHS = IMAGE_WIDTHS_ENTRIES.map(([name, width]) => [

export const IMAGE_WIDTHS_MAP = new Map(IMAGE_WIDTHS_ENTRIES);

export const DEFAULT_PER_PAGE = 25;
export const MAX_PER_PAGE = 100;

export const PDF_SIZE_LIMIT = 525336576;
Expand Down
22 changes: 17 additions & 5 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** API helpers related to documents.
* Lots of duplicated code here that should get consolidated at some point.
*/
import type { Document, sizes } from "./types";
import type { Document, DocumentResults, SearchOptions, sizes } from "./types";

import { error } from "@sveltejs/kit";

Expand All @@ -10,17 +10,29 @@ import { isOrg } from "@/api/types/orgAndUser";
import { APP_URL, BASE_API_URL } from "@/config/config.js";
import { isErrorCode } from "../utils";

/** Search documents */
/**
* Search documents
* https://www.documentcloud.org/help/search/
*
* */
export async function search(
query = "",
highlight = false,
options: SearchOptions = {
hl: Boolean(query),
per_page: 25,
cursor: "",
version: "2.0",
},
fetch = globalThis.fetch,
) {
): Promise<DocumentResults> {
const endpoint = new URL("documents/search/", BASE_API_URL);

endpoint.searchParams.set("expand", DEFAULT_EXPAND);
endpoint.searchParams.set("q", query);
endpoint.searchParams.set("hl", String(highlight));

for (const [k, v] of Object.entries(options)) {
endpoint.searchParams.set(k, String(v));
}

const resp = await fetch(endpoint, { credentials: "include" });

Expand Down
9 changes: 8 additions & 1 deletion src/lib/api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export interface Document {
note_highlights?: Record<string, Highlight[]>;
}

// export type DocumentResults = Page<Document>;
export interface DocumentResults extends Page<Document> {}
eyeseast marked this conversation as resolved.
Show resolved Hide resolved

export interface Note {
Expand Down Expand Up @@ -135,6 +134,14 @@ export interface Section {

export type SectionResults = Page<Section>;

export interface SearchOptions {
hl?: boolean;
per_page?: number;
cursor?: string;
expand?: string;
version?: number | string;
}

export interface OEmbed {
version: "1.0";
provider_name: "DocumentCloud";
Expand Down
12 changes: 7 additions & 5 deletions src/lib/components/Search.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { Search24, XCircleFill24 } from "svelte-octicons";
import Button from "./common/Button.svelte";

export let query: string = "";
let input: HTMLInputElement;
Expand All @@ -11,16 +10,19 @@
}
</script>

<form class="container" on:submit|preventDefault>
<form class="container" on:submit>
<label for="query" title="Search"><Search24 /></label>
<input
type="search"
id="query"
name="query"
name="q"
autocomplete="off"
placeholder="Search…"
bind:value={query}
bind:this={input}
on:change
autocomplete="off"
placeholder="Search…"
on:input
on:reset
/>
<button
title="Clear Search"
Expand Down
Loading
Loading