-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from dotslashf/development
Custom Tweet Component
- Loading branch information
Showing
20 changed files
with
528 additions
and
48 deletions.
There are no files selected for viewing
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
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,68 @@ | ||
import { parseISO } from "date-fns"; | ||
import { X } from "lucide-react"; | ||
import React, { useState, useEffect } from "react"; | ||
import { enrichTweet } from "react-tweet"; | ||
import { type Tweet } from "react-tweet/api"; | ||
import EmptyState from "~/components/EmptyState"; | ||
import CustomTweet from "~/components/Tweet/CustomTweet"; | ||
import SkeletonTweet from "~/components/Tweet/SkeletonTweet"; | ||
import { sanitizeTweetEnrich } from "~/lib/utils"; | ||
|
||
interface TweetPageProps { | ||
id: string; | ||
onTweetLoaded: ({ | ||
content, | ||
postedAt, | ||
}: { | ||
content: string; | ||
postedAt: Date; | ||
}) => void; | ||
} | ||
export default function TweetPage({ id, onTweetLoaded }: TweetPageProps) { | ||
const [tweet, setTweet] = useState<Tweet | undefined | null>(null); | ||
const [error, setError] = useState<Error | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
const fetchTweet = async () => { | ||
setError(null); | ||
try { | ||
setIsLoading(true); | ||
const response = await fetch(`/api/tweet/${id}`); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch tweet"); | ||
} | ||
const { tweet }: { tweet: Tweet } = await response.json(); | ||
setTweet(tweet); | ||
const enrich = enrichTweet(tweet); | ||
onTweetLoaded({ | ||
content: sanitizeTweetEnrich(enrich), | ||
postedAt: parseISO(tweet.created_at), | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
setError(err instanceof Error ? err : new Error("An error occurred")); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
void fetchTweet(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [id]); | ||
|
||
if (error) | ||
return ( | ||
<EmptyState | ||
className="border-solid text-lg" | ||
message={ | ||
<span className="inline-flex items-center"> | ||
Tweet tidak ditemukan <X className="ml-2 inline" /> | ||
</span> | ||
} | ||
/> | ||
); | ||
if (!tweet || isLoading) return <SkeletonTweet />; | ||
|
||
return <CustomTweet tweet={tweet} />; | ||
} |
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,43 @@ | ||
import { unstable_cache } from "next/cache"; | ||
import { NextResponse, type NextRequest } from "next/server"; | ||
import { getTweet as _getTweet } from "react-tweet/api"; | ||
|
||
const getTweet = unstable_cache( | ||
async (id: string) => _getTweet(id), | ||
["tweet"], | ||
{ revalidate: 3600 * 24 }, | ||
); | ||
|
||
export async function GET( | ||
_: NextRequest, | ||
{ params }: { params: { id: string } }, | ||
) { | ||
try { | ||
const tweet = await getTweet(params.id); | ||
if (!tweet) { | ||
return NextResponse.json( | ||
{ error: "Failed to fetch tweet" }, | ||
{ | ||
status: 404, | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}, | ||
); | ||
} | ||
return NextResponse.json({ | ||
tweet, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json( | ||
{ error: "Failed to fetch tweet" }, | ||
{ | ||
status: 404, | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}, | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.