File size: 3,068 Bytes
d36ee3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useRef } from "react";

export const useFileHandler = ({
    setSessions, setFolders, setGlobalWorldInfo, setBgImage, 
    setSelectedFile, showToast 
}: any) => {
    
    const fileInputRef = useRef<HTMLInputElement>(null);
    const importInputRef = useRef<HTMLInputElement>(null);

    const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
        const f = e.target.files?.[0];
        if (!f) return;

        const isText = f.type.startsWith('text') || f.name.endsWith('.md') || f.name.endsWith('.json') || f.name.endsWith('.js') || f.name.endsWith('.ts');
        
        if (!isText) {
            if (showToast) showToast("🚫 Cloud Mode Limit: Text Files Only!");
            e.target.value = '';
            return;
        }

        const r = new FileReader();
        r.onload = (ev) => {
            const result = ev.target?.result;
            if (typeof result === 'string') {
                const safeContent = result.slice(0, 50000); 
                if (result.length > 50000 && showToast) showToast("⚠️ File truncated to 50KB");

                setSelectedFile({
                    url: safeContent,
                    type: 'text',
                    name: f.name
                });
            }
        };
        r.readAsText(f);
        e.target.value = '';
    };

    const handleImportData = (e: React.ChangeEvent<HTMLInputElement>) => {
        const f = e.target.files?.[0];
        if (!f) return;

        const r = new FileReader();
        r.onload = (ev) => {
            const content = ev.target?.result;
            if (typeof content === 'string') {
                try {
                    const d = JSON.parse(content);
                    if (d.sessions) setSessions(d.sessions);
                    if (d.folders) setFolders(d.folders);
                    if (d.globalWorldInfo || d.global_world_info) setGlobalWorldInfo(d.globalWorldInfo || d.global_world_info);
                    if (d.bgImage || d.bg_image) setBgImage(d.bgImage || d.bg_image);
                    
                    if (showToast) showToast("✅ Data Restored");
                } catch (err) {
                    console.error("Import Parse Error:", err);
                    if (showToast) showToast("❌ Corrupted File");
                }
            }
        };
        r.readAsText(f);
        e.target.value = '';
    };

    const handleBgDrop = (e: React.DragEvent) => {
        e.preventDefault();
        const f = e.dataTransfer.files?.[0];
        if (f && f.type.startsWith('image/')) {
            const r = new FileReader();
            r.onload = (ev) => {
                const res = ev.target?.result;
                if (typeof res === 'string') {
                    setBgImage(res);
                    if (showToast) showToast("🖼️ Background Updated");
                }
            };
            r.readAsDataURL(f);
        }
    };

    return {
        fileInputRef,
        importInputRef,
        handleFileSelect,
        handleImportData,
        handleBgDrop
    };
};