Skip to content

TagSpaces is an offline, open source, document manager with tagging support

fix Add new file type crashes the app (#1883)

Stanimir94cc9384963d
diff --git a/app/components/dialogs/settings/SettingsDialog.tsx b/app/components/dialogs/settings/SettingsDialog.tsx
index a43b13abb..8150f964b 100644
--- a/app/components/dialogs/settings/SettingsDialog.tsx
+++ b/app/components/dialogs/settings/SettingsDialog.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useEffect, useRef, useState } from 'react';
+import React, { useState } from 'react';
import { connect } from 'react-redux';
import Button from '@mui/material/Button';
import withStyles from '@mui/styles/withStyles';
@@ -29,26 +29,17 @@ import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
-import { useStateWithCallbackLazy } from 'use-state-with-callback';
-import { extend } from '@tagspaces/tagspaces-common/misc';
import AppConfig from '-/AppConfig';
import ConfirmDialog from '../ConfirmDialog';
import SettingsGeneral from '../settings/SettingsGeneral';
import SettingsKeyBindings from '../settings/SettingsKeyBindings';
import SettingsFileTypes from '../settings/SettingsFileTypes';
import i18n from '-/services/i18n';
-import {
- actions,
- getCurrentLanguage,
- getExtensions,
- getSupportedFileTypes
-} from '-/reducers/settings';
+import { getCurrentLanguage } from '-/reducers/settings';
import { clearAllURLParams } from '-/utils/dom';
import SettingsAdvanced from '-/components/dialogs/settings/SettingsAdvanced';
import DialogCloseButton from '-/components/dialogs/DialogCloseButton';
-import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
import Links from '-/content/links';
-import { TS } from '-/tagspaces.namespace';
const styles: any = () => ({
mainContent: {
@@ -60,141 +51,20 @@ interface Props {
open: boolean;
classes?: any;
onClose: () => void;
- setSupportedFileTypes?: (fileTypes: Array<any>) => void;
openURLExternally: (url: string, skipConfirmation?: boolean) => void;
- supportedFileTypes?: Array<any>;
- extensions: Array<TS.Extension>;
}
function SettingsDialog(props: Props) {
- const [items, setItems] = useStateWithCallbackLazy<Array<any>>([]);
const [currentTab, setCurrentTab] = useState<number>(0);
- const [selectedItem, setSelectedItem] = useState<any>({});
- const [isValidationInProgress, setIsValidationInProgress] = useState<boolean>(
- false
- );
- const [isConfirmDialogOpened, setIsConfirmDialogOpened] = useState<boolean>(
- false
- );
const [
isResetSettingsDialogOpened,
setIsResetSettingsDialogOpened
] = useState<boolean>(false);
- const settingsFileTypeRef = useRef<HTMLDivElement>(null);
-
- useEffect(() => {
- const initSupportedFileTypes = props.supportedFileTypes.reduce(
- (accumulator, fileType) => {
- const modifiedFileType = extend({}, fileType, {
- id: fileType.id || getUuid()
- });
- if (fileType.viewer !== '') {
- accumulator.push(modifiedFileType);
- }
- return accumulator;
- },
- []
- );
- setItems(initSupportedFileTypes, undefined);
- setIsValidationInProgress(false);
- }, [props.supportedFileTypes]);
const handleTabClick = (event, tab) => {
setCurrentTab(tab);
};
- const onAddFileType = (item = defaultFileTypeObject) => {
- setItems([...items, item], () => {
- if (settingsFileTypeRef && settingsFileTypeRef.current) {
- const lastFileType = settingsFileTypeRef.current.querySelector(
- 'li:last-child'
- );
- lastFileType.scrollIntoView({ behavior: 'smooth' });
- }
- });
- };
-
- const updateItems = (
- identifierKey,
- identifierValue,
- targetKey,
- targetValue,
- disableSave = false
- ) => {
- let isSaveable = false;
- let hasViewer = false;
- const modifiedItems = items.reduce((accumulator, item) => {
- let modifiedItem = extend({}, item);
- if (item[identifierKey] === identifierValue) {
- isSaveable = item.type !== '';
- hasViewer = item.viewer !== '';
- modifiedItem = extend(modifiedItem, {
- [targetKey]: targetValue
- });
- }
- accumulator.push(modifiedItem);
- return accumulator;
- }, []);
-
- setItems(modifiedItems, () => {
- if (
- (targetKey !== 'type' && isSaveable && !disableSave) ||
- (targetKey === 'type' && hasViewer && isSaveable && !disableSave)
- ) {
- saveFileTypes(modifiedItems);
- }
- });
- };
-
- const defaultFileTypeObject = {
- id: getUuid(),
- type: '',
- viewer: '',
- editor: '',
- color: '#2196f3'
- };
-
- const saveFileTypes = newItems => {
- const { setSupportedFileTypes } = props;
-
- setIsValidationInProgress(true);
-
- const isValid = validateSelectedFileTypes(newItems);
-
- if (!isValid) {
- return false;
- }
-
- setSupportedFileTypes(newItems);
- };
-
- const validateSelectedFileTypes = newItems => {
- let isValid = true;
-
- newItems.map(item => {
- const hasDuplicates =
- items.filter(targetItem => targetItem.type === item.type).length > 1;
-
- if (
- isValid &&
- (item.type === '' || item.viewer === '' || hasDuplicates)
- ) {
- isValid = false;
- }
- return item;
- });
-
- return isValid;
- };
-
- const removeItem = (itemForRemoval: any) => {
- const filteredItems = items.filter(
- item => item.type !== itemForRemoval.type
- );
- // setItems(filteredItems, undefined);
- saveFileTypes(filteredItems);
- };
-
const renderTitle = () => (
<>
<DialogTitle>
@@ -232,25 +102,6 @@ function SettingsDialog(props: Props) {
const renderContent = () => (
<DialogContent className={props.classes.mainContent}>
- {isConfirmDialogOpened && (
- <ConfirmDialog
- open={isConfirmDialogOpened}
- onClose={() => {
- setIsConfirmDialogOpened(false);
- }}
- title="Confirm"
- content={i18n.t('core:confirmFileTypeDeletion')}
- confirmCallback={result => {
- if (result) {
- removeItem(selectedItem);
- }
- }}
- cancelDialogTID="cancelDeleteFileTypeDialog"
- confirmDialogTID="confirmDeleteFileTypeDialog"
- confirmDialogContentTID="confirmDeleteFileTypeDialogContent"
- />
- )}
-
{isResetSettingsDialogOpened && (
<ConfirmDialog
open={isResetSettingsDialogOpened}
@@ -278,26 +129,9 @@ function SettingsDialog(props: Props) {
/>
)}
- <div
- data-tid="settingsDialog"
- className={props.classes.mainContent}
- ref={settingsFileTypeRef}
- >
+ <div data-tid="settingsDialog" className={props.classes.mainContent}>
{currentTab === 0 && <SettingsGeneral />}
- {currentTab === 1 && (
- <SettingsFileTypes
- items={items}
- selectedItem={selectedItem}
- setSelectedItem={item => setSelectedItem(item)}
- updateItems={updateItems}
- isValidationInProgress={isValidationInProgress}
- onRemoveItem={item => {
- setSelectedItem(item);
- setIsConfirmDialogOpened(true);
- }}
- extensions={props.extensions}
- />
- )}
+ {currentTab === 1 && <SettingsFileTypes />}
{currentTab === 2 && <SettingsKeyBindings />}
{currentTab === 3 && (
<SettingsAdvanced
@@ -323,16 +157,6 @@ function SettingsDialog(props: Props) {
>
{i18n.t('core:help')}
</Button>
- {currentTab === 1 && (
- <Button
- data-tid="addNewFileTypeTID"
- onClick={() => onAddFileType()}
- color="secondary"
- style={{ float: 'left' }}
- >
- {i18n.t('core:addNewFileType')}
- </Button>
- )}
<Button
data-tid="closeSettingsDialog"
@@ -363,17 +187,7 @@ function SettingsDialog(props: Props) {
}
const mapStateToProps = state => ({
- supportedFileTypes: getSupportedFileTypes(state),
- language: getCurrentLanguage(state),
- extensions: getExtensions(state)
-});
-
-const mapDispatchToProps = dispatch => ({
- setSupportedFileTypes: supportedFileTypes =>
- dispatch(actions.setSupportedFileTypes(supportedFileTypes))
+ language: getCurrentLanguage(state)
});
-export default connect(
- mapStateToProps,
- mapDispatchToProps
-)(withStyles(styles)(SettingsDialog));
+export default connect(mapStateToProps)(withStyles(styles)(SettingsDialog));
diff --git a/app/components/dialogs/settings/SettingsFileTypes.tsx b/app/components/dialogs/settings/SettingsFileTypes.tsx
index 7448f41aa..cd325c9e7 100644
--- a/app/components/dialogs/settings/SettingsFileTypes.tsx
+++ b/app/components/dialogs/settings/SettingsFileTypes.tsx
@@ -16,12 +16,13 @@
*
*/
-import React, { useState } from 'react';
+import React, { useState, useEffect, useRef, useReducer } from 'react';
+import { connect } from 'react-redux';
+import { extend } from '@tagspaces/tagspaces-common/misc';
+import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
import withStyles from '@mui/styles/withStyles';
-import ListItem from '@mui/material/ListItem';
import MenuItem from '@mui/material/MenuItem';
import Input from '@mui/material/Input';
-import InputLabel from '@mui/material/InputLabel';
import FormControl from '@mui/material/FormControl';
import IconButton from '@mui/material/IconButton';
import RemoveIcon from '@mui/icons-material/RemoveCircle';
@@ -34,13 +35,23 @@ import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';
-import { TableVirtuoso, TableComponents } from 'react-virtuoso';
+import {
+ TableVirtuoso,
+ TableComponents,
+ TableVirtuosoHandle
+} from 'react-virtuoso';
import Tooltip from '-/components/Tooltip';
import ColorPickerDialog from '-/components/dialogs/ColorPickerDialog';
import i18n from '-/services/i18n';
import TransparentBackground from '-/components/TransparentBackground';
import { TS } from '-/tagspaces.namespace';
import AppConfig from '-/AppConfig';
+import {
+ actions,
+ getExtensions,
+ getSupportedFileTypes
+} from '-/reducers/settings';
+import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
const styles: any = (theme: any) => ({
fileTypeColorDialog: {
@@ -61,50 +72,139 @@ const styles: any = (theme: any) => ({
});
interface Props {
- items: Array<TS.FileTypes>;
+ supportedFileTypes: Array<TS.FileTypes>;
+ setSupportedFileTypes?: (fileTypes: Array<any>) => void;
classes: any;
- updateItems: Function;
- selectedItem: any;
- setSelectedItem: Function;
- isValidationInProgress: boolean;
- onRemoveItem: Function;
extensions: Array<TS.Extension>;
}
function SettingsFileTypes(props: Props) {
+ // const [items, setItems] = useStateWithCallbackLazy<Array<TS.FileTypes>>([]);
+ const items = useRef<Array<TS.FileTypes>>(props.supportedFileTypes);
+ const selectedItem = useRef<TS.FileTypes>(undefined);
+ const isValidationInProgress = useRef<boolean>(false);
+ const [isConfirmDialogOpened, setIsConfirmDialogOpened] = useState<boolean>(
+ false
+ );
const [isColorPickerVisible, setColorPickerVisible] = useState<boolean>(
false
);
+ const settingsFileTypeRef = useRef<TableVirtuosoHandle>(null);
+
+ const [ignored, forceUpdate] = useReducer(x => x + 1, 0);
+
+ /*function getItems(supportedFileTypes) {
+ return supportedFileTypes.reduce((accumulator, fileType) => {
+ const modifiedFileType = extend({}, fileType, {
+ id: fileType.id || getUuid()
+ });
+ if (fileType.viewer !== '') {
+ accumulator.push(modifiedFileType);
+ }
+ return accumulator;
+ }, []);
+ }*/
+
+ useEffect(() => {
+ items.current = props.supportedFileTypes;
+ // setItems(getItems(props.supportedFileTypes), undefined);
+ isValidationInProgress.current = false;
+ }, [props.supportedFileTypes]);
+
+ const updateItems = (
+ identifierKey,
+ identifierValue,
+ targetKey,
+ targetValue,
+ disableSave = false
+ ) => {
+ let isSaveable = false;
+ let hasViewer = false;
+ const modifiedItems = items.current.reduce((accumulator, item) => {
+ let modifiedItem = extend({}, item);
+ if (item[identifierKey] === identifierValue) {
+ isSaveable =
+ item.type !== '' || (targetKey === 'type' && targetValue !== '');
+ hasViewer = item.viewer !== '';
+ modifiedItem = extend(modifiedItem, {
+ [targetKey]: targetValue
+ });
+ }
+ accumulator.push(modifiedItem);
+ return accumulator;
+ }, []);
+ items.current = modifiedItems;
+ // setItems(modifiedItems, () => {
+ if (
+ (targetKey !== 'type' && isSaveable && !disableSave) ||
+ (targetKey === 'type' && hasViewer && isSaveable && !disableSave)
+ ) {
+ saveFileTypes(modifiedItems);
+ } else {
+ isValidationInProgress.current = true;
+ forceUpdate();
+ }
+ //});
+ };
+
+ const saveFileTypes = newItems => {
+ const { setSupportedFileTypes } = props;
+
+ isValidationInProgress.current = true;
- const openColorPicker = selectedItem => {
- const { setSelectedItem } = props;
+ const isValid = validateSelectedFileTypes(newItems);
+
+ if (!isValid) {
+ forceUpdate();
+ return false;
+ }
+
+ setSupportedFileTypes(newItems);
+ };
+
+ const validateSelectedFileTypes = newItems => {
+ let isValid = true;
+
+ newItems.map(item => {
+ const hasDuplicates =
+ items.current.filter(targetItem => targetItem.type === item.type)
+ .length > 1;
+
+ if (
+ isValid &&
+ (item.type === '' || item.viewer === '' || hasDuplicates)
+ ) {
+ isValid = false;
+ }
+ return item;
+ });
+
+ return isValid;
+ };
+
+ const openColorPicker = selected => {
setColorPickerVisible(true);
- setSelectedItem(selectedItem);
+ selectedItem.current = selected;
};
const closeColorPicker = () => {
- const { setSelectedItem } = props;
setColorPickerVisible(false);
- setSelectedItem({});
+ selectedItem.current = undefined;
};
const handleChangeColor = color => {
- const { updateItems, selectedItem } = props;
- updateItems('id', selectedItem.id, 'color', color);
+ updateItems(
+ 'id',
+ selectedItem.current && selectedItem.current.id,
+ 'color',
+ color
+ );
};
const sanitizeFileTypeInput = fileTypeInput =>
fileTypeInput.replace(/[^a-zA-Z0-9 ]/g, '');
- const {
- classes,
- items,
- extensions,
- selectedItem,
- updateItems = () => {},
- onRemoveItem = () => {},
- isValidationInProgress = false
- } = props;
+ const { classes, extensions } = props;
interface ColumnData {
dataKey: keyof TS.FileTypes;
@@ -152,6 +252,31 @@ function SettingsFileTypes(props: Props) {
))
};
+ const onAddFileType = () => {
+ const defaultFileTypeObject = {
+ id: getUuid(),
+ type: '',
+ viewer: '',
+ editor: '',
+ color: '#2196f3'
+ };
+ items.current = [defaultFileTypeObject, ...items.current];
+ forceUpdate();
+ };
+
+ const onRemoveItem = item => {
+ selectedItem.current = item;
+ setIsConfirmDialogOpened(true);
+ };
+
+ const removeItem = (itemForRemoval: any) => {
+ const filteredItems = items.current.filter(
+ item => item.type !== itemForRemoval.type
+ );
+ items.current = filteredItems;
+ saveFileTypes(filteredItems);
+ };
+
function fixedHeaderContent() {
return (
<TableRow>
@@ -180,17 +305,18 @@ function SettingsFileTypes(props: Props) {
<TableCell sx={{ padding: '0 5px 20px 5px' }}>
<FormControl
error={
- (isValidationInProgress && item.type === '') ||
- items.filter(targetItem => targetItem.type === item.type).length >
- 1
+ (isValidationInProgress.current && item.type === '') ||
+ items.current.filter(targetItem => targetItem.type === item.type)
+ .length > 1
}
>
<Input
defaultValue={item.type}
error={
- (isValidationInProgress && item.type === '') ||
- items.filter(targetItem => targetItem.type === item.type)
- .length > 1
+ (isValidationInProgress.current && item.type === '') ||
+ items.current.filter(
+ targetItem => targetItem.type === item.type
+ ).length > 1
}
onBlur={event => {
const nextValue = event.target.value;
@@ -201,9 +327,11 @@ function SettingsFileTypes(props: Props) {
</FormControl>
</TableCell>
<TableCell align={'left'} sx={{ padding: '0 5px 20px 0' }}>
- <FormControl error={isValidationInProgress && item.viewer === ''}>
+ <FormControl
+ error={isValidationInProgress.current && item.viewer === ''}
+ >
<Select
- error={isValidationInProgress && item.viewer === ''}
+ error={isValidationInProgress.current && item.viewer === ''}
value={item.viewer}
sx={{ width: 180 }}
input={<Input id="" />}
@@ -274,11 +402,10 @@ function SettingsFileTypes(props: Props) {
</Button>
</Tooltip>
</TransparentBackground>
- <Tooltip title={i18n.t('core:delete')}>
+ <Tooltip title={i18n.t('removeFileType', { itemType: item.type })}>
<IconButton
data-tid="settingsFileTypes_remove_"
className={classes.fileExtRemove}
- title={i18n.t('removeFileType', { itemType: item.type })}
onClick={() => onRemoveItem(item)}
size="large"
>
@@ -300,16 +427,44 @@ function SettingsFileTypes(props: Props) {
overflow: 'hidden'
}}
>
+ <Button
+ data-tid="addNewFileTypeTID"
+ onClick={onAddFileType}
+ color="secondary"
+ style={{ width: '100%' }}
+ >
+ {i18n.t('core:addNewFileType')}
+ </Button>
+ {isConfirmDialogOpened && (
+ <ConfirmDialog
+ open={isConfirmDialogOpened}
+ onClose={() => {
+ setIsConfirmDialogOpened(false);
+ }}
+ title="Confirm"
+ content={i18n.t('core:confirmFileTypeDeletion')}
+ confirmCallback={result => {
+ if (result) {
+ removeItem(selectedItem.current);
+ }
+ }}
+ cancelDialogTID="cancelDeleteFileTypeDialog"
+ confirmDialogTID="confirmDeleteFileTypeDialog"
+ confirmDialogContentTID="confirmDeleteFileTypeDialogContent"
+ />
+ )}
<TableVirtuoso
style={{
overflowX: 'hidden',
+ height: 'calc(100% - 30px)',
// @ts-ignore
overflowY: AppConfig.isFirefox ? 'auto' : 'overlay'
}}
- data={items}
+ data={items.current}
components={VirtuosoTableComponents}
fixedHeaderContent={fixedHeaderContent}
itemContent={rowContent}
+ ref={settingsFileTypeRef}
/>
</Paper>
@@ -317,10 +472,23 @@ function SettingsFileTypes(props: Props) {
open={isColorPickerVisible}
setColor={handleChangeColor}
onClose={closeColorPicker}
- color={selectedItem.color}
+ color={selectedItem.current && selectedItem.current.color}
/>
</>
);
}
-export default withStyles(styles)(SettingsFileTypes);
+const mapStateToProps = state => ({
+ supportedFileTypes: getSupportedFileTypes(state),
+ extensions: getExtensions(state)
+});
+
+const mapDispatchToProps = dispatch => ({
+ setSupportedFileTypes: supportedFileTypes =>
+ dispatch(actions.setSupportedFileTypes(supportedFileTypes))
+});
+
+export default connect(
+ mapStateToProps,
+ mapDispatchToProps
+)(withStyles(styles)(SettingsFileTypes));
diff --git a/app/utils/extension-utils.js b/app/utils/extension-utils.js
index 838afe846..b314c23d6 100644
--- a/app/utils/extension-utils.js
+++ b/app/utils/extension-utils.js
@@ -49,11 +49,9 @@ function processDirs(directoryPath, dirs, isExternal = false) {
...props
} = packageJsonObj['tsextension'];
- const extensionId = id
- ? id
- : '@tagspaces/extensions/' +
- dir.name +
- (buildFolder ? '/' + buildFolder : '');
+ const extensionId =
+ (id ? id : '@tagspaces/extensions/' + dir.name) +
+ (buildFolder ? '/' + buildFolder : '');
if (fileTypes) {
fileTypes.forEach(fileType => {