///
import { type Entry, type Item } from './item'
export type Shared = {
addEntry: (item: Item) => Promise,
setEditing: (key: string | null) => void,
focusInput: () => void,
getHistory: (key: string) => Entry[],
}
function fmtDateTime(ts: number): string {
return new Date(ts).toLocaleString(undefined, {
month: 'short', day: 'numeric', year: 'numeric',
hour: 'numeric', minute: '2-digit',
})
}
function fmtDate(ts: number): string {
return new Date(ts).toLocaleDateString(undefined, {
month: 'short', day: 'numeric', year: 'numeric',
})
}
export function renderItem(group: Entry[], shared: Shared): HTMLLIElement {
let currentItem = group[0]!.item
const li = document.createElement("li")
li.className = "list-item"
li.dataset.key = currentItem.key
const row = document.createElement("div")
row.className = "item-row"
if (currentItem.checked != null) {
const checkbox = document.createElement("input")
checkbox.type = "checkbox"
checkbox.checked = currentItem.checked
checkbox.onchange = async () => {
await shared.addEntry({ ...currentItem, checked: checkbox.checked })
currentItem = { ...currentItem, checked: checkbox.checked }
}
row.appendChild(checkbox)
}
const textSpan = document.createElement("span")
textSpan.className = "item-text"
textSpan.textContent = currentItem.text
textSpan.onclick = () => {
const editor = document.createElement("input")
editor.type = "text"
editor.className = "editor"
editor.value = currentItem.text
shared.setEditing(currentItem.key)
textSpan.replaceWith(editor)
editor.focus()
editor.setSelectionRange(editor.value.length, editor.value.length)
const commit = async () => {
editor.onblur = null
const updated = editor.value.trim()
if (updated !== currentItem.text) {
await shared.addEntry({ ...currentItem, text: updated })
currentItem = { ...currentItem, text: updated }
textSpan.textContent = updated
rebuildHistory()
}
editor.replaceWith(textSpan)
shared.setEditing(null)
shared.focusInput()
if (updated === "") li.remove()
}
editor.onkeydown = (e) => {
if (e.key === "Enter") commit()
else if (e.key === "Escape") { editor.value = currentItem.text; commit() }
}
editor.onblur = commit
}
row.appendChild(textSpan)
// Info: date · id · history toggle
const info = document.createElement("div")
info.className = "item-info"
const dateSpan = document.createElement("span")
dateSpan.className = "item-date"
dateSpan.textContent = fmtDate(currentItem.created)
info.appendChild(dateSpan)
const idSpan = document.createElement("span")
idSpan.className = "item-id"
idSpan.textContent = currentItem.key.slice(0, 8)
idSpan.title = currentItem.key
info.appendChild(idSpan)
const histBtn = document.createElement("button")
histBtn.className = "history-btn"
info.appendChild(histBtn)
row.appendChild(info)
li.appendChild(row)
// Collapsible history panel
const histPanel = document.createElement("div")
histPanel.className = "history-panel"
histPanel.hidden = true
li.appendChild(histPanel)
let histOpen = false
const rebuildHistory = () => {
const entries = shared.getHistory(currentItem.key)
histBtn.textContent = entries.length === 1 ? "1 entry" : `${entries.length} entries`
if (!histOpen) return
histPanel.innerHTML = ""
const keyLine = document.createElement("div")
keyLine.className = "history-key"
keyLine.textContent = `ID: ${currentItem.key}`
histPanel.appendChild(keyLine)
const table = document.createElement("table")
table.className = "history-table"
for (const e of entries) {
const tr = document.createElement("tr")
const tdTime = document.createElement("td")
tdTime.textContent = fmtDateTime(e.created)
const tdText = document.createElement("td")
tdText.textContent = e.item.text || "(deleted)"
const tdId = document.createElement("td")
tdId.className = "entry-id"
tdId.textContent = e.id.slice(0, 8)
tdId.title = e.id
tr.append(tdTime, tdText, tdId)
table.appendChild(tr)
}
histPanel.appendChild(table)
}
histBtn.onclick = () => {
histOpen = !histOpen
histPanel.hidden = !histOpen
rebuildHistory()
}
rebuildHistory() // set initial button label
return li
}