Skip to content

Commit 4ab4f83

Browse files
Check file type before accessing its extension (#590)
Since directories do not have `extname`, when switched to index listing, `FilePreview` was throwing an error saying `file.extname` is undefined. Fixed it by checking file type before accessing `extname`.
1 parent 5c8bc51 commit 4ab4f83

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/components/FilePreview.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ export default class FilePreview extends Component {
3434
}
3535
}
3636

37+
get isImageFile() {
38+
const {
39+
file: { type, extname },
40+
} = this.props;
41+
if (type === 'directory') {
42+
return false;
43+
}
44+
45+
return /png|jpg|gif|jpeg|svg|ico/i.test(extname.substring(1));
46+
}
47+
3748
render() {
3849
const { onClick, file, splat } = this.props;
39-
const extension = file.extname.substring(1);
40-
const image = /png|jpg|gif|jpeg|svg|ico/i.test(extension);
41-
const node = image ? (
50+
const node = this.isImageFile ? (
4251
<img src={file.http_url} alt={file.relative_path} />
4352
) : (
4453
<div>

0 commit comments

Comments
 (0)