-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
364 additions
and
20 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | ||
import { Icons } from "@/icons"; | ||
import type { Airing, Media } from "@/types"; | ||
import { | ||
format, | ||
formatDistanceToNowStrict, | ||
fromUnixTime, | ||
isPast, | ||
} from "date-fns"; | ||
|
||
interface AnimesTabsProps { | ||
animes: { | ||
trending: Media[]; | ||
latestAiring: Airing[]; | ||
futureAiring: Airing[]; | ||
upcomingSeason: Media[]; | ||
}; | ||
} | ||
|
||
interface TabsTriggerProps { | ||
value: string; | ||
label: string; | ||
icon: keyof typeof Icons; | ||
} | ||
|
||
const tabTriggers: TabsTriggerProps[] = [ | ||
{ | ||
value: "latestAiring", | ||
label: "In Last 24 Hours", | ||
icon: "tv", | ||
}, | ||
{ | ||
value: "futureAiring", | ||
label: "Coming Up Next!", | ||
icon: "clock", | ||
}, | ||
{ value: "trending", label: "Trending", icon: "flame" }, | ||
{ value: "upcomingSeason", label: "Upcoming Season", icon: "calendar" }, | ||
]; | ||
|
||
export function AnimesTabs({ animes }: AnimesTabsProps) { | ||
return ( | ||
<Tabs defaultValue="latestAiring" className="w-full"> | ||
<p className="text-sm text-muted-foreground"> | ||
<i>Images aren't optimized.</i> | ||
</p> | ||
<TabsList className="!bg-muted/80 mt-2 mb-4"> | ||
{tabTriggers.map((tab) => { | ||
const Icon = Icons[tab.icon || "flame"]; | ||
return ( | ||
<TabsTrigger key={tab.value} value={tab.value}> | ||
<div className="flex gap-x-2 items-center"> | ||
<Icon className="size-5" /> | ||
<span className="hidden sm:inline-flex">{tab.label}</span> | ||
</div> | ||
</TabsTrigger> | ||
); | ||
})} | ||
</TabsList> | ||
|
||
{Object.entries(animes).map(([key, value]) => ( | ||
<TabsContent key={key} value={key}> | ||
<div className="anime-grid"> | ||
{value.map((anime, i) => ( | ||
<AnimeCard key={key + "-" + i} data={anime} /> | ||
))} | ||
</div> | ||
</TabsContent> | ||
))} | ||
</Tabs> | ||
); | ||
} | ||
|
||
function AnimeCard({ data }: { data: Media | Airing }) { | ||
let media, airingAt, episode; | ||
|
||
if ("media" in data) { | ||
media = data.media; | ||
airingAt = data.airingAt; | ||
episode = data.episode; | ||
} else { | ||
media = data; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col w-full"> | ||
<div className="w-full h-[280px] rounded-md overflow-hidden bg-muted-foreground/15"> | ||
<img | ||
width={260} | ||
height={300} | ||
loading="eager" | ||
className="size-full object-cover object-center" | ||
src={media.coverImage.extraLarge} | ||
alt={media.title.userPreferred} | ||
/> | ||
</div> | ||
|
||
<div className="flex w-full flex-col justify-center"> | ||
<p className="font-medium text-sm text-balance line-clamp-1 mt-1 text-foreground"> | ||
{media.title.userPreferred} | ||
</p> | ||
|
||
{airingAt && episode ? ( | ||
<p className="flex text-xs sm:text-[13px] font-medium text-muted-foreground line-clamp-1"> | ||
{isPast(fromUnixTime(airingAt)) | ||
? formatDistanceToNowStrict(fromUnixTime(airingAt), { | ||
addSuffix: true, | ||
}) | ||
: format(fromUnixTime(airingAt), "p")} | ||
• Ep. {episode} | ||
</p> | ||
) : null} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { getSeasonInfo } from "@/lib/utils"; | ||
import type { Airing, Media } from "@/types"; | ||
import { addDays, getUnixTime, subDays } from "date-fns"; | ||
|
||
const graphqlAPI = "https://graphql.anilist.co/"; | ||
|
||
export const getPlanningAnimes = async () => { | ||
const { season, seasonYear, nextSeason, nextYear } = await getSeasonInfo(); | ||
const query = ` | ||
query getMedias( | ||
$season: MediaSeason | ||
$seasonYear: Int | ||
$nextSeason: MediaSeason | ||
$nextYear: Int | ||
$latestAiringStart: Int | ||
$latestAiringEnd: Int | ||
$futureAiringStart: Int | ||
$futureAiringEnd: Int | ||
) { | ||
latestAiring: Page(page: 1, perPage: 50) { | ||
# sort : TIME_DESC | ||
airingSchedules( | ||
airingAt_greater: $latestAiringStart | ||
airingAt_lesser: $latestAiringEnd | ||
sort: TIME_DESC | ||
) { | ||
id | ||
episode | ||
airingAt | ||
media { | ||
...mediaFragment | ||
} | ||
} | ||
} | ||
futureAiring: Page(page: 1, perPage: 50) { | ||
# sort : TIME | ||
airingSchedules( | ||
airingAt_greater: $futureAiringStart | ||
airingAt_lesser: $futureAiringEnd | ||
sort: TIME | ||
) { | ||
id | ||
episode | ||
airingAt | ||
media { | ||
...mediaFragment | ||
} | ||
} | ||
} | ||
trending: Page(page: 1, perPage: 30) { | ||
media(sort: TRENDING_DESC, type: ANIME, isAdult: false) { | ||
...mediaFragment | ||
} | ||
} | ||
season: Page(page: 1, perPage: 30) { | ||
media( | ||
season: $season | ||
seasonYear: $seasonYear | ||
sort: POPULARITY_DESC | ||
type: ANIME | ||
isAdult: false | ||
) { | ||
...mediaFragment | ||
} | ||
} | ||
nextSeason: Page(page: 1, perPage: 30) { | ||
media( | ||
season: $nextSeason | ||
seasonYear: $nextYear | ||
sort: POPULARITY_DESC | ||
type: ANIME | ||
isAdult: false | ||
) { | ||
...mediaFragment | ||
} | ||
} | ||
} | ||
fragment mediaFragment on Media { | ||
id | ||
title { | ||
userPreferred | ||
} | ||
coverImage { | ||
extraLarge | ||
large | ||
} | ||
isAdult | ||
} | ||
`; | ||
|
||
const response = await fetch(graphqlAPI, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
query: query, | ||
variables: { | ||
season: season, | ||
seasonYear: seasonYear, | ||
nextSeason: nextSeason, | ||
nextYear: nextYear, | ||
latestAiringStart: getUnixTime(subDays(new Date(), 1)), | ||
latestAiringEnd: getUnixTime(new Date()), | ||
futureAiringStart: getUnixTime(new Date()), | ||
futureAiringEnd: getUnixTime(addDays(new Date(), 1)), | ||
}, | ||
}), | ||
}); | ||
|
||
const { data } = await response.json(); | ||
|
||
const trending: Media[] = data.trending.media; | ||
const upcomingSeason: Media[] = data.nextSeason.media; | ||
|
||
const latestAiring: Airing[] = data?.latestAiring?.airingSchedules.filter( | ||
(airing: Airing) => airing.media.isAdult == false | ||
); | ||
|
||
const futureAiring: Airing[] = data?.futureAiring?.airingSchedules.filter( | ||
(airing: Airing) => airing.media.isAdult == false | ||
); | ||
|
||
return { | ||
trending, | ||
upcomingSeason, | ||
latestAiring, | ||
futureAiring, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.