import { createMemo, createSignal, For, Match, onCleanup, Show, Switch, type JSX } from "solid-js" import { Portal } from "solid-js/web" import { AgentBanner } from "./activity" import { createDraft, DraftBanner } from "./drafting" import { createEditor } from "./edit" import { createFolds } from "./fold" import { Conversation, PrChecks, PrFiles, writtenCount } from "./pr" import { createKeyed, usePlugin, type Context, type MountedSession, type UiMenuEntry } from "@opencode/plugin/gui" import { latest } from "./queries" import { allowed, errorMessage, repoOf, type Actions } from "./actions" import { parseItemUrl, pullFor } from "./git" import type { Check, Item, ItemRef, Label } from "./contract" import type { definition } from "./definition" import { ago, PrIcon } from "./panel" /** A side tab showing one issue or pull request, with its conversation and actions. */ export function ItemView(props: { item: ItemRef; session: MountedSession; actions: Actions }) { const ctx = usePlugin() const ui = ctx.ui const t = ctx.t const git = props.actions.git const editor = createEditor(ctx, props.actions) const [comment, setComment] = createSignal("") const [busy, setBusy] = createSignal() const [section, setSection] = createSignal<"conversation" | "files" | "checks">( (props.actions.takeFocus({ kind: "item", ref: props.item }) as "files" | "checks" | undefined) ?? "conversation", ) const smart = () => ctx.stores.prefs.value.foldItems ?? true const folds = { conversation: createFolds(smart), files: createFolds(), checks: createFolds() } const q = props.actions.queries const data = latest(q.query(() => q.options.item(props.item))) const reviews = latest(q.query(() => ({ ...q.options.reviews(props.item), enabled: data.latest?.kind === "pr" }))) const overview = latest(q.query(() => q.options.overview(props.session.directory))) const local = () => repoOf(overview.latest) === props.item.repo const refresh = () => q.invalidate(q.keys.item(props.item)) const written = () => (data.latest ? writtenCount(data.latest, reviews.latest) : 0) const unresolved = () => reviews.latest?.threads.filter((thread) => !thread.resolved).length ?? 0 const draft = createDraft({ ref: props.item, item: () => data.latest, session: props.session, actions: props.actions }) // Loads the Files section while the user reads the conversation, so switching to it is instant. createKeyed( () => data.latest?.pr?.sha, (head) => q.prefetch(q.options.files(props.item, head)), ) const run = async (id: string, work: () => Promise) => { if (busy()) return setBusy(id) try { await work() } finally { setBusy(undefined) } } const send = (item: Item, event?: "APPROVE" | "REQUEST_CHANGES" | "COMMENT") => run("comment", async () => { const typed = comment() const body = typed.trim() if (!body && event !== "APPROVE") return const done = await props.actions.attempt( () => event ? git.submitReview({ repo: item.repo, number: item.number, event, body }) : git.comment({ repo: item.repo, number: item.number, kind: item.kind, body }), event ? t(`review.sent.${event}`) : undefined, ) // Keeps whatever was typed while the comment was sending. if (done !== undefined && comment() === typed) setComment("") }) const edits = (item: Item): UiMenuEntry[] => [ { label: t("edit.textMenu"), icon: "edit", onSelect: () => void editor.text(item) }, { label: t("edit.labels"), icon: "bullet-list", onSelect: () => void editor.labels(item) }, { label: t("edit.assignees"), icon: "status", onSelect: () => void editor.people(item, "assignees") }, ...(item.kind === "pr" ? [{ label: t("edit.reviewers"), icon: "review" as const, onSelect: () => void editor.people(item, "reviewers") }] : []), { label: t("edit.milestone"), icon: "flask", onSelect: () => void editor.milestone(item) }, ...(item.kind === "pr" && item.state === "OPEN" ? [{ label: item.draft ? t("edit.ready") : t("edit.toDraft"), icon: "check" as const, disabled: !!busy(), onSelect: () => void run("draft", () => editor.draft(item)) }] : []), ] const more = (item: Item): UiMenuEntry[] => [ { label: t("action.openOnGitHub"), icon: "square-arrow-top-right", onSelect: () => ctx.system.openExternal(item.url) }, { label: t("action.copyLink"), icon: "link", onSelect: () => void props.actions.copy(item.url) }, { label: t("action.refresh"), icon: "refresh", onSelect: refresh }, { type: "separator" }, { label: t("view.foldPref"), icon: smart() ? "check" : undefined, onSelect: () => ctx.stores.prefs.update((draft) => { draft.foldItems = !(draft.foldItems ?? true) }), }, { type: "separator" }, ...(item.state === "OPEN" ? allowed(item, "close") ? props.actions.closeMenu(item, (work) => void run("state", work)) : [] : item.state === "CLOSED" && allowed(item, "reopen") ? [{ label: t("action.reopen"), icon: "outline-undo" as const, onSelect: () => void run("state", () => props.actions.setItemState(item, true)) }] : []), ] return (
}> {data.error ? errorMessage(data.error) : t("item.loading")} {t("action.retry")}
} > {(row) => ( // The list row's facts while the conversation loads, so the tab shows what was clicked at once.

{row().title} #{row().number}

· {row().author} {t("item.opened", { ago: ago(Date.parse(row().created) / 1000) })}
{(label) => }
)} } > {(current) => ( <>

{current().title}{" "} {/* The tab says the number; another repository's item says whose. */} {local() ? "" : current().repo}#{current().number}

{(pr) => ( void run("checkout", () => props.actions.progress( t("action.checkoutPr"), () => git.checkoutPr({ cwd: overview.latest?.root ?? props.session.directory, number: current().number, repo: current().repo }), t("toast.switched", { name: pr().head }), ), ) } /> )}
· {current().author} {t("item.opened", { ago: ago(Date.parse(current().created) / 1000) })} } > {(pr) => ( <> · {current().author} {t("item.wantsToMerge")} {pr().head} → {pr().base} · {ago(Date.parse(current().created) / 1000)} · {t("item.fork")} {(review) => ( · {t(`pulls.review.${review()}`)} )} 0}> )}
0 || current().milestone || (current().pr?.requested.length ?? 0) > 0}>
{(label) => } {(login) => @{login}} {(milestone) => ◇ {milestone()}} 0}> {t("item.reviewers")}: {current().pr?.requested.join(", ")}
void run("checkout", () => props.actions.checkout(overview.latest?.root ?? props.session.directory, name, name))} />
{(pr) => (
{(id) => ( )}
folds.conversation.foldAll(!folds.conversation.all())} />
)} setSection("files")} bar={current().kind === "issue"} />
{/* Pinned under the conversation, like the session's composer: the reply and what to do with the item stay in reach however far the conversation scrolls. */}