@@ -18,20 +18,23 @@ import {
1818 findPathInTree ,
1919} from './utils/file-tree-util'
2020
21- import { confirm , message , open } from '@tauri-apps/plugin-dialog'
22- import { documentDir , resolve } from '@tauri-apps/api/path'
23- import { getCurrent } from '@tauri-apps/api/window'
21+ import { useToast } from '@/components/ui/use-toast'
22+ import { useTranslation } from 'react-i18next'
2423import {
24+ fullScreen ,
25+ openLink ,
26+ searchKeywordInDir ,
27+ readDir ,
28+ showInFlower ,
29+ chooseDir ,
30+ resolve ,
2531 exists ,
2632 mkdir ,
2733 rename ,
2834 writeTextFile ,
2935 readTextFile ,
3036 remove ,
31- } from '@tauri-apps/plugin-fs'
32- import { useTranslation } from 'react-i18next'
33- import { invoke } from '@tauri-apps/api/core'
34- import { open as openLink } from '@tauri-apps/plugin-shell'
37+ } from '@/lib/bindings'
3538import Tree from './Tree'
3639import SearchList from './SearchList'
3740import TocList from './TocList'
@@ -48,6 +51,7 @@ import dayjs from 'dayjs'
4851import { useLocalStorage } from 'react-use'
4952import { Context } from './Layout'
5053import { useHotkeys } from 'react-hotkeys-hook'
54+ import { useConfirm } from './ui/confirm'
5155
5256interface FileNode {
5357 name : string
@@ -91,6 +95,8 @@ const FileTree = forwardRef<TreeRef, Props>(
9195 { onSelect, selectedPath, setShowPPT, onScroll, dirPath, setDirPath } ,
9296 ref
9397 ) => {
98+ const { toast } = useToast ( )
99+ const { confirm } = useConfirm ( )
94100 const { isMacOS } = useContext ( Context )
95101 const { t } = useTranslation ( )
96102 const [ scrollLine , setScrollLine ] = useState ( 1 )
@@ -108,12 +114,9 @@ const FileTree = forwardRef<TreeRef, Props>(
108114 const [ fileTreeData , setTreeData ] = useState < FileNode [ ] > ( [ ] )
109115
110116 const reloadTree = async ( ) => {
111- const res = await exists ( dirPath )
112- if ( res ) {
113- const result : FileNode = await invoke ( 'read_dir' , { path : dirPath } )
114- if ( result . children ) {
115- setTreeData ( result . children )
116- }
117+ const result = await readDir < FileNode > ( dirPath )
118+ if ( result . children ) {
119+ setTreeData ( result . children )
117120 }
118121 }
119122 useEffect ( ( ) => {
@@ -174,8 +177,9 @@ title: ${file}
174177 const content = await readTextFile ( filePath )
175178 openMd ( fullPath , content . replace ( / { { date} } / g, fileName ) )
176179 } catch ( error ) {
177- message ( t ( 'The template file does not exist' ) , {
178- title : t ( 'Prompt' ) ,
180+ toast ( {
181+ title : t ( 'Error' ) ,
182+ description : t ( 'The template file does not exist' ) ,
179183 } )
180184 }
181185 } else {
@@ -209,15 +213,17 @@ title: ${file}
209213 reloadTree ( )
210214 onSelect ( dirPath + '/' + fileName )
211215 } else {
212- message ( t ( 'Please set the working directory' ) , {
216+ toast ( {
213217 title : t ( 'Prompt' ) ,
218+ description : t ( 'Please set the working directory' ) ,
214219 } )
215220 }
216221 } )
217222
218223 const fileExists = async ( ) => {
219- await message ( t ( 'File already exists' ) , {
224+ toast ( {
220225 title : t ( 'Prompt' ) ,
226+ description : t ( 'File already exists' ) ,
221227 } )
222228 setAction ( {
223229 path : '' ,
@@ -247,10 +253,10 @@ title: ${file}
247253 if ( e . keyCode !== 13 ) {
248254 return
249255 }
250- const result : SearchResultItem [ ] = await invoke ( 'search_keyword_in_dir' , {
251- keyword : value . trim ( ) ,
252- path : dirPath ,
253- } )
256+ const result = await searchKeywordInDir < SearchResultItem [ ] > (
257+ value . trim ( ) ,
258+ dirPath
259+ )
254260 console . log ( 'search' , result )
255261 setSearchList ( result )
256262 setSearchValue ( value )
@@ -490,7 +496,7 @@ title: ${file}
490496 const handleOpenFinder = async ( ) => {
491497 setMenuStyle ( { display : 'none' } )
492498 const path = selectedKeys [ 0 ]
493- await invoke ( 'show_in_folder' , { path } )
499+ showInFlower ( path )
494500 }
495501
496502 const handleRefresh = async ( ) => {
@@ -504,30 +510,27 @@ title: ${file}
504510 return
505511 }
506512 const name = getCurrentFolderName ( path )
507- const confirmed = await confirm (
508- `${ t ( 'Are you sure you want to delete' ) } '${ name } '?` ,
509- {
510- title : t ( 'Delete confirmation' ) ,
511- kind : 'warning' ,
512- }
513- )
514- if ( confirmed ) {
515- if ( supportFile ( path ) ) {
516- await remove ( path )
517- } else {
518- remove ( path , {
519- recursive : true ,
520- } )
521- }
522- reloadTree ( )
523- }
513+
514+ confirm ( {
515+ title : t ( 'Delete confirmation' ) ,
516+ description : `${ t ( 'Are you sure you want to delete' ) } ${ name } ?` ,
517+ onOk : async ( ) => {
518+ if ( supportFile ( path ) ) {
519+ await remove ( path )
520+ } else {
521+ remove ( path , {
522+ recursive : true ,
523+ } )
524+ }
525+ reloadTree ( )
526+ } ,
527+ } )
524528 } , [ selectedKeys , dirPath ] )
525529
526530 const handlePPT = async ( ) => {
527531 setMenuStyle ( { display : 'none' } )
528532 setShowPPT ( )
529- const appWindow = getCurrent ( )
530- await appWindow . setFullscreen ( true )
533+ fullScreen ( )
531534 }
532535
533536 let menu : MenuItemProps [ ] = [
@@ -552,10 +555,7 @@ title: ${file}
552555 menu = dirItemMenu
553556 }
554557 const handleChooseDir = async ( ) => {
555- const selected = await open ( {
556- directory : true ,
557- defaultPath : await documentDir ( ) ,
558- } )
558+ const selected = await chooseDir ( )
559559 if ( selected ) {
560560 setDirPath ( selected )
561561 setExpandedKeys ( [ ] )
@@ -567,38 +567,40 @@ title: ${file}
567567 < div
568568 data-tauri-drag-region
569569 className = { clsx (
570- 'px-4 pb-3 bg-white dark:bg-gray-900 sticky top-0 left-0 z-10 border-b border-gray-200 dark:border-gray-800 flex items-center flex-none ' ,
571- isMacOS ? 'pt-7 ' : 'pt-4'
570+ 'px-4 pb-3 bg-white dark:bg-gray-900 sticky top-0 left-0 z-10 border-b flex-none dark:shadow-highlight/4 ' ,
571+ isMacOS ? 'pt-6 ' : 'pt-4'
572572 ) }
573573 >
574- < div className = "flex items-center w-full text-left px-2 h-8 mt-[2px] bg-white ring-1 ring-slate-900/10 hover:ring-slate-300 focus:outline-none focus:ring-2 focus:ring-sky-500 shadow-sm rounded-lg text-slate-400 dark:bg-slate-800 dark:ring-0 dark:text-slate-300 dark:highlight-white/5 dark:hover:bg-slate-700" >
575- < SearchIcon className = "w-4 h-4 flex-none text-slate-300 dark:text-slate-400" />
576- < input
577- className = "flex-auto bg-transparent w-full px-2 focus:ring-0 outline-none h-full border-0 text-slate-900 dark:text-slate-200"
578- aria-label = "search"
579- ref = { searchInputRef }
580- onKeyDown = { onChange }
581- />
582- { searchValue && (
583- < XCircleIcon
584- onClick = { ( ) => {
585- setSearchValue ( '' )
586- if ( searchInputRef . current ) {
587- searchInputRef . current . value = ''
588- }
589- } }
590- className = "w-4 h-4 cursor-pointer flex-none text-slate-300 dark:text-slate-400"
574+ < div className = "h-9 flex items-center" >
575+ < div className = "flex items-center w-full text-left px-2 h-8 mt-[2px] bg-white ring-1 ring-slate-900/10 hover:ring-slate-300 focus:outline-none focus:ring-2 focus:ring-sky-500 shadow-sm rounded-lg text-slate-400 dark:bg-slate-800 dark:ring-0 dark:text-slate-300 dark:highlight-white/5 dark:hover:bg-slate-700" >
576+ < SearchIcon className = "w-4 h-4 flex-none text-slate-300 dark:text-slate-400" />
577+ < input
578+ className = "flex-auto bg-transparent w-full px-2 focus:ring-0 outline-none h-full border-0 text-slate-900 dark:text-slate-200"
579+ aria-label = "search"
580+ ref = { searchInputRef }
581+ onKeyDown = { onChange }
591582 />
583+ { searchValue && (
584+ < XCircleIcon
585+ onClick = { ( ) => {
586+ setSearchValue ( '' )
587+ if ( searchInputRef . current ) {
588+ searchInputRef . current . value = ''
589+ }
590+ } }
591+ className = "w-4 h-4 cursor-pointer flex-none text-slate-300 dark:text-slate-400"
592+ />
593+ ) }
594+ </ div >
595+ { ! searchValue && (
596+ < span
597+ onClick = { ( ) => setShowToc ( ! showToc ) }
598+ className = "pl-2 cursor-pointer text-slate-700 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-300"
599+ >
600+ { showToc ? < ListIcon /> : < ListTreeIcon /> }
601+ </ span >
592602 ) }
593603 </ div >
594- { ! searchValue && (
595- < span
596- onClick = { ( ) => setShowToc ( ! showToc ) }
597- className = "pl-2 cursor-pointer text-slate-700 hover:text-slate-900 dark:text-slate-400 dark:hover:text-slate-300"
598- >
599- { showToc ? < ListIcon /> : < ListTreeIcon /> }
600- </ span >
601- ) }
602604 </ div >
603605 < SearchList
604606 value = { searchList }
0 commit comments