File size: 2,413 Bytes
085db90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useRef, useEffect } from 'react';
import { Download, Settings } from 'lucide-react';
import { exportChat, exportApiLog } from '../utils/api';

export default function ExportBar({ sessionId }) {
  const [devOpen, setDevOpen] = useState(false);
  const dropdownRef = useRef(null);

  useEffect(() => {
    const handleClickOutside = (e) => {
      if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
        setDevOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    return () => document.removeEventListener('mousedown', handleClickOutside);
  }, []);

  const downloadFile = (filename, content) => {
    const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = filename;
    a.click();
    URL.revokeObjectURL(url);
  };

  const handleExport = async (fmt) => {
    try {
      const result = await exportChat(sessionId, fmt);
      downloadFile(result.filename, result.content);
    } catch (err) {
      console.error('Export failed:', err);
    }
  };

  const handleApiLogExport = async () => {
    try {
      const result = await exportApiLog(sessionId);
      downloadFile('api_log.json', JSON.stringify(result, null, 2));
      setDevOpen(false);
    } catch (err) {
      console.error('API log export failed:', err);
    }
  };

  if (!sessionId) return null;

  return (
    <div className="export-bar">
      <button className="btn-secondary" onClick={() => handleExport('txt')}>
        <Download size={14} style={{ verticalAlign: 'middle', marginRight: 4 }} />
        Download .txt
      </button>
      <button className="btn-secondary" onClick={() => handleExport('md')}>
        <Download size={14} style={{ verticalAlign: 'middle', marginRight: 4 }} />
        Download .md
      </button>

      <div className="dev-dropdown" ref={dropdownRef}>
        <button
          className="icon-btn"
          onClick={() => setDevOpen(o => !o)}
          title="Developer Options"
        >
          <Settings size={16} />
        </button>
        {devOpen && (
          <div className="dev-dropdown-menu">
            <button onClick={handleApiLogExport}>
              Download Full API Log
            </button>
          </div>
        )}
      </div>
    </div>
  );
}