refactor(explore): extract note tags

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2025-02-19 23:04:04 +01:00
parent 0df149aba3
commit f645cffcd5
No known key found for this signature in database
GPG key ID: DB99ADDDC5C0AF82
2 changed files with 37 additions and 9 deletions

View file

@ -0,0 +1,33 @@
/*
* SPDX-FileCopyrightText: 2025 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import React, { useCallback, type MouseEvent } from 'react'
import { Badge } from 'react-bootstrap'
export interface NoteTagsProps {
tags: string[]
onClickTag?: (tag: string) => void
hoverLabel?: string
}
export const NoteTags: React.FC<NoteTagsProps> = ({ tags, onClickTag, hoverLabel }) => {
const onMouseEvent = useCallback(
(tag: string) => (event: MouseEvent<HTMLDivElement>) => {
event.preventDefault()
onClickTag?.(tag)
},
[onClickTag]
)
if (tags.length === 0) {
return null
}
return tags.map((tag) => (
<Badge key={tag} bg={'secondary'} pill={true} className={'me-1'} onClick={onMouseEvent(tag)} title={hoverLabel}>
{tag}
</Badge>
))
}

View file

@ -15,6 +15,7 @@ import { UiIcon } from '../../common/icons/ui-icon'
import Link from 'next/link'
import { useTranslatedText } from '../../../hooks/common/use-translated-text'
import { useRouter } from 'next/navigation'
import { NoteTags } from '../note-tags/note-tags'
export interface NoteCardProps {
title: string
@ -51,14 +52,6 @@ export const PinnedNoteCard: React.FC<NoteCardProps> = ({ title, id, lastVisited
[router]
)
const tagsChips = useMemo(() => {
return tags.map((tag) => (
<Badge key={tag} bg={'secondary'} pill={true} className={'me-1'} onClick={onClickTag(tag)} title={labelTag}>
{tag}
</Badge>
))
}, [tags, onClickTag, labelTag])
return (
<Card className={`${styles.card}`} as={Link} href={`/n/${primaryAddress}`}>
<Card.Body className={`${styles.cardBody}`}>
@ -73,7 +66,9 @@ export const PinnedNoteCard: React.FC<NoteCardProps> = ({ title, id, lastVisited
</span>
</Card.Title>
<Card.Subtitle className='mb-2 text-muted'>{lastVisitedString}</Card.Subtitle>
<div>{tagsChips}</div>
<div>
<NoteTags tags={tags} onClickTag={onClickTag} hoverLabel={labelTag} />
</div>
</Card.Body>
</Card>
)