Skip to content

Commit 7a8f8ac

Browse files
committed
PreviewDialog: don't refresh preview unless file uri has changed
This fixes a glitch that appeared on Windows where FS changes everytime a file is accessed. Problem also appeared on Unix if the folder was updated. This commit also fixes the NoPreview date/size that wasn't always updated.
1 parent f255172 commit 7a8f8ac

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ const App = observer(() => {
304304
{splitView && <SideView viewState={views[1]} hide={!isExplorer} />}
305305
<Downloads hide={isExplorer} />
306306
</div>
307-
<PreviewDialog />
307+
{cache.cursor && <PreviewDialog />}
308308
</React.Fragment>
309309
</Provider>
310310
)

src/components/dialogs/PreviewDialog.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { KeyboardEvent as KE, ReactNode, useCallback, useEffect, useMemo,
22
import { useStores } from '$src/hooks/useStores'
33
import { Button, Classes, Colors, Dialog, Icon } from '@blueprintjs/core'
44
import { observer } from 'mobx-react'
5-
import DocViewer, { DocViewerRenderers, IConfig } from 'react-doc-viewer'
5+
import DocViewer, { DocViewerRenderers, IConfig, IDocument } from 'react-doc-viewer'
66
import { TypeIcons } from '$src/constants/icons'
77
import { formatBytes } from '$src/utils/formatBytes'
88
import { FileDescriptor } from '$src/services/Fs'
@@ -28,13 +28,16 @@ export const PreviewDialog = observer(() => {
2828
const view = appState.activeView
2929
const cache = view.getVisibleCache()
3030
const cursorIndex = cache.getFileIndex(cache.cursor)
31+
const currentFile = cache.join(cache.cursor.dir, cache.cursor.fullname).replace(/#/g, '%23')
3132
const docs = useMemo(() => {
3233
return cache.files.map((file) => ({ uri: cache.join(file.dir, file.fullname).replace(/#/g, '%23') }))
33-
}, [cache.cursor])
34-
const activeDocument = docs[cursorIndex]
35-
const { isDir, type, length, mDate } = cache.cursor || ({} as FileDescriptor)
34+
}, [currentFile])
35+
const activeDocumentRef = useRef<IDocument>({
36+
...docs[cursorIndex],
37+
})
38+
activeDocumentRef.current.uri = docs[cursorIndex].uri
39+
const { isDir, type } = cache.cursor || ({} as FileDescriptor)
3640
const icon = (isDir && TypeIcons['dir']) || (type && TypeIcons[type]) || TypeIcons['any']
37-
const size = (length && formatBytes(length)) || 0
3841
const theme = settingsState.isDarkModeActive ? darkTheme : lightTheme
3942

4043
const Header = ({ cache }: { cache: FileState }) => {
@@ -70,6 +73,9 @@ export const PreviewDialog = observer(() => {
7073
const NoPreviewRenderer = ({ document, fileName }: any) => {
7174
const { t } = useTranslation()
7275
const fileText = fileName || document?.fileType || ''
76+
const { mDate, length, type, isDir } = cache.cursor
77+
const icon = (isDir && TypeIcons['dir']) || (type && TypeIcons[type]) || TypeIcons['any']
78+
const size = (length && formatBytes(length)) || 0
7379
const modifiedString = (mDate && t('DIALOG.PREVIEW.LAST_MODIFIED_ON', { date: mDate.toLocaleString() })) || ''
7480

7581
return (
@@ -123,8 +129,8 @@ export const PreviewDialog = observer(() => {
123129
}}
124130
config={viewerConfig}
125131
documents={docs}
126-
initialActiveDocument={activeDocument}
127-
activeDocument={activeDocument}
132+
initialActiveDocument={activeDocumentRef.current}
133+
activeDocument={activeDocumentRef.current}
128134
pluginRenderers={DocViewerRenderers}
129135
theme={theme}
130136
/>

0 commit comments

Comments
 (0)