Skip to content

Commit 8ca03f8

Browse files
Copilotargyleink
andcommitted
refactor: improve code quality and type safety in search feature
- Move SearchableEpisode type to rss.ts for reuse across components - Fix potential runtime error with optional episodeNumber field - Import shared type in SearchDialog.tsx and search.json.ts Co-authored-by: argyleink <1134620+argyleink@users.noreply.github.com>
1 parent 0a0ab47 commit 8ca03f8

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/components/SearchDialog.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import type { JSX } from 'preact/jsx-runtime';
22
import { useEffect, useState, useRef, useCallback } from 'preact/hooks';
33
import { isSearchOpen } from './state';
4-
import type { Episode } from '../lib/rss';
5-
6-
type SearchableEpisode = Pick<
7-
Episode,
8-
'id' | 'title' | 'description' | 'episodeNumber' | 'episodeSlug' | 'episodeThumbnail'
9-
>;
4+
import type { SearchableEpisode } from '../lib/rss';
105

116
export default function SearchDialog() {
127
const [query, setQuery] = useState('');
@@ -40,7 +35,7 @@ export default function SearchDialog() {
4035
(episode) =>
4136
episode.title.toLowerCase().includes(lowerQuery) ||
4237
episode.description.toLowerCase().includes(lowerQuery) ||
43-
episode.episodeNumber.toLowerCase().includes(lowerQuery)
38+
(episode.episodeNumber?.toLowerCase().includes(lowerQuery) ?? false)
4439
)
4540
.slice(0, 8);
4641

src/lib/rss.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ export interface Episode {
3131
};
3232
}
3333

34+
// Subset of Episode for search functionality
35+
export type SearchableEpisode = Pick<
36+
Episode,
37+
'id' | 'title' | 'description' | 'episodeNumber' | 'episodeSlug' | 'episodeThumbnail'
38+
>;
39+
3440
let showInfoCache: Show | null = null;
3541

3642
export async function getShowInfo() {

src/pages/api/episodes/search.json.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { APIRoute } from 'astro';
2-
import { getAllEpisodes } from '../../../lib/rss';
2+
import { getAllEpisodes, type SearchableEpisode } from '../../../lib/rss';
33

44
export const prerender = true;
55

66
export const GET: APIRoute = async () => {
77
const allEpisodes = await getAllEpisodes();
88

99
// Return a simplified list of episodes optimized for search
10-
const searchableEpisodes = allEpisodes.map((episode) => ({
10+
const searchableEpisodes: SearchableEpisode[] = allEpisodes.map((episode) => ({
1111
id: episode.id,
1212
title: episode.title,
1313
description: episode.description,

0 commit comments

Comments
 (0)