From 954340c2513837f28e8d9e92c7f85f1296378a91 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Sat, 22 Mar 2025 21:52:39 +0000 Subject: [PATCH] Add article title to comment notifications --- app/actions/comments.ts | 10 +++++----- app/actions/views.ts | 4 ++-- app/api/comments/moderate/[id]/route.ts | 2 +- app/page.tsx | 1 + app/writing/page.tsx | 1 + components/layouts/ArticleLayout.tsx | 2 +- components/ui/Views.tsx | 5 +++-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/actions/comments.ts b/app/actions/comments.ts index bd7b8b0..bb893d2 100644 --- a/app/actions/comments.ts +++ b/app/actions/comments.ts @@ -9,10 +9,10 @@ export async function loginWithGitHub() { await signIn('github') } -const notificationBody = (comment: { id: number, content: string }, user: { name: string }) => { +const notificationBody = (comment: { id: number, content: string }, user: { name: string }, article: { title: string }) => { return { - topic: 'comments', - message: `You've got a new comment from ${user.name}:\n${comment.content}`, + topic: 'portfolio', + message: `New comment on ${article.title} from ${user.name}:\n${comment.content}`, actions: [ { action: 'http', @@ -60,7 +60,7 @@ export async function addComment(prevState: { message: string }, formData: FormD const [{data: user}, {data: article}] = await Promise.all([ supabase.from('users').upsert({name, email, image}, {onConflict: 'email'}).select('*').single(), - supabase.from('articles').select('id').eq('slug', slug).single() + supabase.from('articles').select('*').eq('slug', slug).single() ]) const {data: newComment, error} = await supabase @@ -73,7 +73,7 @@ export async function addComment(prevState: { message: string }, formData: FormD return {message: general_error} } - await sendNotification(notificationBody(newComment, user)) + await sendNotification(notificationBody(newComment, user, article)) return {message: success_message} } catch (error) { diff --git a/app/actions/views.ts b/app/actions/views.ts index 5e16019..e16a6aa 100644 --- a/app/actions/views.ts +++ b/app/actions/views.ts @@ -2,9 +2,9 @@ import {createClient} from '@/lib/supabase/server' -export async function incrementViews(slug: string) { +export async function incrementViews(slug: string, title: string) { if (slug !== null) { const supabase = await createClient() - await supabase.rpc('increment_views', {param_slug: slug}) + await supabase.rpc('increment_views', {param_slug: slug, param_title: title}) } } \ No newline at end of file diff --git a/app/api/comments/moderate/[id]/route.ts b/app/api/comments/moderate/[id]/route.ts index 5519ff8..955126d 100644 --- a/app/api/comments/moderate/[id]/route.ts +++ b/app/api/comments/moderate/[id]/route.ts @@ -14,7 +14,7 @@ export async function PATCH(request: Request, {params}: { params: Promise<{ id: .update({published: true}) .eq('id', id) - return new Response(null, {status: 204}) + return new Response(JSON.stringify({}), {status: 200}) } return new Response(JSON.stringify({status: 'Not Found'}), {status: 404}) } diff --git a/app/page.tsx b/app/page.tsx index b160252..4dfab76 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -49,6 +49,7 @@ function Article(article: Article) { diff --git a/app/writing/page.tsx b/app/writing/page.tsx index 32e475d..fc935a7 100644 --- a/app/writing/page.tsx +++ b/app/writing/page.tsx @@ -46,6 +46,7 @@ function Article({article}: { article: Article }) { diff --git a/components/layouts/ArticleLayout.tsx b/components/layouts/ArticleLayout.tsx index 18209fc..4596e4c 100644 --- a/components/layouts/ArticleLayout.tsx +++ b/components/layouts/ArticleLayout.tsx @@ -54,7 +54,7 @@ export function ArticleLayout({ - +

{children} diff --git a/components/ui/Views.tsx b/components/ui/Views.tsx index 49fe561..d75e241 100644 --- a/components/ui/Views.tsx +++ b/components/ui/Views.tsx @@ -9,11 +9,12 @@ import {incrementViews} from '@/app/actions/views' type ViewsProps = { as?: ElementType slug: string + title: string className?: string shouldUpdateViews?: boolean } -export function Views({as: Component = 'span', slug, className, shouldUpdateViews = true}: ViewsProps) { +export function Views({as: Component = 'span', slug, title, className, shouldUpdateViews = true}: ViewsProps) { const {data} = useSWR(`/api/views/${slug}`, fetcher) as { data: { views: number } } const {mutate} = useSWRConfig() @@ -22,7 +23,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView const updateViews = async () => { const hasViewed = sessionStorage.getItem(`has-viewed-${slug}`) if (!hasViewed) { - await incrementViews(slug) + await incrementViews(slug, title) sessionStorage.setItem(`has-viewed-${slug}`, 'true') }