aboutsummaryrefslogtreecommitdiffstats
path: root/renderer/src/components/DocumentPane.tsx
blob: c2e1b2cd0521dcf614b6e6ff8bb2f28b5b89d18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useState, useMemo } from "react";
import type { Phase } from "../types";

interface DocumentPaneProps {
  content: string;
  onChange: (content: string) => void;
  phase: Phase;
  disabled: boolean;
  showOnboarding?: boolean;
}

function renderTable(tableLines: string[]): string {
  if (tableLines.length < 2) return tableLines.join("\n");
  
  const parseRow = (line: string): string[] => {
    return line
      .split("|")
      .slice(1, -1) // Remove empty first/last from |col|col|
      .map((cell) => cell.trim());
  };

  const headerCells = parseRow(tableLines[0]);
  // Skip separator row (index 1)
  const bodyRows = tableLines.slice(2);

  let html = '<table><thead><tr>';
  headerCells.forEach((cell) => {
    html += `<th>${cell}</th>`;
  });
  html += '</tr></thead><tbody>';
  
  bodyRows.forEach((row) => {
    if (row.trim()) {
      html += '<tr>';
      parseRow(row).forEach((cell) => {
        html += `<td>${cell}</td>`;
      });
      html += '</tr>';
    }
  });
  
  html += '</tbody></table>';
  return html;
}

function renderMarkdown(md: string): string {
  // First, handle tables (before other transformations)
  const lines = md.split("\n");
  const processedLines: string[] = [];
  let tableBuffer: string[] = [];
  let inTable = false;

  for (const line of lines) {
    const isTableLine = /^\|.*\|$/.test(line.trim());
    
    if (isTableLine) {
      inTable = true;
      tableBuffer.push(line);
    } else {
      if (inTable && tableBuffer.length > 0) {
        processedLines.push(renderTable(tableBuffer));
        tableBuffer = [];
        inTable = false;
      }
      processedLines.push(line);
    }
  }
  
  // Handle table at end of content
  if (tableBuffer.length > 0) {
    processedLines.push(renderTable(tableBuffer));
  }

  let result = processedLines.join("\n");

  return (
    result
      // Headers
      .replace(/^### (.*$)/gm, "<h3>$1</h3>")
      .replace(/^## (.*$)/gm, "<h2>$1</h2>")
      .replace(/^# (.*$)/gm, "<h1>$1</h1>")
      // Bold/italic
      .replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
      .replace(/\*([^*]+)\*/g, "<em>$1</em>")
      // Code blocks
      .replace(
        /```(\w*)\n([\s\S]*?)```/g,
        '<pre><code class="language-$1">$2</code></pre>'
      )
      .replace(/`([^`]+)`/g, "<code>$1</code>")
      // Lists
      .replace(/^- \[x\] (.*$)/gm, '<li class="task done">☑ $1</li>')
      .replace(/^- \[ \] (.*$)/gm, '<li class="task">☐ $1</li>')
      .replace(/^- (.*$)/gm, "<li>$1</li>")
      // Review comments (highlight them)
      .replace(/(\/\/ REVIEW:.*$)/gm, '<mark class="review">$1</mark>')
      .replace(/(\/\/ NOTE:.*$)/gm, '<mark class="note">$1</mark>')
      // Paragraphs
      .replace(/\n\n/g, "</p><p>")
      .replace(/^(.+)$/gm, "<p>$1</p>")
      // Clean up
      .replace(/<p><\/p>/g, "")
      .replace(/<p>(<h[1-3]>)/g, "$1")
      .replace(/(<\/h[1-3]>)<\/p>/g, "$1")
      .replace(/<p>(<pre>)/g, "$1")
      .replace(/(<\/pre>)<\/p>/g, "$1")
      .replace(/<p>(<li)/g, "$1")
      .replace(/(<\/li>)<\/p>/g, "$1")
      .replace(/<p>(<table>)/g, "$1")
      .replace(/(<\/table>)<\/p>/g, "$1")
  );
}

export function DocumentPane({
  content,
  onChange,
  phase,
  disabled,
  showOnboarding,
}: DocumentPaneProps) {
  const [isEditing, setIsEditing] = useState(false);
  const renderedHtml = useMemo(() => renderMarkdown(content), [content]);

  if (showOnboarding) {
    return (
      <div className="document-pane">
        <div className="document-header">
          <span>Welcome to Claude Flow</span>
        </div>
        <div className="document-content rendered onboarding">
          <h1>Claude Flow</h1>
          <p>
            A structured workflow for AI-assisted coding: <strong>Research → Plan → Implement</strong>.
          </p>

          <h2>Setup</h2>
          <p>Export your Anthropic API key:</p>
          <pre><code>export ANTHROPIC_API_KEY=your-key-here</code></pre>
          <p>Get one at <a href="https://platform.claude.com" target="_blank" rel="noopener">platform.claude.com</a></p>

          <h2>Getting Started</h2>
          <ol>
            <li><strong>Add a project</strong> — Select a codebase folder</li>
            <li><strong>Create a session</strong> — Start a new task</li>
            <li><strong>Describe your work</strong> — Tell Claude what you want to build</li>
          </ol>

          <h2>Workflow</h2>
          <p><strong>Research:</strong> Claude analyzes your codebase and writes findings to <code>research.md</code>. Add notes like <code>// REVIEW: check this</code> — click <strong>Review</strong> when done.</p>
          <p><strong>Plan:</strong> Claude drafts an implementation plan in <code>plan.md</code> with code snippets and a TODO list. Iterate the same way.</p>
          <p><strong>Implement:</strong> Claude executes the plan, marking tasks complete as it goes.</p>

          <p className="onboarding-tip">Iterate on research and plan docs as long as you want. Click <strong>Submit</strong> when happy to move to the next phase.</p>
        </div>
      </div>
    );
  }

  if (phase === "implement") {
    return (
      <div className="document-pane">
        <div className="document-header">
          <span>plan.md</span>
          <span className="badge">Implementing...</span>
        </div>
        <div
          className="document-content rendered"
          dangerouslySetInnerHTML={{ __html: renderedHtml }}
        />
      </div>
    );
  }

  const filename = phase === "research" ? "research.md" : "plan.md";

  return (
    <div className="document-pane">
      <div className="document-header">
        <span>{filename}</span>
        <button onClick={() => setIsEditing(!isEditing)}>
          {isEditing ? "Preview" : "Edit"}
        </button>
      </div>

      {isEditing ? (
        <textarea
          className="document-content editing"
          value={content}
          onChange={(e) => onChange(e.target.value)}
          disabled={disabled}
          placeholder={`${filename} will appear here...`}
        />
      ) : (
        <div
          className="document-content rendered"
          dangerouslySetInnerHTML={{
            __html:
              renderedHtml ||
              '<p class="empty">Document will appear here after Claude generates it...</p>',
          }}
          onClick={() => !disabled && setIsEditing(true)}
        />
      )}
    </div>
  );
}