feat(monitor): 作品标签 —— 词表在设置里维护,作品上贴
和「作品备注」刻意并存,不是一回事:备注是一句话的自由文字,标签是**从一份固定
词表里选的分类**。分类必须封闭,否则「重点 / 重要 / 优先」各写各的,筛就没法用了。
* `monitor_tag`:词表,全局一套(不按平台分)—— 「重点」是给人自己用的心智,不该在
小红书和抖音各定义一遍。颜色存的是**调色板里的名字**,不是色值:Tailwind 的类名是
静态提取的,拼出来的 `text-${color}` 它看不见,线上会静默变无色。
* `monitor_note_tag`:作品↔标签,**多对多**。一条作品可以既是重点又是竞品;只能贴一个
的话人就会跑去备注里写自由文字,这份词表就白建了。全部替换式提交,不是逐个增删。
* 删除标签时**显式删关联**,不靠外键级联 —— 测试跑 SQLite,它默认不开外键约束。
* 组头上的筛选做成平铺开关片,多选是「或」。
顺带:/monitor/notes 现在支持按 tag_id 筛。
This commit is contained in:
@@ -13,6 +13,7 @@ import { UnwiredPlatformNotice } from '@/components/layout/UnwiredPlatformNotice
|
||||
import { CommentsFeed } from './CommentsFeed'
|
||||
import { EventsFeed } from './EventsFeed'
|
||||
import { NotesTable } from './NotesTable'
|
||||
import { TagFilter } from './TagFilter'
|
||||
import { RunHistory } from './RunHistory'
|
||||
import { StatTile } from './StatTile'
|
||||
import { TaskCard } from './TaskCard'
|
||||
@@ -32,6 +33,10 @@ export function MonitorDashboard() {
|
||||
const [editorOpen, setEditorOpen] = useState(false)
|
||||
const [editingTask, setEditingTask] = useState<MonitorTask | null>(null)
|
||||
const [onlyNew, setOnlyNew] = useState(false)
|
||||
// 选中的标签。空数组 = 不筛。
|
||||
// 换任务时**刻意不清**:选中了哪几个标签在筛选条上是看得见的,空列表不会没头没脑;
|
||||
// 而切个任务就把筛选悄悄清掉,反而会让人以为筛选没生效。
|
||||
const [tagIds, setTagIds] = useState<number[]>([])
|
||||
|
||||
// Fall back to the first task so the detail panes are never empty for no
|
||||
// reason; keep an explicit selection once the user makes one.
|
||||
@@ -170,6 +175,7 @@ export function MonitorDashboard() {
|
||||
{/* 导出的是**这个任务**的全部作品,不是屏幕上这 200 条 —— 屏幕上那份是
|
||||
为了好看才截断的,导出跟着截断就成了「导出来的比看到的少」。
|
||||
走 window.open 而不是 blob:鉴权在 Cookie 上,浏览器自己会带上。 */}
|
||||
<TagFilter selected={tagIds} onChange={setTagIds} />
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
@@ -185,7 +191,7 @@ export function MonitorDashboard() {
|
||||
导出 CSV
|
||||
</Button>
|
||||
</div>
|
||||
<NotesTable taskId={selectedTask.id} onlyNew={onlyNew} />
|
||||
<NotesTable taskId={selectedTask.id} onlyNew={onlyNew} tagIds={tagIds} />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="comments" className="flex-1 overflow-auto terminal-scroll px-3 pb-3 mt-2">
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Tag } from 'lucide-react'
|
||||
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { useMonitorTags, useSetNoteTags } from '@/hooks/useMonitor'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { tagColorClass } from '@/lib/tagColors'
|
||||
import type { TagRef } from '@/types/monitor'
|
||||
|
||||
interface NoteTagPickerProps {
|
||||
noteId: string
|
||||
noteTitle: string
|
||||
/** 这条作品现在贴着的标签,用来给复选框定初值。 */
|
||||
current: TagRef[]
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* 给一条作品贴标签。
|
||||
*
|
||||
* 用对话框而不是下拉气泡:这是个**多选**,几个复选框摆开比塞进一个小浮层清楚得多,
|
||||
* 也不用为此引一个新的依赖。
|
||||
*
|
||||
* 点「保存」是一次**全量替换**(这条作品现在该有哪些标签),不是逐个增删 —— 复选框
|
||||
* 表达的就是一个集合,对齐差集只是徒增错处。
|
||||
*/
|
||||
export function NoteTagPicker({
|
||||
noteId,
|
||||
noteTitle,
|
||||
current,
|
||||
open,
|
||||
onOpenChange,
|
||||
}: NoteTagPickerProps) {
|
||||
const { data: tags } = useMonitorTags()
|
||||
const setNoteTags = useSetNoteTags()
|
||||
const [selected, setSelected] = useState<number[]>([])
|
||||
|
||||
// 每次打开都从「现在的标签」重新起算,否则上一次的勾选会串到另一条作品上。
|
||||
useEffect(() => {
|
||||
if (open) setSelected(current.map((tag) => tag.id))
|
||||
// current 是每次渲染新建的数组,放进依赖会让这个 effect 每帧都跑;只在打开时同步。
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [open, noteId])
|
||||
|
||||
const toggle = (tagId: number) =>
|
||||
setSelected((prev) =>
|
||||
prev.includes(tagId) ? prev.filter((id) => id !== tagId) : [...prev, tagId],
|
||||
)
|
||||
|
||||
const save = () => {
|
||||
setNoteTags.mutate({ noteId, tagIds: selected })
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-sm">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="text-xs font-mono">给作品贴标签</DialogTitle>
|
||||
<DialogDescription className="text-[10px] font-mono truncate">
|
||||
{noteTitle || noteId}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{tags && tags.length > 0 ? (
|
||||
<div className="space-y-1.5">
|
||||
{tags.map((tag) => (
|
||||
<label
|
||||
key={tag.id}
|
||||
className="flex items-center gap-2 rounded px-1.5 py-1 cursor-pointer hover:bg-cyber-bg-elevated/50"
|
||||
>
|
||||
<Checkbox
|
||||
checked={selected.includes(tag.id)}
|
||||
onCheckedChange={() => toggle(tag.id)}
|
||||
/>
|
||||
<span
|
||||
className={cn(
|
||||
'rounded border px-1.5 py-0 text-[10px] font-mono',
|
||||
tagColorClass(tag.color),
|
||||
)}
|
||||
>
|
||||
{tag.name}
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-[11px] font-mono text-cyber-text-muted">
|
||||
还没有标签。先去「设置 → 作品标签」里加几个。
|
||||
</p>
|
||||
)}
|
||||
|
||||
<DialogFooter className="gap-2">
|
||||
{selected.length > 0 && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="mr-auto"
|
||||
onClick={() => setSelected([])}
|
||||
>
|
||||
<Tag className="w-3 h-3 mr-1" />
|
||||
全摘掉
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" size="sm" onClick={() => onOpenChange(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button size="sm" onClick={save} disabled={setNoteTags.isPending}>
|
||||
保存
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Fragment, useMemo, useRef, useState } from 'react'
|
||||
import { ChevronDown, ChevronRight, ExternalLink, Pencil, Users } from 'lucide-react'
|
||||
import { ChevronDown, ChevronRight, ExternalLink, Pencil, Tag, Users } from 'lucide-react'
|
||||
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { useMonitorNotes, useSetCreatorAlias, useSetNoteAlias } from '@/hooks/useMonitor'
|
||||
@@ -10,13 +10,18 @@ import {
|
||||
formatDelta,
|
||||
formatRelative,
|
||||
} from '@/lib/monitorFormat'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { tagColorClass } from '@/lib/tagColors'
|
||||
import type { MonitorCreator, MonitorNote, NoteMetrics } from '@/types/monitor'
|
||||
import { NoteCover } from './NoteCover'
|
||||
import { NoteTagPicker } from './NoteTagPicker'
|
||||
import { NoteTrendChart } from './NoteTrendChart'
|
||||
|
||||
interface NotesTableProps {
|
||||
taskId: number | null
|
||||
onlyNew: boolean
|
||||
/** 按标签筛。空数组 = 不筛。命中的是**任意一个**选中的标签。 */
|
||||
tagIds?: number[]
|
||||
}
|
||||
|
||||
/** A metric with its change vs the previous run underneath. */
|
||||
@@ -128,8 +133,8 @@ function CreatorStats({ stats }: { stats: AccountStats }) {
|
||||
* `creator_hash` —— 爬虫刻意不落原始 user_id,所以这是唯一稳定的创作者标识;
|
||||
* 显示名是已脱敏的昵称(张***三)。两者都认不出来时才退回"未知博主"。
|
||||
*/
|
||||
export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
|
||||
const { data, isLoading } = useMonitorNotes(taskId, onlyNew)
|
||||
export function NotesTable({ taskId, onlyNew, tagIds = [] }: NotesTableProps) {
|
||||
const { data, isLoading } = useMonitorNotes(taskId, onlyNew, tagIds)
|
||||
const notes = data?.notes
|
||||
const creators = data?.creators
|
||||
const [expandedNote, setExpandedNote] = useState<string | null>(null)
|
||||
@@ -147,6 +152,8 @@ export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
|
||||
const [noteAliasDraft, setNoteAliasDraft] = useState('')
|
||||
const noteAliasCancelled = useRef(false)
|
||||
const setNoteAlias = useSetNoteAlias()
|
||||
// 正在贴标签的那条作品。null = 对话框没开。
|
||||
const [taggingNote, setTaggingNote] = useState<MonitorNote | null>(null)
|
||||
|
||||
const commitAlias = (creatorHash: string) => {
|
||||
if (aliasCancelled.current) {
|
||||
@@ -455,6 +462,20 @@ export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
|
||||
/>
|
||||
) : (
|
||||
<div className="flex items-center gap-1.5 text-[9px] text-cyber-text-muted">
|
||||
{/* 标签放在这一行而不是标题行:标题行已经有 NEW、备注
|
||||
和跳转,再塞几个标签就糊成一团了。标签跟着 id 和
|
||||
快照数走,是同一层的元信息。 */}
|
||||
{note.tags.map((tag) => (
|
||||
<span
|
||||
key={tag.id}
|
||||
className={cn(
|
||||
'rounded border px-1 py-0 text-[9px] font-mono flex-shrink-0',
|
||||
tagColorClass(tag.color),
|
||||
)}
|
||||
>
|
||||
{tag.name}
|
||||
</span>
|
||||
))}
|
||||
<span className="truncate">
|
||||
{note.note_id} · {note.snapshot_count} 次快照
|
||||
</span>
|
||||
@@ -470,6 +491,17 @@ export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
|
||||
<Pencil className="w-2.5 h-2.5" />
|
||||
{note.note_alias ? '改备注' : '备注'}
|
||||
</button>
|
||||
<button
|
||||
onClick={(event) => {
|
||||
event.stopPropagation()
|
||||
setTaggingNote(note)
|
||||
}}
|
||||
title="给这条作品贴标签"
|
||||
className="flex items-center gap-0.5 flex-shrink-0 text-cyber-text-muted hover:text-cyber-neon-cyan"
|
||||
>
|
||||
<Tag className="w-2.5 h-2.5" />
|
||||
标签
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -514,6 +546,19 @@ export function NotesTable({ taskId, onlyNew }: NotesTableProps) {
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{/* 对话框挂在表格**外面** —— 放在 tbody 里会因为表格的布局规则出问题。 */}
|
||||
{taggingNote && (
|
||||
<NoteTagPicker
|
||||
noteId={taggingNote.note_id}
|
||||
noteTitle={taggingNote.title}
|
||||
current={taggingNote.tags}
|
||||
open={taggingNote !== null}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) setTaggingNote(null)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { X } from 'lucide-react'
|
||||
|
||||
import { useMonitorTags } from '@/hooks/useMonitor'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { tagColorClass } from '@/lib/tagColors'
|
||||
|
||||
/**
|
||||
* 按标签筛作品。
|
||||
*
|
||||
* 做成**平铺的开关片**而不是下拉多选:词表是人自己维护的,通常就几个,全摆出来一眼就
|
||||
* 知道有哪些、选中了哪些 —— 下拉还得点开才知道里面有什么。
|
||||
*
|
||||
* 多选是**或**:选中「重点」和「竞品」= 两类都看。求交集的做法是再点一个标签,而不是
|
||||
* 让筛选去猜。
|
||||
*
|
||||
* 一个标签都没有时整块不画 —— 空的筛选条只是占地方。
|
||||
*/
|
||||
export function TagFilter({
|
||||
selected,
|
||||
onChange,
|
||||
}: {
|
||||
selected: number[]
|
||||
onChange: (tagIds: number[]) => void
|
||||
}) {
|
||||
const { data: tags } = useMonitorTags()
|
||||
|
||||
if (!tags || tags.length === 0) return null
|
||||
|
||||
const toggle = (tagId: number) =>
|
||||
onChange(
|
||||
selected.includes(tagId)
|
||||
? selected.filter((id) => id !== tagId)
|
||||
: [...selected, tagId],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-1 flex-wrap">
|
||||
{tags.map((tag) => {
|
||||
const active = selected.includes(tag.id)
|
||||
return (
|
||||
<button
|
||||
key={tag.id}
|
||||
onClick={() => toggle(tag.id)}
|
||||
title={active ? '取消这个筛选' : `只看「${tag.name}」`}
|
||||
className={cn(
|
||||
'rounded border px-1.5 py-0.5 text-[10px] font-mono transition-colors',
|
||||
active
|
||||
? cn(tagColorClass(tag.color), 'bg-cyber-bg-elevated')
|
||||
: 'border-cyber-border-subtle text-cyber-text-muted hover:text-cyber-text-secondary',
|
||||
)}
|
||||
>
|
||||
{tag.name}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
{selected.length > 0 && (
|
||||
<button
|
||||
onClick={() => onChange([])}
|
||||
className="flex items-center gap-0.5 text-[10px] font-mono text-cyber-text-muted hover:text-cyber-neon-pink"
|
||||
>
|
||||
<X className="w-3 h-3" />
|
||||
清除
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user