Skip to content

Commit

Permalink
fix(desktop): app crashes when file path does not exist
Browse files Browse the repository at this point in the history
close #282
  • Loading branch information
drl990114 committed Sep 11, 2023
1 parent 0ad82d4 commit f837ac2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
7 changes: 7 additions & 0 deletions apps/desktop/src-tauri/src/fc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub fn remove_folder(path: &str) -> AnyResult<()> {


pub mod cmd {
use std::path::Path;

use crate::fc;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
Expand Down Expand Up @@ -158,5 +160,10 @@ pub mod cmd {
fc::remove_folder(file_path);
String::from("OK")
}

#[tauri::command]
pub fn file_exists(file_path: &str) -> bool {
fc::exists(Path::new(file_path))
}

}
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
fc::cmd::write_file,
fc::cmd::delete_file,
fc::cmd::delete_folder,
fc::cmd::file_exists,
conf::cmd::get_app_conf_path,
conf::cmd::get_app_conf,
conf::cmd::reset_app_conf,
Expand Down
20 changes: 16 additions & 4 deletions apps/desktop/src/components/EditorArea/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EditorCount } from '@/editorToolBar/EditorCount'
import bus from '@/helper/eventBus'
import { EVENT } from '@/constants'
import classNames from 'classnames'
import { WarningHeader } from './styles'

const EditorWrapper = styled.div<{ active: boolean }>`
min-height: 100%;
Expand Down Expand Up @@ -44,17 +45,23 @@ function Editor(props: EditorProps) {
const { execute } = useCommandStore()
const editorRef = useRef<EditorRef>(null)
const [delegate, setDelegate] = useState(createWysiwygDelegate())
const [notExistFile, setNotExistFile] = useState(false)

useEffect(() => {
const init = async () => {
const file = getFileObject(id)
setEditorDelegate(id, delegate)

if (file.path) {
const text = await invoke('get_file_content', {
filePath: file.path,
})
setContent(text as string)
const isExists = await invoke('file_exists', { filePath: file.path })
if (isExists) {
const text = await invoke('get_file_content', {
filePath: file.path,
})
setContent(text as string)
} else {
setNotExistFile(true)
}
} else if (file.content !== undefined) {
setContent(file.content)
}
Expand Down Expand Up @@ -121,6 +128,11 @@ function Editor(props: EditorProps) {
[curFile, content, active, delegate],
)


if (notExistFile) {
return <WarningHeader>File is not exist</WarningHeader>
}

const cls = classNames('code-contents', {
'editor-active': active,
'display-none': !active,
Expand Down
6 changes: 6 additions & 0 deletions apps/desktop/src/components/EditorArea/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ export const Dot = styled.div<DotProps>`
interface TabItemProps {
active: boolean
}


export const WarningHeader = styled.h3`
text-align: center;
color: ${(props) => props.theme.warnColor};
`

0 comments on commit f837ac2

Please sign in to comment.