Skip to content

Commit

Permalink
fix: add file name validation during renaming before upload and fix s…
Browse files Browse the repository at this point in the history
…haring/publishing displaying (Issue #253, #385) (#383) (#386)

* fix: add file name validation during renaming before upload
* fix: fix sharing and publishing features checking
  • Loading branch information
IlyaBondar authored Dec 20, 2023
1 parent 24076f5 commit 706213c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
15 changes: 13 additions & 2 deletions src/components/Chatbar/components/ChatFolders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export function ChatFolders() {
ConversationsSelectors.selectMyItemsFilters,
);

const isPublishingEnabled = useAppSelector((state) =>
SettingsSelectors.isPublishingEnabled(state, FeatureType.Chat),
);

const isSharingEnabled = useAppSelector((state) =>
SettingsSelectors.isSharingEnabled(state, FeatureType.Chat),
);
Expand All @@ -297,7 +301,7 @@ export function ChatFolders() {
() =>
[
{
hidden: !isSharingEnabled || !isFilterEmpty,
hidden: !isPublishingEnabled || !isFilterEmpty,
name: t('Organization'),
filters: PublishedWithMeFilter,
displayRootFiles: true,
Expand All @@ -320,7 +324,14 @@ export function ChatFolders() {
dataQa: 'pinned-chats',
},
].filter(({ hidden }) => !hidden),
[commonItemFilter, isFilterEmpty, isSharingEnabled, searchTerm.length, t],
[
commonItemFilter,
isFilterEmpty,
isPublishingEnabled,
isSharingEnabled,
searchTerm.length,
t,
],
);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/Common/FolderContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const FolderContextMenu = ({
SettingsSelectors.isPublishingEnabled(state, featureType),
);
const isSharingEnabled = useAppSelector((state) =>
SettingsSelectors.isPublishingEnabled(state, featureType),
SettingsSelectors.isSharingEnabled(state, featureType),
);
const menuItems: DisplayMenuItemProps[] = useMemo(
() => [
Expand Down
2 changes: 1 addition & 1 deletion src/components/Common/ItemContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function ItemContextMenu({
SettingsSelectors.isPublishingEnabled(state, featureType),
);
const isSharingEnabled = useAppSelector((state) =>
SettingsSelectors.isPublishingEnabled(state, featureType),
SettingsSelectors.isSharingEnabled(state, featureType),
);
const menuItems: DisplayMenuItemProps[] = useMemo(
() => [
Expand Down
44 changes: 26 additions & 18 deletions src/components/Files/PreUploadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ export const PreUploadDialog = ({
);

const handleUpload = useCallback(() => {
const errors = [];
if (attachments.length + selectedFiles.length > 10) {
setErrorMessage(
errors.push(
t(
`Maximum allowed attachments number is {{maxAttachmentsAmount}}. With your uploadings amount will be {{selectedAttachmentsAmount}}`,
{
Expand All @@ -196,7 +197,21 @@ export const PreUploadDialog = ({
},
) as string,
);
return;
}
const incorrectFileNames: string[] = getFilesWithInvalidFileName(
selectedFiles,
).map((file) => file.name);

if (incorrectFileNames.length > 0) {
errors.push(
t(
`The symbols {{notAllowedSymbols}} are not allowed in file name. Please rename or remove them from uploading files list: {{fileNames}}`,
{
notAllowedSymbols: notAllowedSymbols.join(''),
fileNames: incorrectFileNames.join(', '),
},
) as string,
);
}

const attachmentsNames = files
Expand All @@ -206,32 +221,25 @@ export const PreUploadDialog = ({
.filter((file) => attachmentsNames.includes(file.name))
.map((file) => file.name);
if (localIncorrectSameNameFiles.length > 0) {
setErrorMessage(
errors.push(
t(
'Files which you trying to upload already presented in selected folder. Please rename or remove them from uploading files list: {{fileNames}}',
{ fileNames: localIncorrectSameNameFiles.join(', ') },
) as string,
);
return;
}
let isFilesNamesSame = false;
for (let i = 0; i < selectedFiles.length - 1; i++) {
for (let j = i + 1; j < selectedFiles.length; j++) {
if (selectedFiles[i].name === selectedFiles[j].name) {
isFilesNamesSame = true;
break;
}
}
if (isFilesNamesSame) {
break;
}
}
if (isFilesNamesSame) {
setErrorMessage(

const fileNameSet = new Set(selectedFiles.map((file) => file.name));
if (fileNameSet.size < selectedFiles.length) {
errors.push(
t(
'Files which you trying to upload have same names. Please rename or remove them from uploading files list',
) as string,
);
}

if (errors.length) {
setErrorMessage(errors.join('\n'));
return;
}

Expand Down
15 changes: 13 additions & 2 deletions src/components/Promptbar/components/PromptFolders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,15 @@ export function PromptFolders() {
SettingsSelectors.isSharingEnabled(state, FeatureType.Prompt),
);

const isPublishingEnabled = useAppSelector((state) =>
SettingsSelectors.isPublishingEnabled(state, FeatureType.Prompt),
);

const folderItems: FolderSectionProps[] = useMemo(
() =>
[
{
hidden: !isSharingEnabled || !isFilterEmpty,
hidden: !isPublishingEnabled || !isFilterEmpty,
name: t('Organization'),
filters: PublishedWithMeFilter,
displayRootFiles: true,
Expand All @@ -304,7 +308,14 @@ export function PromptFolders() {
dataQa: 'pinned-prompts',
},
].filter(({ hidden }) => !hidden),
[commonSearchFilter, isFilterEmpty, isSharingEnabled, searchTerm.length, t],
[
commonSearchFilter,
isFilterEmpty,
isPublishingEnabled,
isSharingEnabled,
searchTerm.length,
t,
],
);

return (
Expand Down
4 changes: 3 additions & 1 deletion src/utils/app/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export const notAllowedSymbolsRegex = new RegExp(
`[${notAllowedSymbols.join()}]`,
'g',
);
export const getFilesWithInvalidFileName = (files: File[]): File[] => {
export const getFilesWithInvalidFileName = <T extends { name: string }>(
files: T[],
): T[] => {
return files.filter(({ name }) => name.match(notAllowedSymbolsRegex));
};

Expand Down

0 comments on commit 706213c

Please sign in to comment.