chore: use typedef instead of inline type

This commit is contained in:
Adrian Castro
2024-02-04 17:38:03 +01:00
parent e88b7d2051
commit 1b9fbb4120
2 changed files with 15 additions and 23 deletions

View File

@@ -3,15 +3,14 @@ import { ScrollView, View } from "react-native";
import { getMediaPoster, searchTitle } from "@movie-web/tmdb"; import { getMediaPoster, searchTitle } from "@movie-web/tmdb";
import type { ItemData } from "~/components/item/item";
import Item from "~/components/item/item"; import Item from "~/components/item/item";
import ScreenLayout from "~/components/layout/ScreenLayout"; import ScreenLayout from "~/components/layout/ScreenLayout";
import { Text } from "~/components/ui/Text"; import { Text } from "~/components/ui/Text";
import Searchbar from "./Searchbar"; import Searchbar from "./Searchbar";
export default function SearchScreen() { export default function SearchScreen() {
const [searchResults, setSearchResults] = useState< const [searchResults, setSearchResults] = useState<ItemData[]>([]);
{ title: string; posterUrl: string; year: number; type: "movie" | "tv" }[]
>([]);
const handleSearchChange = async (query: string) => { const handleSearchChange = async (query: string) => {
if (query.length > 0) { if (query.length > 0) {
@@ -45,11 +44,7 @@ export default function SearchScreen() {
); );
} }
async function fetchSearchResults( async function fetchSearchResults(query: string): Promise<ItemData[]> {
query: string,
): Promise<
{ title: string; posterUrl: string; year: number; type: "movie" | "tv" }[]
> {
const results = await searchTitle(query); const results = await searchTitle(query);
return results return results
@@ -57,6 +52,7 @@ async function fetchSearchResults(
switch (result.media_type) { switch (result.media_type) {
case "movie": case "movie":
return { return {
id: result.id.toString(),
title: result.title, title: result.title,
posterUrl: getMediaPoster(result.poster_path), posterUrl: getMediaPoster(result.poster_path),
year: new Date(result.release_date).getFullYear(), year: new Date(result.release_date).getFullYear(),
@@ -64,6 +60,7 @@ async function fetchSearchResults(
}; };
case "tv": case "tv":
return { return {
id: result.id.toString(),
title: result.name, title: result.name,
posterUrl: getMediaPoster(result.poster_path), posterUrl: getMediaPoster(result.poster_path),
year: new Date(result.first_air_date).getFullYear(), year: new Date(result.first_air_date).getFullYear(),
@@ -73,14 +70,5 @@ async function fetchSearchResults(
return undefined; return undefined;
} }
}) })
.filter( .filter((item): item is ItemData => item !== undefined);
(
item,
): item is {
title: string;
posterUrl: string;
year: number;
type: "movie" | "tv";
} => item !== undefined,
);
} }

View File

@@ -2,11 +2,15 @@ import { Image, View } from "react-native";
import { Text } from "~/components/ui/Text"; import { Text } from "~/components/ui/Text";
export default function Item({ export interface ItemData {
data, id: string;
}: { title: string;
data: { title: string; type: string; year: number; posterUrl: string }; type: "movie" | "tv";
}) { year: number;
posterUrl: string;
}
export default function Item({ data }: { data: ItemData }) {
const { title, type, year, posterUrl } = data; const { title, type, year, posterUrl } = data;
return ( return (