Skip to content

Commit

Permalink
fix folders structure when migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Kezik authored and IlyaBondar committed Feb 12, 2024
1 parent af06c79 commit 9733bfb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions apps/chat/src/store/conversations/conversations.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ const migrateConversationsEpic: AppEpic = (action$, state$) => {
.getConversations()
.pipe(map(filterOnlyMyEntities)),
conversationsFolders: browserStorage
.getConversationsFolders()
.getConversationsFolders(undefined, true)
.pipe(map(filterOnlyMyEntities)),
migratedConversationIds: BrowserStorage.getMigratedEntityIds(
MigrationStorageKeys.MigratedConversationIds,
Expand Down Expand Up @@ -769,14 +769,15 @@ const migrateConversationsEpic: AppEpic = (action$, state$) => {
const { path } = getPathToFolderById(
conversationsFolders,
conv.folderId,
true,
);
const newName = conv.name.replace(notAllowedSymbolsRegex, '');

return {
...conv,
id: constructPath(...[path, newName]),
name: newName,
folderId: path.replace(notAllowedSymbolsRegex, ''),
folderId: path,
};
}); // to send conversation with proper parentPath and lastActivityDate order

Expand Down
10 changes: 7 additions & 3 deletions apps/chat/src/store/prompts/prompts.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ const migratePromptsEpic: AppEpic = (action$, state$) => {
forkJoin({
prompts: browserStorage.getPrompts().pipe(map(filterOnlyMyEntities)),
promptsFolders: browserStorage
.getPromptsFolders()
.getPromptsFolders(undefined, true)
.pipe(map(filterOnlyMyEntities)),
migratedPromptIds: BrowserStorage.getMigratedEntityIds(
MigrationStorageKeys.MigratedPromptIds,
Expand Down Expand Up @@ -453,14 +453,18 @@ const migratePromptsEpic: AppEpic = (action$, state$) => {
}

const preparedPrompts: Prompt[] = notMigratedPrompts.map((prompt) => {
const { path } = getPathToFolderById(promptsFolders, prompt.folderId);
const { path } = getPathToFolderById(
promptsFolders,
prompt.folderId,
true,
);
const newName = prompt.name.replace(notAllowedSymbolsRegex, '');

return {
...prompt,
id: constructPath(...[path, newName]),
name: newName,
folderId: path.replace(notAllowedSymbolsRegex, ''),
folderId: path,
};
}); // to send prompts with proper parentPath
let migratedPromptsCount = 0;
Expand Down
10 changes: 6 additions & 4 deletions apps/chat/src/utils/app/data/storages/browser-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,25 @@ export class BrowserStorage implements DialStorage {
return BrowserStorage.setData(UIStorageKeys.Prompts, prompts);
}

getConversationsFolders(path?: string) {
getConversationsFolders(path?: string, recursive?: boolean) {
return BrowserStorage.getData(UIStorageKeys.Folders, []).pipe(
map((folders: FolderInterface[]) => {
return folders.filter(
(folder) =>
folder.type === FolderType.Chat && folder.folderId === path,
folder.type === FolderType.Chat &&
(recursive || folder.folderId === path),
);
}),
);
}

getPromptsFolders(path?: string) {
getPromptsFolders(path?: string, recursive?: boolean) {
return BrowserStorage.getData(UIStorageKeys.Folders, []).pipe(
map((folders: FolderInterface[]) => {
return folders.filter(
(folder) =>
folder.type === FolderType.Prompt && folder.folderId === path,
folder.type === FolderType.Prompt &&
(recursive || folder.folderId === path),
);
}),
);
Expand Down
9 changes: 8 additions & 1 deletion apps/chat/src/utils/app/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { FolderInterface, FolderType } from '@/src/types/folder';
import { Prompt, PromptInfo } from '@/src/types/prompt';
import { EntityFilters } from '@/src/types/search';

import { DEFAULT_FOLDER_NAME } from '@/src/constants/default-settings';

import escapeStringRegexp from 'escape-string-regexp';

export const getFoldersDepth = (
Expand Down Expand Up @@ -193,13 +195,18 @@ export const getFolderIdByPath = (path: string, folders: FolderInterface[]) => {
export const getPathToFolderById = (
folders: FolderInterface[],
starterId: string | undefined,
removeNotAllowedSymbols = false,
) => {
const path: string[] = [];
const createPath = (folderId: string) => {
const folder = folders.find((folder) => folder.id === folderId);
if (!folder) return;

path.unshift(folder.name);
path.unshift(
removeNotAllowedSymbols
? folder.name.replace(notAllowedSymbolsRegex, '') || DEFAULT_FOLDER_NAME
: folder.name,
);

if (folder.folderId) {
createPath(folder.folderId);
Expand Down

0 comments on commit 9733bfb

Please sign in to comment.