Skip to content

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

add new workSpacesDropdown component

sytolkb75afe8fae50
diff --git a/src/renderer/components/dialogs/CreateEditLocationDialog.tsx b/src/renderer/components/dialogs/CreateEditLocationDialog.tsx
index cf984bf33..145231dc7 100644
--- a/src/renderer/components/dialogs/CreateEditLocationDialog.tsx
+++ b/src/renderer/components/dialogs/CreateEditLocationDialog.tsx
@@ -74,6 +74,7 @@ import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import TsToggleButton from '../TsToggleButton';
import LocalForm from './components/LocalForm';
+import WorkSpacesDropdown from '-/components/dialogs/components/WorkSpacesDropdown';
interface Props {
open: boolean;
@@ -819,46 +820,22 @@ function CreateEditLocationDialog(props: Props) {
</AccordionSummary>
<AccordionDetails>
<FormGroup>
- <TsSelect
+ <WorkSpacesDropdown
+ disabled={!Pro}
+ dataTid="locationWorkspaceTID"
+ workSpaceId={workSpaceId}
+ setWorkSpaceId={setWorkSpaceId}
+ workSpaces={workSpaces}
label={
<>
{t('core:workspace')}
<ProLabel />
</>
}
- disabled={!Pro}
- style={{ minWidth: 200 }}
- data-tid="locationWorkspaceTID"
- value={workSpaceId}
- onChange={(event: ChangeEvent<HTMLInputElement>) =>
- setWorkSpaceId(event.target.value)
+ onOpenNewWorkspace={() =>
+ workSpacesContext.openNewWorkspaceDialog()
}
- slotProps={{
- input: {
- endAdornment: workSpaceId && (
- <InputAdornment position="end" sx={{ ml: -12 }}>
- <TsIconButton
- aria-label={t('core:deleteWSpace')}
- onClick={() => setWorkSpaceId('')}
- data-tid="wSpaceResetTID"
- >
- <RemoveIcon />
- </TsIconButton>
- </InputAdornment>
- ),
- },
- }}
- >
- {workSpaces?.map((wSpace) => (
- <MenuItem
- key={wSpace.uuid}
- value={wSpace.uuid}
- data-tid={'wSpace' + wSpace.shortName + 'TID'}
- >
- {wSpace.shortName + ' - ' + wSpace.fullName}
- </MenuItem>
- ))}
- </TsSelect>
+ />
<FormControlLabel
labelPlacement="start"
style={{ justifyContent: 'space-between', marginLeft: 0 }}
diff --git a/src/renderer/components/dialogs/CreateTagGroupDialog.tsx b/src/renderer/components/dialogs/CreateTagGroupDialog.tsx
index 0ad796318..a707f01b9 100644
--- a/src/renderer/components/dialogs/CreateTagGroupDialog.tsx
+++ b/src/renderer/components/dialogs/CreateTagGroupDialog.tsx
@@ -17,13 +17,11 @@
*/
import AppConfig from '-/AppConfig';
-import { RemoveIcon } from '-/components/CommonIcons';
import DraggablePaper from '-/components/DraggablePaper';
import { ProLabel } from '-/components/HelperComponents';
import Tag from '-/components/Tag';
import TransparentBackground from '-/components/TransparentBackground';
import TsButton from '-/components/TsButton';
-import TsIconButton from '-/components/TsIconButton';
import TsSelect from '-/components/TsSelect';
import TsTextField from '-/components/TsTextField';
import ColorPickerDialog from '-/components/dialogs/ColorPickerDialog';
@@ -38,7 +36,6 @@ import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
-import InputAdornment from '@mui/material/InputAdornment';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import MenuItem from '@mui/material/MenuItem';
@@ -54,6 +51,7 @@ import React, {
} from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
+import WorkSpacesDropdown from '-/components/dialogs/components/WorkSpacesDropdown';
interface Props {
open: boolean;
@@ -253,46 +251,22 @@ function CreateTagGroupDialog(props: Props) {
</ListItem>
)}
<ListItem style={{ paddingLeft: 0, paddingRight: 0 }}>
- <TsSelect
+ <WorkSpacesDropdown
disabled={!Pro}
- data-tid="taggroupWorkspaceTID"
- value={workSpaceId}
- fullWidth
+ dataTid="createTgWorkspaceTID"
+ workSpaceId={workSpaceId}
+ setWorkSpaceId={setWorkSpaceId}
+ workSpaces={workSpaces}
label={
<>
{t('core:workspace')}
<ProLabel />
</>
}
- onChange={(event: ChangeEvent<HTMLInputElement>) =>
- setWorkSpaceId(event.target.value)
+ onOpenNewWorkspace={() =>
+ workSpacesContext.openNewWorkspaceDialog()
}
- slotProps={{
- input: {
- endAdornment: workSpaceId && (
- <InputAdornment position="end" sx={{ ml: -12 }}>
- <TsIconButton
- aria-label={t('core:deleteWSpace')}
- onClick={() => setWorkSpaceId('')}
- data-tid="wSpaceResetTID"
- >
- <RemoveIcon />
- </TsIconButton>
- </InputAdornment>
- ),
- },
- }}
- >
- {workSpaces.map((wSpace) => (
- <MenuItem
- key={wSpace.uuid}
- value={wSpace.uuid}
- data-tid={'wSpace' + wSpace.shortName + 'TID'}
- >
- {wSpace.shortName + ' - ' + wSpace.fullName}
- </MenuItem>
- ))}
- </TsSelect>
+ />
</ListItem>
<ListItem style={{ paddingLeft: 0, paddingRight: 0 }}>
<ListItemText primary={t('core:tagBackgroundColor')} />
diff --git a/src/renderer/components/dialogs/EditTagGroupDialog.tsx b/src/renderer/components/dialogs/EditTagGroupDialog.tsx
index cd61825bd..bb8c2a18c 100644
--- a/src/renderer/components/dialogs/EditTagGroupDialog.tsx
+++ b/src/renderer/components/dialogs/EditTagGroupDialog.tsx
@@ -15,13 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
-
-import { RemoveIcon } from '-/components/CommonIcons';
+import React, { ChangeEvent, useContext, useEffect, useState } from 'react';
import { ProLabel } from '-/components/HelperComponents';
import Tag from '-/components/Tag';
import TransparentBackground from '-/components/TransparentBackground';
import TsButton from '-/components/TsButton';
-import TsIconButton from '-/components/TsIconButton';
import TsSelect from '-/components/TsSelect';
import TsTextField from '-/components/TsTextField';
import ColorPickerDialog from '-/components/dialogs/ColorPickerDialog';
@@ -39,14 +37,13 @@ import Dialog from '@mui/material/Dialog';
import DialogContent from '@mui/material/DialogContent';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
-import InputAdornment from '@mui/material/InputAdornment';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import MenuItem from '@mui/material/MenuItem';
import Switch from '@mui/material/Switch';
-import React, { ChangeEvent, useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
+import WorkSpacesDropdown from './components/WorkSpacesDropdown';
const defaultTagGroupLocation = 'TAG_LIBRARY';
@@ -236,46 +233,20 @@ function EditTagGroupDialog(props: Props) {
</ListItem>
)}
<ListItem style={{ paddingLeft: 0, paddingRight: 0 }}>
- <TsSelect
+ <WorkSpacesDropdown
disabled={!Pro}
- data-tid="taggroupWorkspaceTID"
- value={workSpaceId}
- fullWidth
+ dataTid="taggroupWorkspaceTID"
+ workSpaceId={workSpaceId}
+ setWorkSpaceId={setWorkSpaceId}
+ workSpaces={workSpaces}
label={
<>
{t('core:workspace')}
<ProLabel />
</>
}
- onChange={(event: ChangeEvent<HTMLInputElement>) =>
- setWorkSpaceId(event.target.value)
- }
- slotProps={{
- input: {
- endAdornment: workSpaceId && (
- <InputAdornment position="end" sx={{ ml: -12 }}>
- <TsIconButton
- aria-label={t('core:deleteWSpace')}
- onClick={() => setWorkSpaceId('')}
- data-tid="wSpaceResetTID"
- >
- <RemoveIcon />
- </TsIconButton>
- </InputAdornment>
- ),
- },
- }}
- >
- {workSpaces.map((wSpace) => (
- <MenuItem
- key={wSpace.uuid}
- value={wSpace.uuid}
- data-tid={'wSpace' + wSpace.shortName + 'TID'}
- >
- {wSpace.shortName + ' - ' + wSpace.fullName}
- </MenuItem>
- ))}
- </TsSelect>
+ onOpenNewWorkspace={() => workSpacesContext.openNewWorkspaceDialog()}
+ />
</ListItem>
<ListItem style={{ paddingLeft: 0, paddingRight: 0 }}>
<ListItemText primary={t('tagBackgroundColor')} />
diff --git a/src/renderer/components/dialogs/components/WorkSpacesDropdown.tsx b/src/renderer/components/dialogs/components/WorkSpacesDropdown.tsx
new file mode 100644
index 000000000..f6f6f0387
--- /dev/null
+++ b/src/renderer/components/dialogs/components/WorkSpacesDropdown.tsx
@@ -0,0 +1,115 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-present TagSpaces GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+import React, { useEffect, useRef } from 'react';
+import TsSelect from '-/components/TsSelect';
+import TsIconButton from '-/components/TsIconButton';
+import MenuItem from '@mui/material/MenuItem';
+import InputAdornment from '@mui/material/InputAdornment';
+import AddIcon from '@mui/icons-material/Add';
+import { RemoveIcon } from '-/components/CommonIcons';
+import { TS } from '-/tagspaces.namespace';
+
+interface WorkSpacesDropdownProps {
+ workSpaceId: string | undefined;
+ setWorkSpaceId: (id: string) => void;
+ workSpaces: TS.WorkSpace[];
+ disabled?: boolean;
+ dataTid?: string; // will be passed as data-tid on the underlying TsSelect
+ label?: React.ReactNode;
+ onOpenNewWorkspace?: () => void;
+}
+
+const WorkSpacesDropdown: React.FC<WorkSpacesDropdownProps> = ({
+ workSpaceId,
+ setWorkSpaceId,
+ workSpaces,
+ disabled = false,
+ dataTid = 'taggroupWorkspaceTID',
+ label,
+ onOpenNewWorkspace,
+}) => {
+ // Keep track of initial workSpaces to detect additions (keeps original behavior from EditTagGroupDialog)
+ const initWorkSpaces = useRef<TS.WorkSpace[]>(workSpaces);
+
+ useEffect(() => {
+ // handle new workSpace add
+ if (JSON.stringify(workSpaces) !== JSON.stringify(initWorkSpaces.current)) {
+ const oldIds = new Set(initWorkSpaces.current.map((item) => item.uuid));
+ const addedItems: TS.WorkSpace[] = workSpaces.filter(
+ (item) => !oldIds.has(item.uuid),
+ );
+ if (addedItems && addedItems.length === 1) {
+ // when a single workspace was added, select it
+ setWorkSpaceId(addedItems[0].uuid);
+ }
+ initWorkSpaces.current = workSpaces;
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [workSpaces]);
+
+ return (
+ <TsSelect
+ disabled={disabled}
+ data-tid={dataTid}
+ value={workSpaceId}
+ fullWidth
+ label={label}
+ onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
+ setWorkSpaceId(event.target.value)
+ }
+ slotProps={{
+ input: {
+ startAdornment: (
+ <InputAdornment position="start" sx={{ ml: -2 }}>
+ <TsIconButton
+ aria-label="add-wspace"
+ onClick={() => onOpenNewWorkspace && onOpenNewWorkspace()}
+ data-tid="wSpaceAddTID"
+ >
+ <AddIcon />
+ </TsIconButton>
+ </InputAdornment>
+ ),
+ endAdornment: workSpaceId && (
+ <InputAdornment position="end">
+ <TsIconButton
+ aria-label="delete-wspace"
+ onClick={() => setWorkSpaceId('')}
+ data-tid="wSpaceResetTID"
+ >
+ <RemoveIcon />
+ </TsIconButton>
+ </InputAdornment>
+ ),
+ },
+ }}
+ >
+ {workSpaces.map((wSpace) => (
+ <MenuItem
+ key={wSpace.uuid}
+ value={wSpace.uuid}
+ data-tid={'wSpace' + wSpace.shortName + 'TID'}
+ >
+ {wSpace.shortName + ' - ' + wSpace.fullName}
+ </MenuItem>
+ ))}
+ </TsSelect>
+ );
+};
+
+export default WorkSpacesDropdown;