Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add not found page #13

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import HomePage from './pages/HomePage.tsx'
import DiaryPage from './pages/DiaryPage.tsx'
import { SettingsProvider } from './context/SettingsContext.tsx'
import NotFoundPage from './pages/NotFoundPage.tsx'

const router = createBrowserRouter([
{
path: '*',
element: <NotFoundPage />,
},
{
path: '/',
element: <HomePage />,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Calendar as CalendarBase, CalendarDate } from 'calendar-base'
import moment from 'moment'
import { Link } from 'react-router-dom'
import { months } from '../util/time'
import { months } from '../util/date'

type DayProps = {
date: CalendarDate
Expand Down
12 changes: 10 additions & 2 deletions src/pages/DiaryPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@mdxeditor/editor/style.css'
import { Link, useParams } from 'react-router-dom'
import { Link, useNavigate, useParams } from 'react-router-dom'
import {
MDXEditor,
headingsPlugin,
Expand All @@ -20,8 +20,9 @@ import {
dateStatus,
getNextDayLink,
getPreviousDayLink,
isDateValid,
months,
} from '../util/time'
} from '../util/date'
import { debounce } from 'lodash'
import { useEffect, useRef, useState } from 'react'
import moment from 'moment'
Expand All @@ -34,6 +35,7 @@ type ParamsType = {

const DiaryPage = () => {
const { year, month, day } = useParams<ParamsType>()
const navigate = useNavigate()
const [canEdit, setCanEdit] = useState(false)
const [loading, setLoading] = useState(true)
const [fileContent, setFileContent] = useState('')
Expand All @@ -51,6 +53,12 @@ const DiaryPage = () => {
])
)

useEffect(() => {
if (!isDateValid(day, month, year)) {
navigate('/')
}
}, [day, month, year, navigate])

useEffect(() => {
if (year && month && day) {
const isToday = moment([year, month, day]).isSame(moment().startOf('day'))
Expand Down
21 changes: 21 additions & 0 deletions src/pages/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AlertOctagon } from 'lucide-react'
import { Link } from 'react-router-dom'
import { Button } from '@/components/ui/button'

const NotFoundPage = () => {
return (
<div className="h-screen">
<div className="flex flex-col items-center justify-center h-full gap-4">
<AlertOctagon className="h-20 w-20" />
<h3 className="scroll-m-20 text-2xl font-semibold tracking-tight">
whoops... whatever you are looking for is not here
</h3>
<Link to="/">
<Button>go back home</Button>
</Link>
</div>
</div>
)
}

export default NotFoundPage
10 changes: 10 additions & 0 deletions src/util/time.ts → src/util/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ export const getPreviousDayLink = (
'month'
)}/${currentDate.get('date')}`
}

export const isDateValid = (day?: string, month?: string, year?: string) =>
!isNaN(Number(year)) &&
!isNaN(Number(month)) &&
!isNaN(Number(day)) &&
moment([
parseInt(year ?? ''),
parseInt(month ?? ''),
parseInt(day ?? ''),
]).isValid()