Skip to content

Commit

Permalink
fix(chat): fix folders unpublishing and publication folders uploading…
Browse files Browse the repository at this point in the history
… (Issues #318, #1482) (#1500)
  • Loading branch information
Alexander-Kezik authored Jun 5, 2024
1 parent 2fc6197 commit f9fd30c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 54 deletions.
28 changes: 14 additions & 14 deletions apps/chat/src/components/Chat/Publish/PublicationItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface Props {
files: DialFile[];
containerClassNames?: string;
collapsibleSectionClassNames?: string;
publishAction: PublishActions;
}

export function PublicationItemsList({
Expand All @@ -42,6 +43,7 @@ export function PublicationItemsList({
files,
containerClassNames,
collapsibleSectionClassNames,
publishAction,
}: Props) {
const { t } = useTranslation(Translation.Chat);

Expand Down Expand Up @@ -70,7 +72,7 @@ export function PublicationItemsList({
<ConversationPublicationResources
rootFolder={entity}
resources={entities.map((entity) => ({
action: PublishActions.ADD,
action: publishAction,
sourceUrl: entity.id,
targetUrl: constructPath(
ApiKeys.Conversations,
Expand Down Expand Up @@ -115,19 +117,17 @@ export function PublicationItemsList({
) : (
<PromptPublicationResources
rootFolder={entity}
resources={[
{
action: PublishActions.ADD,
sourceUrl: entity.id,
targetUrl: constructPath(
ApiKeys.Prompts,
'public',
path,
entity.name,
),
reviewUrl: entity.id,
},
]}
resources={entities.map((entity) => ({
action: publishAction,
sourceUrl: entity.id,
targetUrl: constructPath(
ApiKeys.Conversations,
'public',
path,
splitEntityId(entity.id).name,
),
reviewUrl: entity.id,
}))}
forViewOnly
/>
)}
Expand Down
3 changes: 2 additions & 1 deletion apps/chat/src/components/Chat/Publish/PublishWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ApiUtils } from '@/src/utils/server/api';
import { Conversation } from '@/src/types/chat';
import { FeatureType, ShareEntity } from '@/src/types/common';
import { ModalState } from '@/src/types/modal';
import { TargetAudienceFilter } from '@/src/types/publication';
import { PublishActions, TargetAudienceFilter } from '@/src/types/publication';
import { SharingType } from '@/src/types/share';
import { Translation } from '@/src/types/translation';

Expand Down Expand Up @@ -412,6 +412,7 @@ export function PublishModal({
path={path}
files={files}
containerClassNames="px-5 py-4"
publishAction={PublishActions.ADD}
/>
</div>

Expand Down
33 changes: 30 additions & 3 deletions apps/chat/src/components/Chat/Publish/UnpublishModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { useTranslation } from 'next-i18next';

import { getFolderIdFromEntityId } from '@/src/utils/app/folders';
import { getAttachments } from '@/src/utils/app/share';
import { ApiUtils } from '@/src/utils/server/api';

import { Conversation } from '@/src/types/chat';
import { Entity } from '@/src/types/common';
import { ModalState } from '@/src/types/modal';
import { PublishActions } from '@/src/types/publication';
import { SharingType } from '@/src/types/share';
import { Translation } from '@/src/types/translation';

Expand All @@ -16,6 +19,9 @@ import { PublicationActions } from '@/src/store/publication/publication.reducers
import Modal from '../../Common/Modal';
import { PublicationItemsList } from './PublicationItemsList';

import compact from 'lodash-es/compact';
import flatMapDeep from 'lodash-es/flatMapDeep';

interface Props {
subtitle: string;
entity: Entity;
Expand Down Expand Up @@ -56,21 +62,41 @@ export function UnpublishModal({
e.preventDefault();
e.stopPropagation();

const mappedFiles =
type === SharingType.Conversation ||
type === SharingType.ConversationFolder
? (entities as Conversation[])
.filter((c) =>
(c.playback?.messagesStack || c.messages).some(
(m) => m.custom_content?.attachments,
),
)
.flatMap((c) => {
const urls = compact(
flatMapDeep(c.playback?.messagesStack || c.messages, (m) =>
m.custom_content?.attachments?.map((a) => a.url),
),
);

return urls.map((oldUrl) => ApiUtils.decodeApiUrl(oldUrl));
})
: [];

dispatch(
PublicationActions.deletePublication({
targetFolder: `${getFolderIdFromEntityId(entity.id).split('/').slice(1).join('/')}/`,
resources: [
...entities.map((entity) => ({ targetUrl: entity.id })),
...files.map((f) => ({
targetUrl: f.id,
...mappedFiles.map((url) => ({
targetUrl: url,
})),
],
}),
);

onClose();
},
[dispatch, entities, entity.id, files, onClose],
[dispatch, entities, entity.id, onClose, type],
);

return (
Expand Down Expand Up @@ -98,6 +124,7 @@ export function UnpublishModal({
entities={entities}
path={''}
files={files}
publishAction={PublishActions.DELETE}
/>
</div>
<div className="flex justify-end gap-3 px-6 pt-4">
Expand Down
22 changes: 16 additions & 6 deletions apps/chat/src/components/Folder/Folder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { Translation } from '@/src/types/translation';
import { ConversationsActions } from '@/src/store/conversations/conversations.reducers';
import { FilesActions } from '@/src/store/files/files.reducers';
import { useAppDispatch, useAppSelector } from '@/src/store/hooks';
import { PromptsActions } from '@/src/store/prompts/prompts.reducers';
import { SettingsSelectors } from '@/src/store/settings/settings.reducers';
import { ShareActions } from '@/src/store/share/share.reducers';
import { UIActions } from '@/src/store/ui/ui.reducers';
Expand Down Expand Up @@ -170,6 +171,8 @@ const Folder = <T extends ConversationInfo | PromptInfo | DialFile>({
const dragDropElement = useRef<HTMLDivElement>(null);
const [isPublishing, setIsPublishing] = useState(false);
const [isUnpublishing, setIsUnpublishing] = useState(false);
const [isUploadedForUnpublishing, setIsUploadedForUnpublishing] =
useState(false);
const [isUnshareConfirmOpened, setIsUnshareConfirmOpened] = useState(false);
const isPublishingEnabled = useAppSelector((state) =>
SettingsSelectors.isPublishingEnabled(state, featureType),
Expand Down Expand Up @@ -273,21 +276,28 @@ const Folder = <T extends ConversationInfo | PromptInfo | DialFile>({
(e) => {
e.stopPropagation();

if (
featureType === FeatureType.Chat &&
(!allChildItems.length ||
!allChildItems.every((item) => 'messages' in item))
) {
if (featureType === FeatureType.Chat && !isUploadedForUnpublishing) {
dispatch(
ConversationsActions.uploadConversationsWithContentRecursive({
path: currentFolder.id,
}),
);
} else if (
featureType === FeatureType.Prompt &&
!isUploadedForUnpublishing
) {
dispatch(
PromptsActions.uploadPromptsWithFoldersRecursive({
path: currentFolder.id,
noLoader: true,
}),
);
}

setIsUploadedForUnpublishing(true);
setIsUnpublishing(true);
},
[allChildItems, currentFolder.id, dispatch, featureType],
[currentFolder.id, dispatch, featureType, isUploadedForUnpublishing],
);

const handleCloseUnpublishModal = useCallback(() => {
Expand Down
72 changes: 42 additions & 30 deletions apps/chat/src/store/publication/publication.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
catchError,
concat,
filter,
from,
iif,
map,
mergeAll,
mergeMap,
of,
switchMap,
Expand All @@ -21,6 +23,7 @@ import {
getFolderFromId,
getFolderIdFromEntityId,
getParentFolderIdsFromEntityId,
getRootFolderIdFromEntityId,
splitEntityId,
} from '@/src/utils/app/folders';
import {
Expand Down Expand Up @@ -139,18 +142,37 @@ const uploadPublicationEpic: AppEpic = (action$) =>
const promptResources = publication.resources.filter((r) =>
isPromptId(r.targetUrl),
);
const unpublishResources = publication.resources.filter(
(r) => r.action === PublishActions.DELETE,
);

if (unpublishResources.length) {
const rootFolderPaths = uniq(
unpublishResources.map((r) =>
getRootFolderIdFromEntityId(r.reviewUrl),
),
);

actions.push(
from(
rootFolderPaths.map((path) => {
const actionCreator = isConversationId(path)
? ConversationsActions.uploadConversationsWithFoldersRecursive
: PromptsActions.uploadPromptsWithFoldersRecursive;

return of(actionCreator({ noLoader: true, path }));
}),
).pipe(mergeAll()),
);
}

if (promptResources.length) {
const promptPaths = uniq(
promptResources.flatMap((resource) => {
const url = resource.reviewUrl
? resource.reviewUrl
: resource.targetUrl;

return getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(url),
).filter((id) => id !== url);
}),
promptResources.flatMap((resource) =>
getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(resource.reviewUrl),
).filter((id) => id !== resource.reviewUrl),
),
);

actions.push(
Expand Down Expand Up @@ -188,15 +210,11 @@ const uploadPublicationEpic: AppEpic = (action$) =>
);
if (conversationResources.length) {
const conversationPaths = uniq(
conversationResources.flatMap((resource) => {
const url = resource.reviewUrl
? resource.reviewUrl
: resource.targetUrl;

return getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(url),
).filter((id) => id !== url);
}),
conversationResources.flatMap((resource) =>
getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(resource.reviewUrl),
).filter((id) => id !== resource.reviewUrl),
),
);

actions.push(
Expand Down Expand Up @@ -236,15 +254,11 @@ const uploadPublicationEpic: AppEpic = (action$) =>

if (fileResources.length) {
const filePaths = uniq(
fileResources.flatMap((resource) => {
const url = resource.reviewUrl
? resource.reviewUrl
: resource.targetUrl;

return getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(url),
).filter((id) => id !== url);
}),
fileResources.flatMap((resource) =>
getParentFolderIdsFromEntityId(
getFolderIdFromEntityId(resource.reviewUrl),
).filter((id) => id !== resource.reviewUrl),
),
);

actions.push(
Expand Down Expand Up @@ -356,9 +370,7 @@ const uploadPublishedWithMeItemsEpic: AppEpic = (action$, state$) =>

if (pathsToUpload.length) {
const rootFolderIds = uniq(
pathsToUpload.map((path) =>
path.split('/').slice(0, 3).join('/'),
),
pathsToUpload.map((path) => getRootFolderIdFromEntityId(path)),
);

rootFolderIds.forEach((path) =>
Expand Down
3 changes: 3 additions & 0 deletions apps/chat/src/utils/app/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,6 @@ export const updateMovedEntityId = (

export const getFolderIdFromEntityId = (id: string) =>
id.split('/').slice(0, -1).join('/');

export const getRootFolderIdFromEntityId = (id: string) =>
id.split('/').slice(0, 3).join('/');

0 comments on commit f9fd30c

Please sign in to comment.