File size: 3,518 Bytes
30605ea
 
15d490d
c4c7362
d1d23d8
30605ea
4fd5040
 
 
 
 
15d490d
30605ea
5093905
30605ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fd5040
30605ea
4fd5040
 
 
 
6bf36a9
4fd5040
30605ea
 
4fd5040
30605ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4c7362
30605ea
 
 
 
c4c7362
 
d1d23d8
c4c7362
 
 
 
 
 
 
30605ea
c4c7362
30605ea
c4c7362
15d490d
 
d1d23d8
c4c7362
 
 
 
 
 
 
15d490d
30605ea
 
 
 
 
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
import type { Message } from 'ai';
import { toast } from 'react-toastify';
import { ImportFolderButton } from '~/components/chat/ImportFolderButton';
import { Button } from '~/components/ui/Button';
import { classNames } from '~/utils/classNames';

type ChatData = {
  messages?: Message[]; // Standard Bolt format
  description?: string; // Optional description
};

export function ImportButtons(importChat: ((description: string, messages: Message[]) => Promise<void>) | undefined) {
  return (
    <div className="flex flex-col items-center justify-center w-auto">
      <input
        type="file"
        id="chat-import"
        className="hidden"
        accept=".json"
        onChange={async (e) => {
          const file = e.target.files?.[0];

          if (file && importChat) {
            try {
              const reader = new FileReader();

              reader.onload = async (e) => {
                try {
                  const content = e.target?.result as string;
                  const data = JSON.parse(content) as ChatData;

                  // Standard format
                  if (Array.isArray(data.messages)) {
                    await importChat(data.description || 'Imported Chat', data.messages);
                    toast.success('Chat imported successfully');

                    return;
                  }

                  toast.error('Invalid chat file format');
                } catch (error: unknown) {
                  if (error instanceof Error) {
                    toast.error('Failed to parse chat file: ' + error.message);
                  } else {
                    toast.error('Failed to parse chat file');
                  }
                }
              };
              reader.onerror = () => toast.error('Failed to read chat file');
              reader.readAsText(file);
            } catch (error) {
              toast.error(error instanceof Error ? error.message : 'Failed to import chat');
            }
            e.target.value = ''; // Reset file input
          } else {
            toast.error('Something went wrong');
          }
        }}
      />
      <div className="flex flex-col items-center gap-4 max-w-2xl text-center">
        <div className="flex gap-2">
          <Button
            onClick={() => {
              const input = document.getElementById('chat-import');
              input?.click();
            }}
            variant="outline"
            size="lg"
            className={classNames(
              'gap-2 bg-[#F5F5F5] dark:bg-[#252525]',
              'text-bolt-elements-textPrimary dark:text-white',
              'hover:bg-[#E5E5E5] dark:hover:bg-[#333333]',
              'border-[#E5E5E5] dark:border-[#333333]',
              'h-10 px-4 py-2 min-w-[120px] justify-center',
              'transition-all duration-200 ease-in-out',
            )}
          >
            <span className="i-ph:upload-simple w-4 h-4" />
            Import Chat
          </Button>
          <ImportFolderButton
            importChat={importChat}
            className={classNames(
              'gap-2 bg-[#F5F5F5] dark:bg-[#252525]',
              'text-bolt-elements-textPrimary dark:text-white',
              'hover:bg-[#E5E5E5] dark:hover:bg-[#333333]',
              'border border-[#E5E5E5] dark:border-[#333333]',
              'h-10 px-4 py-2 min-w-[120px] justify-center',
              'transition-all duration-200 ease-in-out rounded-lg',
            )}
          />
        </div>
      </div>
    </div>
  );
}