Skip to content

Add FileUpload component for GSoC 2025 prep #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import UseShare from "./components/UseShare";
import LearnContent from "./components/Content";
import FloatingFAB from "./components/FabButton";
import ResizableContainer from "./components/ResizableContainer";
import FileUpload from "./components/FileUpload";

const { Content } = Layout;

Expand Down Expand Up @@ -129,7 +130,7 @@ const App = () => {
display: "flex",
justifyContent: "center",
alignItems: "center",
minHeight: "calc(100vh - 64px - 70px)", // Adjust for Navbar and Footer height
minHeight: "calc(100vh - 64px - 70px)",
}}
>
<Spinner />
Expand Down Expand Up @@ -159,6 +160,7 @@ const App = () => {
>
<SampleDropdown setLoading={setLoading} />
<UseShare />
<FileUpload />
</Row>
</Col>
<Col span={18}>
Expand All @@ -173,19 +175,19 @@ const App = () => {
}}
>
<ResizableContainer
leftPane={
<Collapse
defaultActiveKey={activePanel}
onChange={onChange}
items={panels}
style={{ marginBottom: "24px" }}
/>
}
rightPane={<AgreementHtml loading={loading} isModal={false} />}
initialLeftWidth={66}
minLeftWidth={30}
minRightWidth={30}
/>
leftPane={
<Collapse
defaultActiveKey={activePanel}
onChange={onChange}
items={panels}
style={{ marginBottom: "24px" }}
/>
}
rightPane={<AgreementHtml loading={loading} isModal={false} />}
initialLeftWidth={66}
minLeftWidth={30}
minRightWidth={30}
/>
</div>
<FloatingFAB />
</div>
Expand Down
64 changes: 64 additions & 0 deletions src/components/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, { useCallback } from 'react';
import { Upload, Button, message } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import type { UploadFile } from 'antd/es/upload/interface';
import useAppStore from '../store/store';

const { Dragger } = Upload;

const FileUpload: React.FC = () => {
const { setTemplateMarkdown, setModelCto, setData } = useAppStore();

const handleFileUpload = useCallback(async (file: File) => {
try {
const content = await file.text();

// Determine file type based on extension
const fileExtension = file.name.split('.').pop()?.toLowerCase();

switch (fileExtension) {
case 'md':
await setTemplateMarkdown(content);
message.success('Template file loaded successfully');
break;
case 'cto':
await setModelCto(content);
message.success('Model file loaded successfully');
break;
case 'json':
await setData(content);
message.success('Data file loaded successfully');
break;
default:
message.error('Unsupported file type. Please upload .md, .cto, or .json files');
}
} catch (error) {
message.error('Failed to read file');
console.error('File upload error:', error);
}
}, [setTemplateMarkdown, setModelCto, setData]);

const uploadProps = {
name: 'file',
multiple: false,
accept: '.md,.cto,.json',
beforeUpload: (file: File) => {
handleFileUpload(file);
return false; // Prevent default upload behavior
},
};

return (
<Dragger {...uploadProps}>
<p className="ant-upload-drag-icon">
<UploadOutlined />
</p>
<p className="ant-upload-text">Click or drag file to upload</p>
<p className="ant-upload-hint">
Support for .md (Template), .cto (Model), or .json (Data) files
</p>
</Dragger>
);
};

export default FileUpload;