Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Fix timezones and add routing hack
Browse files Browse the repository at this point in the history
  • Loading branch information
anli5005 committed Dec 30, 2023
1 parent d333c85 commit 48e920d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
19 changes: 12 additions & 7 deletions app/assignments/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { allHomework, allAssessments } from 'contentlayer/generated'
import { Card } from '@/components/Card'
import Link from 'next/link'
import { FormattedDate } from '@/components/FormattedDate'
import { FormattedDate, defaultTimezone } from '@/components/FormattedDate'
import { toDate } from 'date-fns-tz'

type Assignment = {
title: string
Expand All @@ -16,18 +17,22 @@ type Assignment = {
sortDate: Date
}

export function parseDate(str: string) {
return toDate(str, { timeZone: defaultTimezone })
}

export const formattedHomework: Assignment[] = allHomework.map(hw => {
const due = new Date(hw.dueDate)
const due = parseDate(hw.dueDate)

return {
title: hw.title,
href: `/assignments/hw/${hw.slug}`,
isReleased: hw.isReleased,
releaseDate: hw.releaseDate && new Date(hw.releaseDate),
releaseDate: hw.releaseDate && parseDate(hw.releaseDate),
dates: [
...(hw.auxiliaryDates ?? []).map(aux => ({
name: aux.name,
date: new Date(aux.date),
date: parseDate(aux.date),
specifyTime: true,
})),
{
Expand All @@ -36,18 +41,18 @@ export const formattedHomework: Assignment[] = allHomework.map(hw => {
specifyTime: true,
}
],
sortDate: new Date(hw.dueDate),
sortDate: parseDate(hw.dueDate),
}
})

export const formattedAssessments = allAssessments.map(a => {
const date = new Date(a.assessmentDate)
const date = parseDate(a.assessmentDate)

return {
title: a.title,
href: `/assignments/assessment/${a.slug}`,
isReleased: a.isReleased,
releaseDate: a.releaseDate && new Date(a.releaseDate),
releaseDate: a.releaseDate && parseDate(a.releaseDate),
dates: [
{
name: "Scheduled",
Expand Down
13 changes: 10 additions & 3 deletions app/menu.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"

import Link from "next/link"
import { usePathname } from "next/navigation"
import { usePathname, useRouter } from "next/navigation"
import { ReactNode, createContext, useContext, useEffect, useState } from "react"

export type MenuItemProps = {
Expand Down Expand Up @@ -58,7 +58,6 @@ export function MenuContextProvider({ children }: { children: ReactNode }) {
return <MenuContext.Provider value={{
activeItem: activeState.pathname === pathname ? activeState.item : null,
setActiveItem(item) {
console.log({ item, pathname })
setActiveState({ item, pathname })
}
}}>
Expand Down Expand Up @@ -88,6 +87,7 @@ export function MenuItemActivator({ item }: MenuItemActivatorProps) {

function MenuItem({ id, title, icon, href }: MenuItemProps) {
const context = useMenuContext()
const router = useRouter()
const isActive = id === context.activeItem

let className = "block px-3 py-2 rounded-xl group relative bg-opacity-0"
Expand All @@ -100,7 +100,14 @@ function MenuItem({ id, title, icon, href }: MenuItemProps) {
let containerClassName = "flex gap-3 transition-[margin-left] justify-center text-center md:text-left"
if (!isActive) containerClassName += " md:group-hover:ml-1"

return <Link className={className} href={href}>
return <Link className={className} href={href} onClick={e => {
if (href === "/") {
// HACK: For some reason navigating to / forces a full refresh
// So we just intercept it and do it ourselves instead
router.push("/")
e.preventDefault()
}
}}>
<div className={containerClassName}>
<div className="w-4">{icon}</div>
<div className="md:grow line-clamp-1 overflow-hidden overflow-ellipsis">{title}</div>
Expand Down
2 changes: 1 addition & 1 deletion components/FormattedDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type FormattedDateProps = {
format: string
}

const defaultTimezone = "America/New_York"
export const defaultTimezone = "America/New_York"

export function FormattedDate({ date, format: formatStr }: FormattedDateProps) {
const [value, setValue] = useState(formatInTimeZone(date, defaultTimezone, formatStr, {
Expand Down

0 comments on commit 48e920d

Please sign in to comment.