"use client"; import { usePathname, useRouter } from "next/navigation"; import { toast } from "@/components/ui/use-toast"; import { cn } from "@/lib/utils"; import React, { useState, useEffect, useRef } from 'react'; import { useUpdateHour } from "@lifetracker/shared-react/hooks/days"; import { EditableText } from "@/components/dashboard/EditableText"; import { format } from "date-fns"; import { TZDate } from "@date-fns/tz"; import { ZHour } from "@lifetracker/shared/types/days"; import { MessageCircle, Pencil } from "lucide-react"; import { ButtonWithTooltip } from "@/components/ui/button"; import { EditableHourCode } from "./EditableHourCode"; import { EditableHourComment } from "./EditableHourComment"; import { api } from "@/lib/trpc"; import spacetime from 'spacetime'; import { eq, is } from "drizzle-orm"; export default function EditableHour({ hour: initialHour, i, className, }: { hour: ZHour, i: number, className?: string; }) { const [hour, setHour] = useState(initialHour); const { mutate: updateHour, isPending } = useUpdateHour({ onSuccess: (res, req, meta) => { const { categoryCode: oldCode, comment: oldComment } = hour; const newHour = { categoryCode: parseInt(req.code!), comment: oldComment, ...res, }; console.log(res); setHour(newHour); // Only show toast if client screen is larger than mobile if (window.innerWidth > 640) { toast({ description: "Hour updated!", }); } }, }); const tzOffset = spacetime().offset() / 60; const localDateTime = spacetime(hour.date).add(hour.time + tzOffset, "hour"); hour.datetime = `${localDateTime.format('{hour} {ampm}')}`; useEffect(() => { // console.log(hour.categoryDesc); }, [hour]); function isActiveHour(hour: ZHour) { const now = new TZDate(); const isCurrentHour = ((now.getHours() - localDateTime.hour()) == 0); const isToday = (localDateTime.format("iso-short") == format(now, "yyyy-MM-dd")); return isToday && isCurrentHour; } return (
640 ? "50px 100px 1fr 50px" : "50px 100px 1fr", // Known issue: This won't work if the screen is resized, only on reload }} > {/* {isActiveHour(hour) && "--> "} */} {hour.datetime}
{hour.categoryName}
{ console.log("Pushed edit") }} >
); }