"use client";
import { useEffect, useState } from "react";
import ReactMarkdown from "react-markdown";
import { IconX, IconFileCode } from "@tabler/icons-react";
import { Badge } from "@kandev/ui/badge";
import { Button } from "@kandev/ui/button";
import { useAppStore } from "@/components/state-provider";
import { useCommentsStore } from "@/lib/state/slices/comments";
import { useFileEditors } from "@/hooks/use-file-editors";
import { useRunComment } from "@/hooks/domains/comments/use-run-comment";
import { revealFileAtLine, type OpenFileFn } from "@/lib/diff/walkthrough-reveal";
import { generateUUID } from "@/lib/utils";
import { useToast } from "@/components/toast-provider";
import {
markdownComponents,
normalizeMarkdown,
remarkPlugins,
} from "@/components/shared/markdown-components";
import type { TaskWalkthrough, WalkthroughStep } from "@/lib/types/http";
import type { WalkthroughComment } from "@/lib/state/slices/comments";
import { CommentForm } from "./comment-form";
import { cn } from "@kandev/ui/lib/utils";
import { useTranslation } from "react-i18next";
export const WALKTHROUGH_STEP_BODY_CLASS =
"prose prose-sm dark:prose-invert max-w-none text-left text-sm leading-6 [overflow-wrap:anywhere] [text-align:left] [&_li]:text-left [&_p]:my-2 [&_p]:text-left";
function StepBody({ step, onOpenFile }: { step: WalkthroughStep; onOpenFile: () => void }) {
const { t } = useTranslation();
const lineLabel = step.line_end ? `${step.line}–${step.line_end}` : `${step.line}`;
const fileName = step.file.split("/").pop() || step.file;
return (
{step.title ? (
{step.title}
) : null}
{normalizeMarkdown(step.text)}
);
}
function StepHeader({
activeStep,
stepCount,
lineLabel,
onClose,
}: {
activeStep: number;
stepCount: number;
lineLabel: string;
onClose: () => void;
}) {
const { t } = useTranslation();
return (
{t("diff:walkthrough")}
{t("diff:stepProgress", { current: activeStep + 1, total: stepCount, lineLabel })}
);
}
function StepNavigation({
activeStep,
stepCount,
onPrevious,
onNext,
}: {
activeStep: number;
stepCount: number;
onPrevious: () => void;
onNext: () => void;
}) {
const { t } = useTranslation();
return (
);
}
/** True when the active task has a walkthrough with a valid active step. */
export function useHasActiveWalkthroughStep(): boolean {
return useAppStore((s) => {
const taskId = s.tasks.activeTaskId;
if (!taskId) return false;
const wt = s.walkthroughs.byTaskId[taskId];
const idx = s.walkthroughs.activeStepByTaskId[taskId] ?? 0;
return !!wt?.steps[idx];
});
}
function useWalkthroughStepFeedback(params: {
activeStep: number;
activeTaskId: string | null;
sessionId: string | null | undefined;
step: WalkthroughStep | undefined;
stepCount: number;
walkthrough: TaskWalkthrough | null | undefined;
}) {
const { t } = useTranslation();
const { activeStep, activeTaskId, sessionId, step, stepCount, walkthrough } = params;
const addComment = useCommentsStore((s) => s.addComment);
const { runComment } = useRunComment({ sessionId: sessionId ?? null, taskId: activeTaskId });
const { toast } = useToast();
const buildComment = (text: string): WalkthroughComment | null => {
if (!sessionId || !activeTaskId || !walkthrough || !step) return null;
return buildWalkthroughComment({
sessionId,
taskId: activeTaskId,
walkthrough,
step,
activeStep,
stepCount,
text,
});
};
const showMissingSessionError = () => {
toast({ title: t("diff:noActiveSessionForWalkthroughNote"), variant: "error" });
};
const addWalkthroughFeedback = (text: string) => {
const comment = buildComment(text);
if (!comment) {
showMissingSessionError();
return;
}
addComment(comment);
toast({ title: t("diff:walkthroughNoteAdded"), variant: "success" });
};
const runWalkthroughFeedback = (text: string) => {
const comment = buildComment(text);
if (!comment) {
showMissingSessionError();
return;
}
addComment(comment);
void runComment(comment)
.then(({ queued }) => {
toast({
title: queued ? t("diff:walkthroughNoteQueued") : t("diff:walkthroughNoteSent"),
variant: "success",
});
})
.catch(() => {
toast({ title: t("diff:failedToSendWalkthroughNote"), variant: "error" });
});
};
return { addWalkthroughFeedback, runWalkthroughFeedback };
}
/**
* The shared inner card body (header, markdown, Prev/Next, ask box) used by both
* the inline diff-anchored card and the editor-mode floating window. Reads all
* state from the store; renders nothing when there is no active step. The note
* box mirrors other agent feedback surfaces: "Add" creates a pending
* walkthrough context item, and "Run" sends the same context to the agent.
*/
export function WalkthroughStepInner({
onClose,
onSelectFile,
layout = "inline",
}: {
onClose: () => void;
onSelectFile?: OpenFileFn;
layout?: "inline" | "floating";
}) {
const { t } = useTranslation();
const activeTaskId = useAppStore((s) => s.tasks.activeTaskId);
const sessionId = useAppStore((s) => s.tasks.activeSessionId);
const walkthrough = useAppStore((s) =>
activeTaskId ? s.walkthroughs.byTaskId[activeTaskId] : null,
);
const activeStep = useAppStore((s) =>
activeTaskId ? (s.walkthroughs.activeStepByTaskId[activeTaskId] ?? 0) : 0,
);
const setActiveStep = useAppStore((s) => s.setWalkthroughActiveStep);
const markSeen = useAppStore((s) => s.markWalkthroughSeen);
const { openFile: defaultOpenFile } = useFileEditors();
const openFile = onSelectFile ?? defaultOpenFile;
const stepCount = walkthrough?.steps.length ?? 0;
const step = walkthrough?.steps[activeStep];
const { addWalkthroughFeedback, runWalkthroughFeedback } = useWalkthroughStepFeedback({
activeStep,
activeTaskId,
sessionId,
step,
stepCount,
walkthrough,
});
useEffect(() => {
if (activeTaskId && walkthrough) markSeen(activeTaskId);
}, [activeTaskId, walkthrough, markSeen]);
if (!activeTaskId || !walkthrough) return null;
if (!step) return null;
const isFloating = layout === "floating";
// Two keys rather than one plural: a range and a single line are structurally
// different messages, not two counts of the same one. Left English, this fed
// the translated `stepProgress` header as a hardcoded "Line"/"Lines".
const lineLabel = step.line_end
? t("diff:lineRangeLabel", { start: step.line, end: step.line_end })
: t("diff:lineLabel", { line: step.line });
const navigation = (
setActiveStep(activeTaskId, activeStep - 1)}
onNext={() => setActiveStep(activeTaskId, activeStep + 1)}
/>
);
return (
revealFileAtLine(openFile, step.file, step.line, step.repo)}
/>
{!isFloating ? navigation : null}
{isFloating ? navigation : null}
);
}
function buildWalkthroughComment(params: {
sessionId: string;
taskId: string;
walkthrough: TaskWalkthrough;
step: WalkthroughStep;
activeStep: number;
stepCount: number;
text: string;
}): WalkthroughComment {
const { sessionId, taskId, walkthrough, step, activeStep, stepCount, text } = params;
const startLine = step.line;
const endLine = step.line_end ?? step.line;
return {
id: generateUUID(),
source: "walkthrough",
sessionId,
taskId,
walkthroughId: walkthrough.id,
walkthroughTitle: walkthrough.title,
stepIndex: activeStep,
stepCount,
repo: step.repo,
filePath: step.file,
startLine: Math.min(startLine, endLine),
endLine: Math.max(startLine, endLine),
stepText: step.text,
text,
createdAt: new Date().toISOString(),
status: "pending",
};
}
/**
* Inline walkthrough popover rendered as a diff annotation, anchored directly
* beneath the step's line. A caret + left accent visually bind it to the line
* above.
*/
export function WalkthroughStepCard() {
const hasStep = useHasActiveWalkthroughStep();
const activeTaskId = useAppStore((s) => s.tasks.activeTaskId);
const activeStep = useAppStore((s) =>
activeTaskId ? (s.walkthroughs.activeStepByTaskId[activeTaskId] ?? 0) : 0,
);
const updatedAt = useAppStore((s) =>
activeTaskId ? s.walkthroughs.byTaskId[activeTaskId]?.updated_at : undefined,
);
const [dismissedKey, setDismissedKey] = useState(null);
const stepKey = activeTaskId ? `${activeTaskId}:${updatedAt ?? ""}:${activeStep}` : "";
if (!hasStep || dismissedKey === stepKey) return null;
return (
setDismissedKey(stepKey)} />
);
}