Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions frontend/src/app/_components.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it, vi } from 'vitest';

/**
* 回归 #208:首页内容卡片的「更多反馈」下拉菜单被卡片裁掉。
*
* 菜单是绝对定位(absolute)浮层,挂在卡片最后一行按钮组的下方。
* 卡片容器一旦带 overflow-hidden,超出卡片边界的菜单就会被裁掉,
* 表现为「只剩贴着卡片下边缘的一条白边」,五个选项一个都点不到。
*
* 这里锁两件事:
* 1. 承载下拉菜单的卡片不得裁剪溢出(overflow-hidden)。
* 2. 菜单本身完整渲染五个反馈选项。
*/

vi.mock('next/navigation', () => ({
useRouter: () => ({ push: vi.fn() }),
}));

vi.mock('@/components/ClientLayout', () => ({
useAppContext: () => ({ currentUser: null, openReader: vi.fn() }),
}));

import { EditorialItem, FeedbackMenu } from './_components';
import type { ContentItem } from '@/types';

const item = {
id: 42,
title: '测试选题',
source_id: 7,
source_name: 'Hacker News Front Page',
source_type: 'RSS',
published_at: '2026-11-01T10:00:00Z',
crawled_at: '2026-11-01T10:00:00Z',
category: '科技',
url: 'https://example.com/a',
} as ContentItem;

function cardHtml() {
return renderToStaticMarkup(
React.createElement(EditorialItem, {
item,
isFav: false,
onToggleFav: vi.fn(),
onIgnore: vi.fn(),
time: '8 分钟前',
timeLabel: '',
onShowAnalysis: vi.fn(),
onStartWorkflow: vi.fn(),
workflowPending: false,
}),
);
}

describe('内容卡片的反馈菜单', () => {
it('卡片容器不裁剪溢出,否则绝对定位的下拉菜单会被切掉', () => {
const html = cardHtml();
// 卡片根节点是 <section id="topic-item-…">
const cardTag = html.match(/<section[^>]*id="topic-item-42"[^>]*>/)?.[0] ?? '';
expect(cardTag).toBeTruthy();
expect(cardTag).not.toMatch(/overflow-(hidden|clip)/);
});

it('下拉菜单完整列出五个反馈选项', () => {
const html = renderToStaticMarkup(
React.createElement(FeedbackMenu, {
activeFeedback: null,
pending: false,
onSelect: vi.fn(),
}),
);
for (const label of ['精选好文', '有价值', '不感兴趣', '不相关', '过时了']) {
expect(html).toContain(label);
}
});

it('菜单是绝对定位浮层,脱离文档流展开在按钮下方', () => {
const html = renderToStaticMarkup(
React.createElement(FeedbackMenu, {
activeFeedback: null,
pending: false,
onSelect: vi.fn(),
}),
);
expect(html).toContain('absolute');
expect(html).toContain('top-full');
});
});
80 changes: 54 additions & 26 deletions frontend/src/app/_components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ export function EditorialItem({
id={`topic-item-${item.id}`}
onClick={handleCardClick}
className={cx(
'group flex-1 overflow-hidden shadow-sm transition hover:border-primary-border hover:shadow-lg',
// 刻意不加 overflow-hidden:反馈菜单是绝对定位在按钮下方的浮层,
// 裁剪会让它只剩贴着卡片下边缘的一条白边(#208)。
'group flex-1 shadow-sm transition hover:border-primary-border hover:shadow-lg',
compact ? 'px-[18px] py-3.5' : 'px-[22px] py-[18px]',
item.url ? 'cursor-pointer' : 'cursor-default',
)}
Expand Down Expand Up @@ -441,6 +443,49 @@ const FEEDBACK_OPTIONS: { type: FeedbackType; icon: LucideIcon; label: string; c
{ type: 'outdated', icon: Clock3, label: '过时了', color: '#d97706' },
];

/**
* 「更多反馈」下拉菜单本体。
*
* 独立成组件是为了能单独渲染断言(见 _components.test.tsx):
* 菜单是绝对定位浮层,宿主卡片必须允许溢出,否则它会被裁成一条白边(#208)。
*/
export function FeedbackMenu({
activeFeedback,
pending,
onSelect,
}: {
activeFeedback: FeedbackType | null;
pending: boolean;
onSelect: (type: FeedbackType) => void;
}) {
return (
<div
onClick={(e) => e.stopPropagation()}
className="absolute right-0 top-full z-[100] mt-1 min-w-[120px] rounded-sm border border-gray-200 bg-white p-1 shadow-lg"
>
{FEEDBACK_OPTIONS.map(({ type, icon: Icon, label, color }) => (
<button
key={type}
type="button"
disabled={pending}
onClick={(e) => {
e.stopPropagation();
onSelect(type);
}}
className={cx(
'flex w-full items-center gap-2 rounded border-0 px-2.5 py-1.5 text-left text-xs',
activeFeedback === type ? 'cursor-default font-semibold' : 'cursor-pointer font-normal hover:bg-gray-50',
)}
style={{ color: activeFeedback === type ? color : '#4B5563', background: activeFeedback === type ? `${color}10` : 'transparent' }}
>
<Icon size={13} strokeWidth={2.2} />
<span>{label}</span>
</button>
))}
</div>
);
}

export function FeedbackButtons({ contentId }: { contentId: number }) {
const { currentUser } = useAppContext();
const router = useRouter();
Expand Down Expand Up @@ -507,31 +552,14 @@ export function FeedbackButtons({ contentId }: { contentId: number }) {
<ChevronDown size={13} strokeWidth={2.2} />
</button>
{showMore && (
<div
onClick={(e) => e.stopPropagation()}
className="absolute right-0 top-full z-[100] mt-1 min-w-[120px] rounded-sm border border-gray-200 bg-white p-1 shadow-lg"
>
{FEEDBACK_OPTIONS.map(({ type, icon: Icon, label, color }) => (
<button
key={type}
type="button"
disabled={pending}
onClick={(e) => {
e.stopPropagation();
handleFeedback(type);
setShowMore(false);
}}
className={cx(
'flex w-full items-center gap-2 rounded border-0 px-2.5 py-1.5 text-left text-xs',
activeFeedback === type ? 'cursor-default font-semibold' : 'cursor-pointer font-normal hover:bg-gray-50',
)}
style={{ color: activeFeedback === type ? color : '#4B5563', background: activeFeedback === type ? `${color}10` : 'transparent' }}
>
<Icon size={13} strokeWidth={2.2} />
<span>{label}</span>
</button>
))}
</div>
<FeedbackMenu
activeFeedback={activeFeedback}
pending={pending}
onSelect={(type) => {
handleFeedback(type);
setShowMore(false);
}}
/>
)}
</div>
</div>
Expand Down
Loading