Skip to content

Commit

Permalink
feat: return to latest page by default (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Nov 15, 2024
1 parent 84fa0ee commit 408b4da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { cookies } from "next/headers"
import { redirect } from "next/navigation"

export default function Home() {
export default async function Home() {
const latestPage = (await cookies()).get('latest_page')
if (latestPage) {
return redirect(latestPage.value)
}

return redirect("/alerts")
}
17 changes: 17 additions & 0 deletions src/app/template.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client'

import { useEffect } from "react"
import { usePathname, useSearchParams } from 'next/navigation'

export default function Template({ children }: { children: React.ReactNode }) {
const pathname = usePathname()
const searchParams = useSearchParams()

// Save the latest seen page to a cookie
useEffect(() => {
const path = `${pathname}${searchParams.toString() !== '' ? '?' + searchParams : ''}`
document.cookie = `latest_page=${path}; path=/;`
}, [pathname, searchParams])

return children
}

0 comments on commit 408b4da

Please sign in to comment.