forked from leerob/site
-
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.
Revert "Upgrade to stable app router. (leerob#612)" (leerob#614)
This reverts commit 2f5145b.
- Loading branch information
Showing
30 changed files
with
1,069 additions
and
1,116 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,32 +1,46 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
import { increment } from 'app/actions'; | ||
import useSWR from 'swr'; | ||
|
||
type PostView = { | ||
slug: string; | ||
count: string; | ||
}; | ||
|
||
async function fetcher<JSON = any>( | ||
input: RequestInfo, | ||
init?: RequestInit | ||
): Promise<JSON> { | ||
const res = await fetch(input, init); | ||
return res.json(); | ||
} | ||
|
||
export default function ViewCounter({ | ||
slug, | ||
allViews, | ||
trackView, | ||
}: { | ||
slug: string; | ||
allViews: { | ||
slug: string; | ||
count: number; | ||
}[]; | ||
trackView?: boolean; | ||
trackView: boolean; | ||
}) { | ||
const viewsForSlug = allViews && allViews.find((view) => view.slug === slug); | ||
const number = new Number(viewsForSlug?.count || 0); | ||
const { data } = useSWR<PostView[]>('/api/views', fetcher); | ||
const viewsForSlug = data && data.find((view) => view.slug === slug); | ||
const views = new Number(viewsForSlug?.count || 0); | ||
|
||
useEffect(() => { | ||
const registerView = () => | ||
fetch(`/api/views/${slug}`, { | ||
method: 'POST', | ||
}); | ||
|
||
if (trackView) { | ||
increment(slug); | ||
registerView(); | ||
} | ||
}, []); | ||
}, [slug]); | ||
|
||
return ( | ||
<p className="font-mono text-sm text-neutral-500 tracking-tighter"> | ||
{`${number.toLocaleString()} views`} | ||
{data ? `${views.toLocaleString()} views` : ''} | ||
</p> | ||
); | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.