Skip to content

Commit

Permalink
1 error 0 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stolinski committed Sep 22, 2023
1 parent 1e9e0ab commit b262c0b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/lib/search/SearchBox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
import SearchResultList from './SearchResultList.svelte';
import { fade } from 'svelte/transition';
import { clickOutDialog } from '$actions/click_outside_dialog';
import type { Tree } from './types';
import type { Block, Tree } from './types';
import { Show } from '@prisma/client';
let search_input: HTMLInputElement;
let modal: HTMLDialogElement;
let search: {
results: Tree[];
query: string;
} | null = null;
let recent_searches: Tree[] = [];
let recent_searches: (Block & Show)[] = [];
let worker: Worker;
let ready = false;
let active_color = 'var(--fg)';
Expand Down Expand Up @@ -82,8 +84,10 @@
}
$: if ($searching) {
$overlay_open = true;
modal.showModal();
if (modal) {
$overlay_open = true;
modal.showModal();
}
}
function change_color(e: MouseEvent) {
Expand Down
21 changes: 16 additions & 5 deletions src/lib/search/SearchResultList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { player } from '$state/player';
import { search_recent } from '$state/search';
import { createEventDispatcher } from 'svelte';
import type { Tree } from './types';
import type { Tree, Block } from './types';
import { Show } from '@prisma/client';
export let results: Tree[];
export let results: (Block & Show)[] | Tree[];
export let recent_searches: boolean = false;
export let query: string;
Expand Down Expand Up @@ -35,24 +36,34 @@
);
}
}
function play_show(show_or_tree: (Block & Show) | Tree) {
const local_show: Block & Show = is_tree(show_or_tree) ? show_or_tree.node : show_or_tree;
player.play_show(local_show);
}
function is_tree(show_or_tree: (Block & Show) | Tree): show_or_tree is Tree {
return 'node' in show_or_tree;
}
</script>

<ul>
{#each results as result (result.href)}
<li>
<button on:click|preventDefault={() => player.play_show(result.node)} class="play-button">
<!-- Show data not available in recent searches -->
<button on:click|preventDefault={() => play_show(result)} class="play-button">
<Icon name="play" />
</button>

<a
data-sveltekit-preload-data
href={result.href}
on:click={() => dispatch('select', { href: result.href })}
data-has-node={result.node ? true : undefined}
data-has-node={is_tree(result) ? true : undefined}
>
<strong>{@html excerpt(result.breadcrumbs[result.breadcrumbs.length - 1], query)}</strong>

{#if result.node?.content}
{#if is_tree(result) && result.node?.content}
<span class="text-sm">{@html excerpt(result.node.content, query)}</span>
{/if}
</a>
Expand Down
1 change: 0 additions & 1 deletion src/lib/search/search-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ addEventListener('message', async (event) => {

if (type === 'recents') {
const results = payload.map(lookup).filter(Boolean);

postMessage({ type: 'recents', payload: results });
}
});
7 changes: 4 additions & 3 deletions src/lib/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import flexsearch, { type Index } from 'flexsearch';
import type { Block, Tree } from './types';
import { Show } from '@prisma/client';

// @ts-expect-error tbh not sure about this one but sk had it in their code.
const Index = flexsearch.Index ?? flexsearch;
Expand Down Expand Up @@ -63,7 +64,7 @@ export function search(query: string) {
(a?.block?.breadcrumbs.length || 0) - (b?.block?.breadcrumbs.length || 0) || a.rank - b.rank
);
})
.map(({ block }) => block) as Block[];
.map(({ block }) => block) as (Block & Show)[];

const results = tree([], blocks).children;

Expand All @@ -74,13 +75,13 @@ export function lookup(href: string) {
return map.get(href);
}

function tree(breadcrumbs: string[], blocks: Block[]): Tree {
function tree(breadcrumbs: string[], blocks: (Block & Show)[]): Tree {
const depth = breadcrumbs.length;

const node = blocks.find((block) => {
if (block.breadcrumbs.length !== depth) return false;
return breadcrumbs.every((part, i) => block.breadcrumbs[i] === part);
}) as Block;
}) as Block & Show;

const descendants = blocks.filter((block) => {
if (block.breadcrumbs.length <= depth) return false;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Show } from '@prisma/client';

export interface Block {
breadcrumbs: string[];
href: string;
Expand All @@ -8,6 +10,6 @@ export interface Block {
export interface Tree {
breadcrumbs: string[];
href: string;
node: Block;
node: Block & Show;
children: Tree[];
}

0 comments on commit b262c0b

Please sign in to comment.