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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 340x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 28x 28x 42x 42x 28x 3x 3x 3x 3x 42x 42x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 40x 40x 40x 40x 40x 40x 40x 40x 40x 42x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 42x 42x 42x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 42x 42x 42x 42x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 42x 42x 42x 42x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 42x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 42x 42x 1x | import { useReadingQueue } from '@common/hooks/useReadingQueue';
import type { NewsletterMutations } from '@common/hooks/useSharedNewsletterActions';
import { useSharedNewsletterActions } from '@common/hooks/useSharedNewsletterActions';
import type { NewsletterWithRelations } from '@common/types';
import { useLogger } from '@common/utils/logger/useLogger';
import { Archive, ArchiveX, Bookmark as BookmarkIcon, Eye, EyeOff, Heart, MoreHorizontal, Trash2 as TrashIcon } from 'lucide-react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { toast } from 'react-hot-toast';
import { useNavigate } from 'react-router-dom';
interface NewsletterDetailActionsProps {
newsletter: NewsletterWithRelations;
onNewsletterUpdate: (updatedNewsletter: NewsletterWithRelations) => void;
isFromReadingQueue?: boolean;
mutations?: NewsletterMutations;
}
// Action Button Component
const DetailActionButton: React.FC<{
onClick: () => void;
disabled?: boolean;
className: string;
ariaLabel: string;
icon?: React.ReactNode;
text: string;
isLoading?: boolean;
variant?: "primary" | "secondary" | "danger";
dataTestId?: string;
}> = ({ onClick, disabled, className, ariaLabel, icon, text, isLoading, variant = "secondary", dataTestId }) => (
<button
onClick={onClick}
disabled={disabled || isLoading}
className={`
flex items-center justify-center px-3 py-2 rounded-lg text-sm font-medium
transition-all duration-200 disabled:opacity-60 disabled:cursor-not-allowed
focus:outline-none focus:ring-2 focus:ring-offset-1
${className}
${variant === "primary" ? "focus:ring-blue-500" : variant === "danger" ? "focus:ring-red-500" : "focus:ring-gray-500"}
`}
aria-label={ariaLabel}
{...(ariaLabel === 'Mark as read' ? { 'data-testid': 'mark-as-read-btn' } : {})}
{...(dataTestId ? { 'data-testid': dataTestId } : {})}
>
{isLoading && (
<div className="w-4 h-4 mr-2 border-2 border-current border-t-transparent rounded-full animate-spin" />
)}
{/* Desktop: icon + text */}
<span className="hidden sm:inline-flex items-center">
{icon && !isLoading && <span className="mr-2">{icon}</span>}
{text}
</span>
{/* Mobile: icon only */}
<span className="inline-flex sm:hidden items-center">
{icon && !isLoading && icon}
</span>
</button>
);
export const NewsletterDetailActions: React.FC<NewsletterDetailActionsProps> = ({
newsletter,
onNewsletterUpdate,
isFromReadingQueue = false,
mutations,
}) => {
const log = useLogger();
const navigate = useNavigate();
const { isInQueue: _checkIsInQueue } = useReadingQueue();
const {
handleMarkAsRead, handleMarkAsUnread, handleToggleLike, handleToggleArchive,
handleDeleteNewsletter, handleToggleInQueue, isMarkingAsRead, isMarkingAsUnread,
isDeletingNewsletter,
} = useSharedNewsletterActions(
mutations,
{
showToasts: false,
optimisticUpdates: false,
onSuccess: (updatedNl) => { if (updatedNl) onNewsletterUpdate(updatedNl); },
onError: (error) => log.error('Newsletter action failed', { action: 'newsletter_action', metadata: { newsletterId: newsletter.id } }, error),
}
);
const [localNewsletter, setLocalNewsletter] = useState<NewsletterWithRelations>(newsletter);
const [isLiking, setIsLiking] = useState(false);
const [isTogglingQueue, setIsTogglingQueue] = useState(false);
const [isArchiving, setIsArchiving] = useState(false);
const [isTogglingReadStatus, setIsTogglingReadStatus] = useState(false);
const [showMoreMenu, setShowMoreMenu] = useState(false);
const [isInQueue, setIsInQueue] = useState<boolean>(isFromReadingQueue);
const [isCheckingQueue, _setIsCheckingQueue] = useState(false);
const hasCheckedQueue = useRef<{ [key: string]: boolean }>({});
useEffect(() => {
setLocalNewsletter(newsletter);
if (newsletter?.id !== localNewsletter?.id) {
const currentId = newsletter?.id;
hasCheckedQueue.current = currentId ? { [currentId]: hasCheckedQueue.current[currentId] || false } : {};
}
}, [newsletter, localNewsletter?.id]);
useEffect(() => {
// If coming from reading queue, we know it's in queue - no need to check
if (newsletter?.id && isFromReadingQueue) {
setIsInQueue(true);
hasCheckedQueue.current[newsletter.id] = true;
return;
}
// Queue checking temporarily disabled to prevent navigation loop
// TODO: Re-enable once navigation loop is fixed
}, [newsletter?.id, isFromReadingQueue]);
const handleToggleReadStatus = useCallback(async () => {
if (!localNewsletter?.id || isTogglingReadStatus) return;
setIsTogglingReadStatus(true);
const optimisticNewsletter = { ...localNewsletter, is_read: !localNewsletter.is_read };
setLocalNewsletter(optimisticNewsletter);
onNewsletterUpdate(optimisticNewsletter);
try {
if (localNewsletter.is_read) {
await handleMarkAsUnread(localNewsletter.id);
} else {
await handleMarkAsRead(localNewsletter.id);
}
} catch (error) {
log.error('Failed to toggle read status', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
setLocalNewsletter(newsletter);
onNewsletterUpdate(newsletter);
toast.error('Failed to update read status');
} finally {
setIsTogglingReadStatus(false);
}
}, [localNewsletter, isTogglingReadStatus, handleMarkAsRead, handleMarkAsUnread, onNewsletterUpdate, newsletter, log]);
const handleToggleLikeAction = useCallback(async () => {
if (!localNewsletter?.id || isLiking) return;
setIsLiking(true);
try {
await handleToggleLike(localNewsletter, {
optimisticUpdates: false,
showToasts: true,
onSuccess: (updatedNewsletter) => {
if (updatedNewsletter) {
setLocalNewsletter(updatedNewsletter);
onNewsletterUpdate(updatedNewsletter);
}
},
});
} catch (error) {
log.error('Failed to update like status', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
toast.error('Failed to update like status');
} finally {
setIsLiking(false);
}
}, [localNewsletter, isLiking, handleToggleLike, newsletter, onNewsletterUpdate, log]);
const handleToggleQueue = useCallback(async () => {
if (!localNewsletter?.id || isTogglingQueue) return;
setIsTogglingQueue(true);
const previousQueueStatus = isInQueue;
const newQueueStatus = !isInQueue;
setIsInQueue(newQueueStatus);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
try {
await handleToggleInQueue(localNewsletter, previousQueueStatus);
clearTimeout(timeoutId);
} catch (error) {
if (controller.signal.aborted) {
log.error('Toggle queue operation timed out', { metadata: { newsletterId: newsletter.id } }, new Error('Operation timed out'));
toast.error('Operation timed out. Please try again.');
} else {
log.error('Failed to toggle reading queue', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
toast.error('Failed to update reading queue');
}
setIsInQueue(previousQueueStatus);
} finally {
clearTimeout(timeoutId);
setIsTogglingQueue(false);
}
}, [localNewsletter, isTogglingQueue, isInQueue, handleToggleInQueue, newsletter.id, log]);
const handleArchive = useCallback(async () => {
if (!localNewsletter?.id || isArchiving || localNewsletter.is_archived) return;
setIsArchiving(true);
const optimisticNewsletter = { ...localNewsletter, is_archived: true };
setLocalNewsletter(optimisticNewsletter);
onNewsletterUpdate(optimisticNewsletter);
try {
await handleToggleArchive(localNewsletter);
} catch (error) {
log.error('Failed to archive newsletter', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
setLocalNewsletter(newsletter);
onNewsletterUpdate(newsletter);
toast.error('Failed to archive newsletter');
} finally {
setIsArchiving(false);
}
}, [localNewsletter, isArchiving, handleToggleArchive, onNewsletterUpdate, newsletter, log]);
const handleUnarchive = useCallback(async () => {
if (!localNewsletter?.id || isArchiving || !localNewsletter.is_archived) return;
setIsArchiving(true);
const optimisticNewsletter = { ...localNewsletter, is_archived: false };
setLocalNewsletter(optimisticNewsletter);
onNewsletterUpdate(optimisticNewsletter);
try {
await handleToggleArchive(localNewsletter);
} catch (error) {
log.error('Failed to unarchive newsletter', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
setLocalNewsletter(newsletter);
onNewsletterUpdate(newsletter);
toast.error('Failed to unarchive newsletter');
} finally {
setIsArchiving(false);
}
}, [localNewsletter, isArchiving, handleToggleArchive, onNewsletterUpdate, newsletter, log]);
const handleTrash = useCallback(async () => {
if (!localNewsletter?.id) return;
if (!window.confirm('Are you sure? This action is final and cannot be undone.')) return;
try {
await handleDeleteNewsletter(localNewsletter.id);
if (isFromReadingQueue) {
navigate('/reading-queue');
} else {
navigate('/inbox?filter=archived');
}
} catch (error) {
log.error('Failed to delete newsletter', { metadata: { newsletterId: newsletter.id } }, error instanceof Error ? error : new Error(String(error)));
toast.error('Failed to delete newsletter');
}
}, [localNewsletter?.id, handleDeleteNewsletter, isFromReadingQueue, log, newsletter.id, navigate]);
// Primary actions (always visible)
const primaryActions = [
{
key: "read",
onClick: handleToggleReadStatus,
disabled: isTogglingReadStatus || isMarkingAsRead || isMarkingAsUnread,
isLoading: isTogglingReadStatus || isMarkingAsRead || isMarkingAsUnread,
className: localNewsletter?.is_read ? 'bg-blue-100 text-blue-700 hover:bg-blue-200' : 'bg-purple-100 text-purple-700 hover:bg-purple-200',
ariaLabel: localNewsletter?.is_read ? 'Mark as unread' : 'Mark as read',
icon: localNewsletter?.is_read ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />,
text: localNewsletter?.is_read ? 'Mark Unread' : 'Mark Read',
variant: "primary" as const,
},
{
key: "like",
onClick: handleToggleLikeAction,
disabled: isLiking,
isLoading: isLiking,
className: localNewsletter?.is_liked ? 'bg-red-100 text-red-600 hover:bg-red-200' : 'bg-rose-50 text-rose-600 hover:bg-rose-100',
ariaLabel: localNewsletter?.is_liked ? 'Unlike' : 'Like',
icon: <Heart className={`w-4 h-4 ${localNewsletter?.is_liked ? 'fill-red-500' : 'fill-none'}`} stroke="currentColor" />,
text: localNewsletter?.is_liked ? 'Liked' : 'Like',
variant: "secondary" as const,
},
{
key: "queue",
onClick: handleToggleQueue,
disabled: isTogglingQueue || isCheckingQueue,
isLoading: isTogglingQueue || isCheckingQueue,
className: isInQueue ? 'bg-yellow-100 text-yellow-700 hover:bg-yellow-200' : 'bg-emerald-50 text-emerald-600 hover:bg-emerald-100',
ariaLabel: isInQueue ? 'Remove from queue' : 'Add to queue',
icon: <BookmarkIcon className={`w-4 h-4 ${isInQueue ? 'fill-yellow-500' : 'fill-none'}`} stroke="currentColor" />,
text: isCheckingQueue ? 'Checking...' : isInQueue ? 'Saved' : 'Save',
variant: "secondary" as const,
dataTestId: isInQueue ? 'remove-from-queue-btn' : 'add-to-queue-btn',
},
...(!localNewsletter?.is_archived ? [{
key: "archive",
onClick: handleArchive,
disabled: isArchiving,
isLoading: isArchiving,
className: "bg-amber-100 text-amber-700 hover:bg-amber-200",
ariaLabel: "Archive newsletter",
icon: <Archive className="w-4 h-4" />,
text: "Archive",
variant: "secondary" as const,
}] : [{
key: "unarchive",
onClick: handleUnarchive,
disabled: isArchiving,
isLoading: isArchiving,
className: "bg-blue-100 text-blue-700 hover:bg-blue-200",
ariaLabel: "Unarchive newsletter",
icon: <ArchiveX className="w-4 h-4" />,
text: "Unarchive",
variant: "secondary" as const,
}]),
];
// Secondary actions (only destructive actions for More menu on mobile)
const secondaryActions = [
...((localNewsletter?.is_archived) ? [{
key: "trash",
onClick: handleTrash,
disabled: isDeletingNewsletter,
isLoading: isDeletingNewsletter,
className: "bg-red-100 text-red-700 hover:bg-red-200",
ariaLabel: "Delete newsletter permanently",
icon: <TrashIcon className="w-4 h-4" />,
text: "Delete",
variant: "danger" as const,
}] : []),
];
return (
<div className="flex flex-wrap items-center gap-2">
{/* Primary Actions - Desktop */}
<div className="hidden sm:flex items-center gap-2">
{primaryActions.map((action) => (
<DetailActionButton
key={action.key}
onClick={action.onClick}
disabled={action.disabled}
isLoading={action.isLoading}
className={action.className}
ariaLabel={action.ariaLabel}
icon={action.icon}
text={action.text}
variant={action.variant}
dataTestId={action.dataTestId}
/>
))}
</div>
{/* Primary Actions - Mobile */}
<div className="flex sm:hidden items-center gap-2">
{primaryActions.map((action) => (
<DetailActionButton
key={action.key}
onClick={action.onClick}
disabled={action.disabled}
isLoading={action.isLoading}
className={action.className}
ariaLabel={action.ariaLabel}
icon={action.icon}
text={action.text}
variant={action.variant}
dataTestId={action.dataTestId}
/>
))}
</div>
{/* Secondary Actions - Show on desktop, hide in more menu on mobile */}
<div className="hidden sm:flex items-center gap-2">
{secondaryActions.map((action) => (
<DetailActionButton
key={action.key}
onClick={action.onClick}
disabled={action.disabled}
isLoading={action.isLoading}
className={action.className}
ariaLabel={action.ariaLabel}
icon={action.icon}
text={action.text}
variant={action.variant}
/>
))}
</div>
{/* More Menu for Mobile */}
{secondaryActions.length > 0 && (
<div className="sm:hidden relative">
<DetailActionButton
onClick={() => setShowMoreMenu(!showMoreMenu)}
disabled={false}
className="bg-gray-100 text-gray-700 hover:bg-gray-200"
ariaLabel="More actions"
icon={<MoreHorizontal className="w-4 h-4" />}
text="More"
variant="secondary"
/>
{showMoreMenu && (
<>
{/* Backdrop */}
<div
className="fixed inset-0 z-40"
onClick={() => setShowMoreMenu(false)}
/>
{/* Dropdown Menu */}
<div className="absolute right-0 top-full mt-2 w-48 bg-white rounded-lg shadow-lg border border-gray-200 z-50 py-1">
{secondaryActions.map((action) => (
<button
key={action.key}
onClick={() => {
action.onClick();
setShowMoreMenu(false);
}}
disabled={action.disabled}
className={`
w-full text-left px-3 py-2 text-sm flex items-center gap-2
hover:bg-gray-50 transition-colors disabled:opacity-50
${action.className.replace('hover:', '')}
`}
>
{action.isLoading ? (
<div className="w-4 h-4 animate-spin rounded-full border-2 border-current border-t-transparent" />
) : (
<span className="w-4 h-4">{action.icon}</span>
)}
{action.text}
</button>
))}
</div>
</>
)}
</div>
)}
</div>
);
};
export default NewsletterDetailActions;
|