1- import { useEffect , useState } from "react" ;
1+ import { type ChangeEvent , useEffect , useRef , useState } from "react" ;
2+ import { toast } from "react-hot-toast" ;
23import { useNavigate } from "react-router-dom" ;
34import { DarkModeToggle } from "../components/DarkModeToggle" ;
45import "./Home.scss" ;
56import { ImportNote } from "../components/ImportNote.tsx" ;
7+ import { importNote } from "../services/noteAPI.ts" ;
68
79export function Home ( ) {
810 const [ visible , setVisible ] = useState ( false ) ;
@@ -22,6 +24,8 @@ export function Home() {
2224 return ( ) => clearTimeout ( t ) ;
2325 } , [ ] ) ;
2426
27+ const fileInputRef = useRef < HTMLInputElement | null > ( null ) ;
28+
2529 return (
2630 < >
2731 < DarkModeToggle />
@@ -49,6 +53,32 @@ export function Home() {
4953 < span className = "warning" >
5054 ⚠️ Please don’t upload illegal or sensitive content.
5155 </ span >
56+ < label className = "upload" >
57+ < input
58+ type = "file"
59+ accept = ".qnote"
60+ style = { { display : 'none' } }
61+ ref = { fileInputRef }
62+ onChange = { async ( e : ChangeEvent < HTMLInputElement > ) => {
63+ try {
64+ const file = e . target . files ?. [ 0 ] ;
65+ if ( ! file ) return ;
66+ const success = await importNote ( file ) ;
67+ if ( ! success ) {
68+ toast . error ( "Failed to import note" ) ;
69+ return ;
70+ }
71+
72+ navigate ( `/note/${ file . name . replace ( / \. q n o t e $ / , "" ) } ` ) ;
73+
74+ } catch ( error ) {
75+ console . error ( error ) ;
76+ toast . error ( "Failed to import note" ) ;
77+ }
78+ } }
79+ />
80+ < img src = "/file-pencil-alt-svgrepo-com.svg" alt = "upload icon" />
81+ </ label >
5282 </ p >
5383 < div className = "input-container" >
5484 < input
0 commit comments