Files
tolaria/src/components/NoteIcon.tsx

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-04-07 21:09:06 +02:00
import { useCallback, useEffect } from 'react'
import { NoteTitleIcon } from './NoteTitleIcon'
import { focusNoteIconPropertyEditor } from './noteIconPropertyEvents'
import { resolveNoteIcon } from '../utils/noteIcon'
interface NoteIconProps {
icon: string | null
editable?: boolean
}
2026-04-07 21:09:06 +02:00
export function NoteIcon({ icon, editable = true }: NoteIconProps) {
const hasIcon = resolveNoteIcon(icon).kind !== 'none'
// Listen for command palette "Set Note Icon" event
useEffect(() => {
if (!editable) return
2026-04-07 21:09:06 +02:00
const handler = () => focusNoteIconPropertyEditor()
window.addEventListener('laputa:open-icon-picker', handler)
return () => window.removeEventListener('laputa:open-icon-picker', handler)
}, [editable])
2026-04-07 21:09:06 +02:00
const handleActivate = useCallback(() => {
if (!editable) return
2026-04-07 21:09:06 +02:00
focusNoteIconPropertyEditor()
}, [editable])
return (
<div className="note-icon-area" data-testid="note-icon-area" style={{ position: 'relative' }}>
{hasIcon ? (
<button
className="note-icon-button note-icon-button--active"
2026-04-07 21:09:06 +02:00
onClick={handleActivate}
data-testid="note-icon-display"
2026-04-07 21:09:06 +02:00
title={editable ? 'Edit icon' : undefined}
disabled={!editable}
>
2026-04-07 21:09:06 +02:00
<NoteTitleIcon icon={icon} size={26} />
</button>
) : (
editable && (
<button
className="note-icon-button note-icon-button--add"
2026-04-07 21:09:06 +02:00
onClick={handleActivate}
data-testid="note-icon-add"
title="Add icon"
>
<span className="note-icon-button__plus">+</span>
<span className="note-icon-button__label">Add icon</span>
</button>
)
)}
</div>
)
}