Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 5x 29x 29x 29x 29x 29x 29x 29x 2x 1x 1x 27x 10x 6x 29x 29x 93x 93x 93x 93x 93x 93x 3x 3x 3x 93x 2x 93x 2x 93x 1x 1x 1x 93x 5x 5x 5x 4x 93x 36x 93x 36x 93x 1x 29x 29x 29x 2x 4x 2x 12x 1x 5x 1x 1x | import { useMutation, useQueryClient } from "@tanstack/react-query";
import {
AlertTriangle,
Bell,
BellOff,
Check,
Clock,
Plus,
} from "lucide-react";
import { useCallback, useState } from "react";
import { useLeadReminders } from "../../hooks/queries/useCrmQueries";
import { crmApi } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
import { cn } from "../../lib/utils";
import { Button } from "../ui/button";
import { SetReminderDialog } from "./SetReminderDialog";
// ─── Constants ───────────────────────────────────────────────────────────────
const SNOOZE_OPTIONS = [
{ label: "1 hour", seconds: 60 * 60 },
{ label: "3 hours", seconds: 3 * 60 * 60 },
{ label: "Tomorrow", seconds: 24 * 60 * 60 },
{ label: "3 days", seconds: 3 * 24 * 60 * 60 },
];
function formatReminderTime(dateStr: string): { label: string; full: string } {
const date = new Date(dateStr);
const now = Date.now();
const diff = date.getTime() - now;
const absDiff = Math.abs(diff);
const hours = Math.floor(absDiff / (1000 * 60 * 60));
const days = Math.floor(hours / 24);
let label: string;
if (diff < 0) {
// Overdue
Iif (hours < 1) label = "just now";
else if (hours < 24) label = `${hours}h overdue`;
else label = `${days}d overdue`;
} else {
if (hours < 1) label = "< 1h";
else if (hours < 24) label = `in ${hours}h`;
else if (days === 1) label = "tomorrow";
else Elabel = `in ${days}d`;
}
const full = date.toLocaleString("en-IN", {
day: "numeric",
month: "short",
year: "numeric",
hour: "numeric",
minute: "2-digit",
});
return { label, full };
}
// ─── Component ───────────────────────────────────────────────────────────────
type LeadRemindersProps = {
leadId: number;
proId: string;
/** When true, auto-open the create reminder dialog (controlled from parent) */
externalOpen?: boolean;
onExternalClose?: () => void;
};
export function LeadReminders({ leadId, proId, externalOpen, onExternalClose }: LeadRemindersProps) {
const queryClient = useQueryClient();
const { data: reminders } = useLeadReminders(proId, String(leadId));
const [showCreate, setShowCreate] = useState(false);
const isDialogOpen = showCreate || !!externalOpen;
const [snoozeTarget, setSnoozeTarget] = useState<number | null>(null);
const invalidate = useCallback(() => {
queryClient.invalidateQueries({
queryKey: queryKeys.crm.reminders(proId, String(leadId)),
});
queryClient.invalidateQueries({
queryKey: queryKeys.crm.reminderCounts(proId),
});
queryClient.invalidateQueries({
queryKey: queryKeys.crm.activities(proId, String(leadId)),
});
}, [queryClient, proId, leadId]);
const completeMutation = useMutation({
mutationFn: (reminderId: number) =>
crmApi.completeReminder(proId, reminderId),
onSuccess: invalidate,
});
const dismissMutation = useMutation({
mutationFn: (reminderId: number) =>
crmApi.dismissReminder(proId, reminderId),
onSuccess: invalidate,
});
const snoozeMutation = useMutation({
mutationFn: ({
reminderId,
dueAt,
}: {
reminderId: number;
dueAt: number;
}) => crmApi.snoozeReminder(proId, reminderId, dueAt),
onSuccess: () => {
invalidate();
setSnoozeTarget(null);
},
});
// Sort: overdue first, then upcoming by date, completed/dismissed last
const sorted = [...(reminders || [])].sort((a, b) => {
const order = { overdue: 0, upcoming: 1, completed: 2, dismissed: 3 };
const diff = order[a.status] - order[b.status];
if (diff !== 0) return diff;
return new Date(a.dueAt).getTime() - new Date(b.dueAt).getTime();
});
const active = sorted.filter(
(r) => r.status === "overdue" || r.status === "upcoming",
);
const inactive = sorted.filter(
(r) => r.status === "completed" || r.status === "dismissed",
);
return (
<div className="rounded-lg border border-border-default bg-background-elevated p-5">
<div className="flex items-start justify-between mb-4">
<div>
<h3 className="text-sm font-semibold text-foreground-default flex items-center gap-2">
<Bell className="h-4 w-4 text-foreground-subtle" />
Reminders
</h3>
{active.length > 0 && (
<span className="text-xs text-foreground-muted ml-6">
{active.length} active
</span>
)}
</div>
<Button
size="icon"
variant="outline"
className="h-7 w-7"
onClick={() => setShowCreate(true)}
title="Set Reminder"
>
<Plus className="h-3.5 w-3.5" />
</Button>
</div>
{/* Active reminders */}
{active.length === 0 && inactive.length === 0 ? (
<p className="text-sm text-foreground-subtle text-center py-6">
No reminders set
</p>
) : (
<div className="space-y-2">
{active.map((reminder) => {
const time = formatReminderTime(reminder.dueAt);
const isOverdue = reminder.status === "overdue";
return (
<div
key={reminder.id}
className={cn(
"relative flex items-start gap-3 p-2.5 rounded-md border transition-colors group",
isOverdue
? "border-error/30 bg-error/5"
: "border-border-default hover:bg-background-muted",
)}
>
{/* Status icon */}
<div
className={cn(
"flex h-6 w-6 items-center justify-center rounded-full flex-shrink-0 mt-0.5",
isOverdue
? "bg-error/10 text-error"
: "bg-info/10 text-info",
)}
>
{isOverdue ? (
<AlertTriangle className="h-3 w-3" />
) : (
<Clock className="h-3 w-3" />
)}
</div>
{/* Content */}
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-foreground-default">
{reminder.title}
</p>
{reminder.notes && (
<p className="text-xs text-foreground-subtle mt-0.5 truncate">
{reminder.notes}
</p>
)}
<span
className={cn(
"text-xs",
isOverdue
? "text-error font-medium"
: "text-foreground-subtle",
)}
title={time.full}
>
{time.label}
</span>
</div>
{/* Actions */}
<div className="flex items-center gap-1 flex-shrink-0">
<button
type="button"
onClick={() => completeMutation.mutate(reminder.id)}
disabled={completeMutation.isPending || dismissMutation.isPending}
className="p-1.5 text-foreground-subtle hover:text-success transition-colors disabled:opacity-40"
title="Complete"
>
<Check className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() =>
setSnoozeTarget(
snoozeTarget === reminder.id ? null : reminder.id,
)
}
disabled={completeMutation.isPending || dismissMutation.isPending}
className="p-1.5 text-foreground-subtle hover:text-warning transition-colors disabled:opacity-40"
title="Snooze"
>
<Clock className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() => dismissMutation.mutate(reminder.id)}
disabled={completeMutation.isPending || dismissMutation.isPending}
className="p-1.5 text-foreground-subtle hover:text-foreground-muted transition-colors disabled:opacity-40"
title="Dismiss"
>
<BellOff className="h-3.5 w-3.5" />
</button>
</div>
{/* Snooze options dropdown */}
{snoozeTarget === reminder.id && (
<div className="absolute right-2 bottom-full mb-1 z-10 bg-background-elevated border border-border-default rounded-md shadow-md py-1 min-w-[120px]">
{SNOOZE_OPTIONS.map((opt) => (
<button
key={opt.label}
type="button"
onClick={() =>
snoozeMutation.mutate({
reminderId: reminder.id,
dueAt: Math.floor(Date.now() / 1000) + opt.seconds,
})
}
className="w-full text-left px-3 py-1.5 text-sm text-foreground-default hover:bg-background-muted"
>
{opt.label}
</button>
))}
</div>
)}
</div>
);
})}
{/* Completed / dismissed (dimmed) */}
{inactive.length > 0 && (
<div className="pt-2 border-t border-border-default mt-3">
{inactive.slice(0, 3).map((reminder) => (
<div
key={reminder.id}
className="flex items-center gap-2 py-1.5 text-foreground-subtle"
>
{reminder.status === "completed" ? (
<Check className="h-3 w-3 text-success" />
) : (
<BellOff className="h-3 w-3" />
)}
<span className="text-xs line-through truncate">
{reminder.title}
</span>
</div>
))}
{inactive.length > 3 && (
<p className="text-xs text-foreground-subtle pl-5">
+{inactive.length - 3} more
</p>
)}
</div>
)}
</div>
)}
{/* Create reminder dialog */}
<SetReminderDialog
proId={proId}
leadId={leadId}
open={isDialogOpen}
onClose={() => {
setShowCreate(false);
onExternalClose?.();
}}
/>
</div>
);
}
|