Skip to content

Commit

Permalink
Loads pinned add-on data in sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
allanlasser committed Mar 1, 2024
1 parent a9e4f06 commit 7b4810a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
5 changes: 0 additions & 5 deletions src/addons/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
interface Author {
name?: string;
avatar?: string;
}

type AddOnCategory = "premium" | string;

interface AddOnProperty {
Expand Down
16 changes: 16 additions & 0 deletions src/lib/api/addons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { error } from "@sveltejs/kit";
import { BASE_API_URL } from "@/config/config.js";
import { type AddOnListItem } from "@/addons/types";
import { isErrorCode } from "../utils";

export async function getPinnedAddons(): Promise<AddOnListItem[]> {
const endpoint = new URL(
"/api/addons/?active=true&per_page=100",
BASE_API_URL,
);
const resp = await fetch(endpoint, { credentials: "include" });
if (isErrorCode(resp.status)) {
error(resp.status, resp.statusText);
}
return resp.json();
}
49 changes: 49 additions & 0 deletions src/lib/api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,55 @@ export type sizes = "thumbnail" | "small" | "normal" | "large" | "xlarge";

export type Highlight = Record<string, string[]>;

type AddOnCategory = "premium" | string;

interface AddOnProperty {
type: string;
title: string;
description?: string;
default?: string;
format?: string;
enum?: string[];
}

interface AddOnParameters {
type: string;
version: number;
title: string;
description: string;
instructions: string;
categories: AddOnCategory[];
documents: string[];
required: string[];
properties: Record<string, AddOnProperty>;
cost: {
amount: number;
price: number;
unit: string;
};
eventOptions: {
name: string;
events: string[];
};
}

// API endpoint https://api.www.documentcloud.org/api/addons/
export interface AddOnListItem {
id: number;
user: number;
organization: number;
access: "public" | "private";
name: string;
repository: string;
parameters: Partial<AddOnParameters>;
created_at: string;
updated_at: string;
active: boolean;
featured: boolean;
default: boolean;
usage?: number;
}

// https://www.documentcloud.org/help/api#documents
export interface Document {
id: number | string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { isErrorCode } from "./isErrorCode";
5 changes: 5 additions & 0 deletions src/lib/utils/isErrorCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { NumericRange } from "@sveltejs/kit";

export function isErrorCode(status: number): status is NumericRange<400, 599> {
return status >= 400 && status <= 599;
}
3 changes: 3 additions & 0 deletions src/routes/app/+layout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { search } from "$lib/api/documents.js";
import { getPinnedAddons } from "@/lib/api/addons.js";

export async function load({ url, fetch }) {
const query = url.searchParams.get("q") || "";

const searchResults = search(query, true, fetch);
const pinnedAddons = getPinnedAddons();

return {
searchResults,
query,
pinnedAddons,
};
}
34 changes: 21 additions & 13 deletions src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Share16,
Book16,
Hourglass24,
Pin24,
} from "svelte-octicons";
import MainLayout from "$lib/components/MainLayout.svelte";
import Button from "$lib/components/common/Button.svelte";
Expand All @@ -26,15 +27,22 @@
import Search from "$lib/components/Search.svelte";
import Empty from "$lib/components/common/Empty.svelte";
import Paginator from "@/common/Paginator.svelte";
import type { DocumentResults } from "@/lib/api/types";
import type { AddOnListItem } from "@/addons/types";
export let data;
export let data: {
query: string;
searchResults: Promise<DocumentResults>;
pinnedAddons: Promise<AddOnListItem[]>;
};
let page = 1;
let per_page = 25;
let error: Error;
$: searchResults = data.searchResults;
$: query = data.query;
$: pinnedAddons = data.pinnedAddons;
$: count = searchResults.count;
$: next = searchResults.next;
$: previous = searchResults.previous;
Expand Down Expand Up @@ -151,18 +159,18 @@
<SidebarItem slot="title"><Plug16 /> Add-Ons</SidebarItem>
<Action slot="action" icon={Book16}>Explore</Action>
<Flex direction="column" gap={0}>
<SidebarItem small href="/addon/1">
<Pin active /> Scraper
</SidebarItem>
<SidebarItem small href="/addon/2">
<Pin active /> Regex Extractor
</SidebarItem>
<SidebarItem small href="/addon/3">
<Pin active /> Tabula Spreadsheet Analysis
</SidebarItem>
<SidebarItem small href="/addon/4">
<Pin active /> GPT 3.5 Analysis
</SidebarItem>
{#await pinnedAddons}
<Empty icon={Hourglass24}>Loading…</Empty>
{:then addons}
{#each addons as addon}
<SidebarItem small href={`/addon/${addon.id}`}>
<Pin active={addon.active} />
{addon.name}
</SidebarItem>
{:else}
<Empty icon={Pin24}>Pinned add-ons will appear here</Empty>
{/each}
{/await}
</Flex>
</SidebarGroup>
</svelte:fragment>
Expand Down

0 comments on commit 7b4810a

Please sign in to comment.