/** @jsxImportSource @opentui/solid */
import { createEffect, createMemo, createSignal, For, onCleanup, Show } from "solid-js"
import type { Plugin } from "@opencode/plugin/tui"
import type { Todo, TodoStatus } from "./todos.js"
import { headerLabel, headerTimeLabel, todowritesFromMessages } from "./tui-data.js"
import { buildTiming, findItem, hasCompletedItem, itemTimeLabel } from "./timing.js"
import {
normalizeSettings,
openSettingsMenu,
SETTINGS_KEY,
type TodoDisplaySettings,
} from "./tui-settings.js"
/** V1 glyphs: ✓ completed, • in progress, blank otherwise. */
function mark(status: TodoStatus): string {
if (status === "completed") return "✓"
if (status === "in_progress") return "•"
return " "
}
function TodoRow(props: { context: Plugin.Context; todo: Todo; time?: string }) {
const color = () =>
props.todo.status === "in_progress"
? props.context.theme.text.feedback.warning.base
: props.context.theme.text.muted
return (
{`[${mark(props.todo.status)}] `}
{props.todo.content}
{` · ${props.time}`}
)
}
/** V1-parity todo list at the end of the sidebar, with configurable heading. */
function TodoStrip(props: { context: Plugin.Context; sessionID: string; settings: TodoDisplaySettings }) {
const [open, setOpen] = createSignal(true)
const [now, setNow] = createSignal(Date.now())
const display = () => normalizeSettings(props.settings)
const writes = createMemo(() => {
try {
return todowritesFromMessages(props.context.data.session.message.list(props.sessionID))
} catch {
return []
}
})
const timing = createMemo(() => buildTiming(writes()))
const todos = createMemo(() => {
const list = writes()
return list.length === 0 ? [] : list[list.length - 1].todos
})
const run = createMemo(() => timing().current ?? timing().last)
const finished = createMemo(() => {
const state = timing()
return (
state.current === undefined &&
state.last !== undefined &&
state.last.abandoned !== true &&
hasCompletedItem(state.last)
)
})
// Only tick while a run is open; the closed total is rendered from its end time.
createEffect(() => {
if (!display().timer || timing().current === undefined) return
const id = setInterval(() => setNow(Date.now()), 1000)
onCleanup(() => clearInterval(id))
})
const show = createMemo(() => {
const list = todos()
if (list.length === 0) return false
if (list.some((todo) => todo.status !== "completed")) return true
return display().timer && finished()
})
const collapsible = () => todos().length > display().collapseThreshold
const header = createMemo(() => headerLabel(todos(), display()))
const headerTime = createMemo(() => headerTimeLabel(timing(), display().timer, now()))
return (
collapsible() && setOpen((value) => !value)}>
{open() ? "▼" : "▶"}
{header()}
{headerTime()}
0}>
1}>
{(todo) => {
const time = () => (display().timer ? itemTimeLabel(findItem(run(), todo.content), now()) : undefined)
return
}}
)
}
/** Settings command lives in the app slot so it stays available when the sidebar is hidden. */
function SettingsRoot(props: {
context: Plugin.Context
settings: TodoDisplaySettings
update: (mutation: (draft: TodoDisplaySettings) => void) => Promise
}) {
props.context.keymap.layer(() => ({
mode: "global" as const,
commands: [
{
id: "aiev.todolist.settings",
title: "Todolist: Settings",
description: "Configure the todo list reminders and sidebar",
slash: { name: "todo-settings" },
palette: true,
run: () => openSettingsMenu(props.context, props.settings, props.update),
},
],
}))
return null
}
const mod: Plugin.Definition = {
id: "aiev.todolist.tui",
setup(context) {
const [settings, updateSettings] = context.storage.store(SETTINGS_KEY, {
initial: normalizeSettings(undefined),
})
context.ui.slot({
append: "app",
render: () => ,
})
context.ui.slot({
append: "sidebar.content",
render: (props) => ,
})
},
}
export default mod