Skip to content

Commit

Permalink
feat(chat): add focus on new created folder (Issue #1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
Derikyan committed Jun 27, 2024
1 parent 62751b0 commit 0229454
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
62 changes: 48 additions & 14 deletions apps/chat/src/components/Chat/ChangePathDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { ChangeEvent, useCallback, useEffect, useState } from 'react';

import { useTranslation } from 'next-i18next';

import { constructPath } from '@/src/utils/app/file';
import {
getChildAndCurrentFoldersIdsById,
getFolderIdFromEntityId,
getNextDefaultName,
getPathToFolderById,
validateFolderRenaming,
} from '@/src/utils/app/folders';
import { getConversationRootId } from '@/src/utils/app/id';

import { FeatureType } from '@/src/types/common';
import { SharingType } from '@/src/types/share';
Expand All @@ -23,6 +27,7 @@ import {
} from '@/src/store/prompts/prompts.reducers';
import { UIActions } from '@/src/store/ui/ui.reducers';

import { DEFAULT_FOLDER_NAME } from '@/src/constants/default-ui-settings';
import {
MAX_CONVERSATION_AND_PROMPT_FOLDERS_DEPTH,
PUBLISHING_FOLDER_NAME,
Expand Down Expand Up @@ -68,6 +73,7 @@ export const ChangePathDialog = ({
: { selectors: PromptsSelectors, actions: PromptsActions };

const newFolderId = useAppSelector(selectors.selectNewAddedFolderId);

const folders = useAppSelector((state) =>
selectors.selectTemporaryAndFilteredFolders(state, searchQuery),
);
Expand Down Expand Up @@ -97,20 +103,23 @@ export const ChangePathDialog = ({

return;
}
const selectedFolder = folders.find((f) => f.id === folderId);

if (
type === SharingType.Conversation ||
type === SharingType.ConversationFolder
) {
dispatch(
ConversationsActions.uploadConversationsWithFolders({
ids: [folderId],
}),
);
} else {
dispatch(
PromptsActions.uploadChildPromptsWithFolders({ ids: [folderId] }),
);
if (!selectedFolder?.temporary) {
if (
type === SharingType.Conversation ||
type === SharingType.ConversationFolder
) {
dispatch(
ConversationsActions.uploadConversationsWithFolders({
ids: [folderId],
}),
);
} else {
dispatch(
PromptsActions.uploadChildPromptsWithFolders({ ids: [folderId] }),
);
}
}

if (openedFoldersIds.includes(folderId)) {
Expand Down Expand Up @@ -139,6 +148,19 @@ export const ChangePathDialog = ({
const handleRenameFolder = useCallback(
(newName: string, folderId: string) => {
const error = validateFolderRenaming(folders, newName, folderId, false);
const newFolderId = constructPath(
getFolderIdFromEntityId(folderId),
newName,
);
const mappedFolderIds = folders.map(({ id }) => id);

if (mappedFolderIds.some((id) => id === newFolderId)) {
return;
}

setSelectedFolderId(
constructPath(getFolderIdFromEntityId(folderId), newName),
);

if (error) {
setErrorMessage(t(error) as string);
Expand All @@ -152,6 +174,18 @@ export const ChangePathDialog = ({

const handleAddFolder = useCallback(
(parentFolderId: string) => {
const folderName = getNextDefaultName(
t(DEFAULT_FOLDER_NAME),
folders,
0,
false,
true,
);

setSelectedFolderId(
constructPath(parentFolderId || getConversationRootId(), folderName),
);

dispatch(
actions.createTemporaryFolder({
relativePath: parentFolderId,
Expand All @@ -162,7 +196,7 @@ export const ChangePathDialog = ({
setOpenedFoldersIds(openedFoldersIds.concat(parentFolderId));
}
},
[actions, dispatch, openedFoldersIds],
[actions, dispatch, folders, openedFoldersIds, t],
);

const handleDeleteFolder = useCallback(
Expand Down
4 changes: 3 additions & 1 deletion apps/chat/src/store/conversations/conversations.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ export const conversationsSlice = createSlice({
const name = payload.name.trim();

state.temporaryFolders = state.temporaryFolders.map((folder) =>
folder.id !== payload.folderId ? folder : { ...folder, name },
folder.id !== payload.folderId
? folder
: { ...folder, name, id: constructPath(folder.folderId, name) },
);
},
resetNewFolderId: (state) => {
Expand Down

0 comments on commit 0229454

Please sign in to comment.