"use client"; import { usePathname, useRouter } from "next/navigation"; import { toast } from "@/components/ui/use-toast"; import { cn } from "@/lib/utils"; 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 { useEffect, useRef } from "react"; function selectNext(time: number) { document.getElementById("hour-" + (time).toString())?.getElementsByClassName("edit-hour-code")[0].focus(); } function EditCode({ originalText, hour, onSubmit }) { const ref = useRef(null); useEffect(() => { if (ref.current) { ref.current.value = originalText; } }, [ref]); const submit = () => { let newCode: string | null = ref.current?.value ?? null; if (originalText == newCode) { // Nothing to do here return; } if (newCode == "") { if (originalText == null) { // Set value to previous hour's value newCode = document.getElementById("hour-" + (hour.time - 1).toString())?.getElementsByClassName("edit-hour-code")[0].value; console.log(newCode); } else { newCode = null; } } // console.log(hour); onSubmit({ dateQuery: hour.date, time: hour.time, code: newCode, }) selectNext(hour.time + 1); }; return ( { if (e.key === "Enter") { e.preventDefault(); submit(); } if (e.key == "ArrowDown") { e.preventDefault(); selectNext(hour.time + 1); } if (e.key == "ArrowUp") { e.preventDefault(); selectNext(hour.time - 1); } }} onClick={(e) => { const range = document.createRange(); range.selectNodeContents(ref.current); const selection = window.getSelection(); if (selection) { selection.removeAllRanges(); selection.addRange(range); } }} /> // { // if (e.key === "Enter") { // e.preventDefault(); // onSave(); // } // }} // value={originalText.originalText ?? ""} // /> ); } export default function EditableHour({ hour, className, }: { hour: ZHour, className?: string; }) { const router = useRouter(); const currentPath = usePathname(); const { mutate: updateHour, isPending } = useUpdateHour({ onSuccess: () => { toast({ description: "Hour updated!", }); if (currentPath.includes("dashboard")) { router.refresh(); } }, }); function isActiveHour(hour: ZHour) { const now = new TZDate(); return (hour.date == format(now, "yyyy-MM-dd")) && (((now.getHours()) + (now.getTimezoneOffset() / 60) - (parseInt(hour.time))) == 0) } return (
{isActiveHour(hour) && "--> "} {hour.datetime}
{hour.categoryDesc || " "}
); }