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 | 1x 1x 1x 1x 1x 1x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 290x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 1x 1x 1x 1x 58x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 1x 1x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 1x | import { NewsletterWithRelations } from '@common/types';
import { useLogger } from '@common/utils/logger/useLogger';
import clsx from 'clsx';
import {
Archive,
ArchiveX,
BookmarkIcon,
Eye,
EyeOff,
Heart,
MoreHorizontal,
Trash,
} from 'lucide-react';
import React, { useState } from 'react';
interface NewsletterActionsProps {
newsletter: NewsletterWithRelations;
onToggleLike: (newsletter: NewsletterWithRelations) => Promise<void>;
onToggleArchive: (id: string) => Promise<void>;
onToggleRead: (id: string) => Promise<void>;
onTrash: (id: string) => void;
onToggleQueue?: (newsletterId: string) => Promise<void>;
_onToggleTagVisibility?: (e: React.MouseEvent) => void;
loadingStates?: Record<string, string>;
_errorTogglingLike?: Error | null;
isInReadingQueue?: boolean;
compact?: boolean;
}
// Action Button Component
const ActionButton: React.FC<{
onClick: () => void;
disabled?: boolean;
isLoading?: boolean;
_isActive?: boolean;
icon: React.ReactNode;
label: string;
variant?: 'primary' | 'secondary' | 'danger';
size?: 'sm' | 'md';
className?: string;
}> = ({
onClick,
disabled = false,
isLoading = false,
_isActive = false,
icon,
label,
variant = 'secondary',
size = 'md',
className,
}) => {
const baseClasses = `
inline-flex items-center justify-center rounded-lg font-medium transition-all duration-200
focus:outline-none focus:ring-2 focus:ring-offset-1 disabled:opacity-50 disabled:cursor-not-allowed
${size === 'sm' ? 'px-2 py-1 text-xs' : 'px-2.5 py-1.5 text-sm'}
`;
const variantClasses = {
primary: 'text-gray-700 dark:text-slate-100 hover:bg-gray-200 dark:hover:bg-neutral-700/80 focus:ring-gray-500',
secondary: 'text-gray-700 dark:text-slate-100 hover:bg-gray-200 dark:hover:bg-neutral-700/80 focus:ring-gray-500',
danger: 'text-gray-700 dark:text-red-300 hover:bg-gray-200 dark:hover:bg-red-900/30 focus:ring-gray-500',
};
const finalClasses = className || variantClasses[variant];
return (
<button
onClick={onClick}
disabled={disabled || isLoading}
className={`${baseClasses} ${finalClasses}`}
aria-label={label}
title={label}
>
{isLoading ? (
<div
className={`animate-spin rounded-full border-2 border-current border-t-transparent ${size === 'sm' ? 'w-3 h-3' : 'w-4 h-4'}`}
/>
) : (
<span className={`${size === 'sm' ? 'w-3 h-3' : 'w-4 h-4'}`}>{icon}</span>
)}
</button>
);
};
const NewsletterActions: React.FC<NewsletterActionsProps> = ({
newsletter,
onToggleLike,
onToggleArchive,
onToggleRead,
onTrash,
onToggleQueue,
_onToggleTagVisibility,
loadingStates = {},
_errorTogglingLike,
isInReadingQueue = false,
compact = false,
}) => {
const log = useLogger('NewsletterActions');
const [showMoreMenu, setShowMoreMenu] = useState(false);
const handleToggleRead = async () => {
try {
await onToggleRead(newsletter.id);
} catch (error) {
log.error(
'Failed to toggle read status',
{ newsletterId: newsletter.id },
error instanceof Error ? error : new Error(String(error))
);
}
};
const handleToggleLike = async () => {
try {
await onToggleLike(newsletter);
} catch (error) {
log.error(
'Failed to toggle like',
{ newsletterId: newsletter.id },
error instanceof Error ? error : new Error(String(error))
);
}
};
const handleToggleArchive = async () => {
try {
await onToggleArchive(newsletter.id);
} catch (error) {
log.error(
'Failed to toggle archive',
{ newsletterId: newsletter.id },
error instanceof Error ? error : new Error(String(error))
);
}
};
const handleToggleQueue = async () => {
if (!onToggleQueue) return;
try {
await onToggleQueue(newsletter.id);
} catch (error) {
log.error(
'Failed to toggle queue',
{ newsletterId: newsletter.id },
error instanceof Error ? error : new Error(String(error))
);
}
};
const handleTrash = () => {
onTrash(newsletter.id);
};
const isRead = newsletter.is_read;
const isLiked = newsletter.is_liked;
const isArchived = newsletter.is_archived;
const isLoading = Object.keys(loadingStates).length > 0;
const size = compact ? 'sm' : 'md';
// On mobile, show archive, queue, like as primary actions; move read and tag to more menu
const primaryActions = [
...(onToggleQueue
? [
{
key: 'queue',
action: handleToggleQueue,
icon: (
<BookmarkIcon
size={14}
className={isInReadingQueue ? 'fill-blue-500 text-blue-500' : 'text-gray-700 dark:text-slate-200'}
/>
),
label: isInReadingQueue ? 'Remove from queue' : 'Add to queue',
variant: 'secondary' as const,
_isActive: isInReadingQueue,
isLoading: loadingStates[newsletter.id] === 'queue',
},
]
: []),
{
key: 'archive',
action: handleToggleArchive,
icon: isArchived ? <ArchiveX size={14} /> : <Archive size={14} />,
label: isArchived ? 'Unarchive' : 'Archive',
variant: 'secondary' as const,
isLoading: loadingStates[newsletter.id] === 'archive',
className: isArchived ? 'text-green-600 dark:text-green-400 hover:bg-green-100 dark:hover:bg-green-900/30' : undefined,
},
{
key: 'like',
action: handleToggleLike,
icon: <Heart size={14} className={isLiked ? 'fill-red-500 text-red-500' : 'text-gray-700 dark:text-slate-200'} />,
label: isLiked ? 'Unlike' : 'Like',
variant: 'secondary' as const,
_isActive: isLiked,
isLoading: loadingStates[newsletter.id] === 'like',
},
];
// In secondaryActions, ensure all actions are () => void
const secondaryActions = [
{
key: 'read',
action: () => {
handleToggleRead();
},
icon: isRead ? <EyeOff size={14} className="text-gray-700 dark:text-slate-200" /> : <Eye size={14} className="text-gray-700 dark:text-slate-200" />,
label: isRead ? 'Mark as unread' : 'Mark as read',
variant: 'primary' as const,
isLoading: loadingStates[newsletter.id] === 'read',
className: undefined,
},
// Remove tags action from the more menu
// Only show trash icon for archived newsletters
...(isArchived
? [
{
key: 'trash',
action: handleTrash,
icon: <Trash size={14} />,
label: 'Delete',
variant: 'danger' as const,
isLoading: loadingStates[newsletter.id] === 'trash',
className: 'text-red-600 dark:text-red-300 hover:bg-red-100 dark:hover:bg-red-900/30',
},
]
: []),
];
return (
<div className="flex items-center gap-1">
{/* Primary Actions - Always visible */}
{primaryActions.map((action) => (
<ActionButton
key={action.key}
onClick={action.action}
disabled={isLoading}
isLoading={action.isLoading}
_isActive={action._isActive}
icon={action.icon}
label={action.label}
variant={action.variant}
size={size}
className={action.className}
/>
))}
{/* Secondary Actions - Show on desktop, hide in more menu on mobile */}
<div className="hidden sm:flex items-center gap-1">
{secondaryActions.map((action) => (
<ActionButton
key={action.key}
onClick={action.action}
disabled={isLoading}
isLoading={action.isLoading}
icon={action.icon}
label={action.label}
variant={action.variant}
size={size}
className={action.className}
/>
))}
</div>
{/* More Menu for Mobile */}
<div className="sm:hidden relative">
<ActionButton
onClick={() => setShowMoreMenu(!showMoreMenu)}
disabled={isLoading}
icon={<MoreHorizontal size={14} />}
label="More actions"
variant="secondary"
size={size}
/>
{showMoreMenu && (
<>
{/* Backdrop */}
<div className="fixed inset-0 z-[100]" onClick={() => setShowMoreMenu(false)} />
{/* Dropdown Menu */}
<div className="absolute right-0 top-full mt-1 bg-white rounded-lg shadow-lg border border-gray-200 z-[110] py-1">
{secondaryActions.map((action) => (
<button
key={action.key}
onClick={() => {
action.action();
setShowMoreMenu(false);
}}
disabled={isLoading || action.isLoading}
className={clsx(
'flex items-center justify-center p-2 hover:bg-gray-100 transition-colors',
'w-10 h-10 rounded-lg',
{ 'opacity-50 cursor-not-allowed': isLoading || action.isLoading },
action.className
)}
title={action.label}
aria-label={action.label}
>
{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>
)}
</button>
))}
</div>
</>
)}
</div>
</div>
);
};
export default NewsletterActions;
|