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)
  • Loading branch information
Alexander-Kezik committed Jun 5, 2024
1 parent 2fc6197 commit cc8f99c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 51 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
2 changes: 2 additions & 0 deletions apps/chat/src/components/Chat/Publish/UnpublishModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getAttachments } from '@/src/utils/app/share';

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 Down Expand Up @@ -98,6 +99,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
79 changes: 49 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,44 @@ 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) =>
isConversationId(path)
? of(
ConversationsActions.uploadConversationsWithFoldersRecursive(
{ noLoader: true, path: path },
),
)
: of(
PromptsActions.uploadPromptsWithFoldersRecursive({
noLoader: true,
path: 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 +217,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 +261,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 +377,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 cc8f99c

Please sign in to comment.