Files
Media-Server-Setup/live/src/GroupLabel.jsx

23 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useEffect, useRef, useState } from 'react'
import { names } from './names.js'
// Left end of every tab bar: an editable group name (double-click). Session-only.
export default function GroupLabel({ group }) {
const id = group.id
const name = names.use(s => s.groups[id])
const [editing, setEditing] = useState(false)
const [draft, setDraft] = useState('')
const ref = useRef(null)
useEffect(() => { if (editing) { ref.current?.focus(); ref.current?.select() } }, [editing])
const start = e => { e.stopPropagation(); setDraft(name || ''); setEditing(true) }
const commit = () => { if (!editing) return; names.renameGroup(id, draft.trim()); setEditing(false) }
return (
<div className="grouplabel" title="double-click to name this group" onDoubleClick={start} onPointerDown={e => e.stopPropagation()}>
{editing
? <input ref={ref} value={draft} placeholder="group name" onChange={e => setDraft(e.target.value)} onBlur={commit}
onKeyDown={e => { e.stopPropagation(); if (e.key === 'Enter') commit(); if (e.key === 'Escape') setEditing(false) }} />
: <span className={name ? 'gname' : 'hint'}>{name || '+ name'}</span>}
</div>
)
}