mirror of
https://github.com/ManInDark/HabitTrove.git
synced 2026-01-21 06:34:30 +01:00
Show overdue tasks and improved context menu (#110)
This commit is contained in:
@@ -1,26 +1,35 @@
|
||||
import { Circle, Coins, ArrowRight, CircleCheck, ChevronDown, ChevronUp, Timer, Plus, Pin, Calendar } from 'lucide-react'
|
||||
import { Circle, Coins, ArrowRight, CircleCheck, ChevronDown, ChevronUp, Plus, Pin, AlertTriangle } from 'lucide-react' // Removed unused icons
|
||||
import CompletionCountBadge from './CompletionCountBadge'
|
||||
import {
|
||||
ContextMenu,
|
||||
ContextMenuContent,
|
||||
ContextMenuItem,
|
||||
ContextMenuSeparator,
|
||||
ContextMenuTrigger,
|
||||
} from "@/components/ui/context-menu"
|
||||
import { cn } from '@/lib/utils'
|
||||
import Link from 'next/link'
|
||||
import { useState } from 'react'
|
||||
import { useAtom } from 'jotai'
|
||||
import { pomodoroAtom, settingsAtom, completedHabitsMapAtom, browserSettingsAtom, BrowserSettings, hasTasksAtom, dailyHabitsAtom } from '@/lib/atoms'
|
||||
import { getTodayInTimezone, isSameDate, t2d, d2t, getNow } from '@/lib/utils'
|
||||
import { pomodoroAtom, settingsAtom, completedHabitsMapAtom, browserSettingsAtom, BrowserSettings, hasTasksAtom } from '@/lib/atoms'
|
||||
import { getTodayInTimezone, isSameDate, t2d, d2t, getNow, isHabitDue, isTaskOverdue } from '@/lib/utils'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip"
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { Settings, WishlistItemType } from '@/lib/types'
|
||||
import { Habit } from '@/lib/types'
|
||||
import Linkify from './linkify'
|
||||
import { useHabits } from '@/hooks/useHabits'
|
||||
import AddEditHabitModal from './AddEditHabitModal'
|
||||
import ConfirmDialog from './ConfirmDialog'
|
||||
import { Button } from './ui/button'
|
||||
import { HabitContextMenuItems } from './HabitContextMenuItems'
|
||||
|
||||
interface UpcomingItemsProps {
|
||||
habits: Habit[]
|
||||
@@ -34,13 +43,7 @@ interface ItemSectionProps {
|
||||
emptyMessage: string;
|
||||
isTask: boolean;
|
||||
viewLink: string;
|
||||
expanded: boolean;
|
||||
setExpanded: (value: boolean) => void;
|
||||
addNewItem: () => void;
|
||||
badgeType: "tasks" | "habits";
|
||||
todayCompletions: Habit[];
|
||||
settings: Settings;
|
||||
setBrowserSettings: (value: React.SetStateAction<BrowserSettings>) => void;
|
||||
}
|
||||
|
||||
const ItemSection = ({
|
||||
@@ -49,16 +52,46 @@ const ItemSection = ({
|
||||
emptyMessage,
|
||||
isTask,
|
||||
viewLink,
|
||||
expanded,
|
||||
setExpanded,
|
||||
addNewItem,
|
||||
badgeType,
|
||||
todayCompletions,
|
||||
settings,
|
||||
setBrowserSettings,
|
||||
}: ItemSectionProps) => {
|
||||
const { completeHabit, undoComplete, saveHabit, habitFreqMap } = useHabits();
|
||||
const { completeHabit, undoComplete, saveHabit, deleteHabit, archiveHabit, habitFreqMap } = useHabits();
|
||||
const [_, setPomo] = useAtom(pomodoroAtom);
|
||||
const [browserSettings, setBrowserSettings] = useAtom(browserSettingsAtom);
|
||||
const [settings] = useAtom(settingsAtom);
|
||||
const [completedHabitsMap] = useAtom(completedHabitsMapAtom);
|
||||
|
||||
const today = getTodayInTimezone(settings.system.timezone);
|
||||
const currentTodayCompletions = completedHabitsMap.get(today) || [];
|
||||
const currentBadgeType = isTask ? 'tasks' : 'habits';
|
||||
|
||||
const currentExpanded = isTask ? browserSettings.expandedTasks : browserSettings.expandedHabits;
|
||||
const setCurrentExpanded = (value: boolean) => {
|
||||
setBrowserSettings(prev => ({
|
||||
...prev,
|
||||
[isTask ? 'expandedTasks' : 'expandedHabits']: value
|
||||
}));
|
||||
};
|
||||
|
||||
const [isConfirmDeleteDialogOpen, setIsConfirmDeleteDialogOpen] = useState(false);
|
||||
const [habitToDelete, setHabitToDelete] = useState<Habit | null>(null);
|
||||
const [habitToEdit, setHabitToEdit] = useState<Habit | null>(null);
|
||||
|
||||
const handleDeleteClick = (habit: Habit) => {
|
||||
setHabitToDelete(habit);
|
||||
setIsConfirmDeleteDialogOpen(true);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (habitToDelete) {
|
||||
await deleteHabit(habitToDelete.id);
|
||||
setHabitToDelete(null);
|
||||
setIsConfirmDeleteDialogOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditClick = (habit: Habit) => {
|
||||
setHabitToEdit(habit);
|
||||
};
|
||||
|
||||
if (items.length === 0) {
|
||||
return (
|
||||
@@ -89,7 +122,7 @@ const ItemSection = ({
|
||||
<h3 className="font-semibold">{title}</h3>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<CompletionCountBadge type={badgeType} />
|
||||
<CompletionCountBadge type={currentBadgeType} />
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
@@ -101,7 +134,7 @@ const ItemSection = ({
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<ul className={`grid gap-2 transition-all duration-300 ease-in-out ${expanded ? 'max-h-none' : 'max-h-[200px]'} overflow-hidden`}>
|
||||
<ul className={`grid gap-2 transition-all duration-300 ease-in-out ${currentExpanded ? 'max-h-none' : 'max-h-[200px]'} overflow-hidden`}>
|
||||
{items
|
||||
.sort((a, b) => {
|
||||
// First by pinned status
|
||||
@@ -110,8 +143,8 @@ const ItemSection = ({
|
||||
}
|
||||
|
||||
// Then by completion status
|
||||
const aCompleted = todayCompletions.includes(a);
|
||||
const bCompleted = todayCompletions.includes(b);
|
||||
const aCompleted = currentTodayCompletions.includes(a);
|
||||
const bCompleted = currentTodayCompletions.includes(b);
|
||||
if (aCompleted !== bCompleted) {
|
||||
return aCompleted ? 1 : -1;
|
||||
}
|
||||
@@ -134,7 +167,7 @@ const ItemSection = ({
|
||||
const bTarget = b.targetCompletions || 1;
|
||||
return bTarget - aTarget;
|
||||
})
|
||||
.slice(0, expanded ? undefined : 5)
|
||||
.slice(0, currentExpanded ? undefined : 5)
|
||||
.map((habit) => {
|
||||
const completionsToday = habit.completions.filter(completion =>
|
||||
isSameDate(t2d({ timestamp: completion, timezone: settings.system.timezone }), t2d({ timestamp: d2t({ dateTime: getNow({ timezone: settings.system.timezone }) }), timezone: settings.system.timezone }))
|
||||
@@ -190,50 +223,46 @@ const ItemSection = ({
|
||||
)}
|
||||
<Link
|
||||
href={`/habits?highlight=${habit.id}`}
|
||||
className={cn(
|
||||
isCompleted ? 'line-through' : '',
|
||||
'break-all hover:text-primary transition-colors'
|
||||
)}
|
||||
className="flex items-center gap-1 hover:text-primary transition-colors"
|
||||
onClick={() => {
|
||||
const newViewType = isTask ? 'tasks' : 'habits';
|
||||
if (browserSettings.viewType !== newViewType) {
|
||||
setBrowserSettings(prev => ({ ...prev, viewType: newViewType }));
|
||||
}
|
||||
}}
|
||||
>
|
||||
{habit.name}
|
||||
{isTask && isTaskOverdue(habit, settings.system.timezone) && !isCompleted && (
|
||||
<TooltipProvider>
|
||||
<Tooltip delayDuration={0}>
|
||||
<TooltipTrigger asChild>
|
||||
{/* The AlertTriangle itself doesn't need hover styles if the parent Link handles it */}
|
||||
<AlertTriangle className="h-4 w-4 flex-shrink-0 text-red-600 dark:text-red-500" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Overdue</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
)}
|
||||
<span
|
||||
className={cn(
|
||||
isCompleted ? 'line-through' : '',
|
||||
'break-all' // Text specific styles
|
||||
)}
|
||||
>
|
||||
{habit.name}
|
||||
</span>
|
||||
</Link>
|
||||
</span>
|
||||
</div>
|
||||
</ContextMenuTrigger>
|
||||
<ContextMenuContent className="w-64">
|
||||
<ContextMenuItem onClick={() => {
|
||||
setPomo((prev) => ({
|
||||
...prev,
|
||||
show: true,
|
||||
selectedHabitId: habit.id
|
||||
}))
|
||||
}}>
|
||||
<Timer className="mr-2 h-4 w-4" />
|
||||
<span>Start Pomodoro</span>
|
||||
</ContextMenuItem>
|
||||
{habit.isTask && (
|
||||
<ContextMenuItem onClick={() => {
|
||||
saveHabit({ ...habit, frequency: d2t({ dateTime: getNow({ timezone: settings.system.timezone }) }) })
|
||||
}}>
|
||||
<Calendar className="mr-2 h-4 w-4" />
|
||||
<span>Move to Today</span>
|
||||
</ContextMenuItem>
|
||||
)}
|
||||
<ContextMenuItem onClick={() => {
|
||||
saveHabit({ ...habit, pinned: !habit.pinned })
|
||||
}}>
|
||||
{habit.pinned ? (
|
||||
<>
|
||||
<Pin className="mr-2 h-4 w-4" />
|
||||
<span>Unpin</span>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Pin className="mr-2 h-4 w-4" />
|
||||
<span>Pin</span>
|
||||
</>
|
||||
)}
|
||||
</ContextMenuItem>
|
||||
<HabitContextMenuItems
|
||||
habit={habit}
|
||||
onEditRequest={() => handleEditClick(habit)}
|
||||
onDeleteRequest={() => handleDeleteClick(habit)}
|
||||
context="daily-overview"
|
||||
/>
|
||||
</ContextMenuContent>
|
||||
</ContextMenu>
|
||||
</span>
|
||||
@@ -271,10 +300,10 @@ const ItemSection = ({
|
||||
</ul>
|
||||
<div className="flex items-center justify-between">
|
||||
<button
|
||||
onClick={() => setExpanded(!expanded)}
|
||||
onClick={() => setCurrentExpanded(!currentExpanded)}
|
||||
className="text-sm text-muted-foreground hover:text-primary flex items-center gap-1"
|
||||
>
|
||||
{expanded ? (
|
||||
{currentExpanded ? (
|
||||
<>
|
||||
Show less
|
||||
<ChevronUp className="h-3 w-3" />
|
||||
@@ -290,10 +319,9 @@ const ItemSection = ({
|
||||
href={viewLink}
|
||||
className="text-sm text-muted-foreground hover:text-primary flex items-center gap-1"
|
||||
onClick={() => {
|
||||
if (isTask) {
|
||||
setBrowserSettings(prev => ({ ...prev, viewType: 'tasks' }));
|
||||
} else {
|
||||
setBrowserSettings(prev => ({ ...prev, viewType: 'habits' }));
|
||||
const newViewType = isTask ? 'tasks' : 'habits';
|
||||
if (browserSettings.viewType !== newViewType) {
|
||||
setBrowserSettings(prev => ({ ...prev, viewType: newViewType }));
|
||||
}
|
||||
}}
|
||||
>
|
||||
@@ -301,6 +329,27 @@ const ItemSection = ({
|
||||
<ArrowRight className="h-3 w-3" />
|
||||
</Link>
|
||||
</div>
|
||||
{habitToDelete && (
|
||||
<ConfirmDialog
|
||||
isOpen={isConfirmDeleteDialogOpen}
|
||||
onClose={() => setIsConfirmDeleteDialogOpen(false)}
|
||||
onConfirm={confirmDelete}
|
||||
title={`Delete ${isTask ? 'Task' : 'Habit'}`}
|
||||
message={`Are you sure you want to delete "${habitToDelete.name}"? This action cannot be undone.`}
|
||||
confirmText="Delete"
|
||||
/>
|
||||
)}
|
||||
{habitToEdit && (
|
||||
<AddEditHabitModal
|
||||
onClose={() => setHabitToEdit(null)}
|
||||
onSave={async (updatedHabit) => {
|
||||
await saveHabit({ ...habitToEdit, ...updatedHabit });
|
||||
setHabitToEdit(null);
|
||||
}}
|
||||
habit={habitToEdit}
|
||||
isTask={habitToEdit.isTask || false}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -313,14 +362,25 @@ export default function DailyOverview({
|
||||
const { completeHabit, undoComplete } = useHabits()
|
||||
const [settings] = useAtom(settingsAtom)
|
||||
const [completedHabitsMap] = useAtom(completedHabitsMapAtom)
|
||||
const [dailyItems] = useAtom(dailyHabitsAtom)
|
||||
const [browserSettings, setBrowserSettings] = useAtom(browserSettingsAtom)
|
||||
const dailyTasks = dailyItems.filter(habit => habit.isTask)
|
||||
const dailyHabits = dailyItems.filter(habit => !habit.isTask)
|
||||
const today = getTodayInTimezone(settings.system.timezone)
|
||||
const todayCompletions = completedHabitsMap.get(today) || []
|
||||
const { saveHabit } = useHabits()
|
||||
|
||||
const timezone = settings.system.timezone
|
||||
const todayDateObj = getNow({ timezone })
|
||||
|
||||
const dailyTasks = habits.filter(habit =>
|
||||
habit.isTask &&
|
||||
!habit.archived &&
|
||||
(isHabitDue({ habit, timezone, date: todayDateObj }) || isTaskOverdue(habit, timezone))
|
||||
)
|
||||
const dailyHabits = habits.filter(habit =>
|
||||
!habit.isTask &&
|
||||
!habit.archived &&
|
||||
isHabitDue({ habit, timezone, date: todayDateObj })
|
||||
)
|
||||
|
||||
// Get all wishlist items sorted by redeemable status (non-redeemable first) then by coin cost
|
||||
// Filter out archived wishlist items
|
||||
const sortedWishlistItems = wishlistItems
|
||||
@@ -364,13 +424,7 @@ export default function DailyOverview({
|
||||
emptyMessage="No tasks due today. Add some tasks to get started!"
|
||||
isTask={true}
|
||||
viewLink="/habits?view=tasks"
|
||||
expanded={browserSettings.expandedTasks}
|
||||
setExpanded={(value) => setBrowserSettings(prev => ({ ...prev, expandedTasks: value }))}
|
||||
addNewItem={() => setModalConfig({ isOpen: true, isTask: true })}
|
||||
badgeType="tasks"
|
||||
todayCompletions={todayCompletions}
|
||||
settings={settings}
|
||||
setBrowserSettings={setBrowserSettings}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -381,13 +435,7 @@ export default function DailyOverview({
|
||||
emptyMessage="No habits due today. Add some habits to get started!"
|
||||
isTask={false}
|
||||
viewLink="/habits"
|
||||
expanded={browserSettings.expandedHabits}
|
||||
setExpanded={(value) => setBrowserSettings(prev => ({ ...prev, expandedHabits: value }))}
|
||||
addNewItem={() => setModalConfig({ isOpen: true, isTask: false })}
|
||||
badgeType="habits"
|
||||
todayCompletions={todayCompletions}
|
||||
settings={settings}
|
||||
setBrowserSettings={setBrowserSettings}
|
||||
/>
|
||||
|
||||
<div className="space-y-2">
|
||||
|
||||
Reference in New Issue
Block a user