Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/@types/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2696,7 +2696,8 @@
"pageNameIsEmpty": "The page name is empty, please try another one"
},
"visitOurWebsite": "Visit our official website",
"addMessagesToPage": "Add messages to page"
"addMessagesToPage": "Add messages to page",
"addMessagesToPageDisabled": "No messages available"
},
"globalComment": {
"comments": "Comments",
Expand Down
23 changes: 22 additions & 1 deletion src/application/services/js-services/http/http_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import dayjs from 'dayjs';
import { omit } from 'lodash-es';
import { nanoid } from 'nanoid';
import { notify } from '@/components/_shared/notify';
import { RepeatedChatMessage } from '@appflowyinc/ai-chat/dist/types';

export * from './gotrue';

Expand Down Expand Up @@ -1844,4 +1845,24 @@ export async function searchWorkspace(workspaceId: string, query: string) {
}

return Promise.reject(res?.data);
}
}

export async function getChatMessages(workspaceId: string, chatId: string, limit?: number | undefined) {
const url = `/api/chat/${workspaceId}/${chatId}/message`;

const response = await axiosInstance?.get<{
code: number;
data?: RepeatedChatMessage;
message: string;
}>(url, {
params: { limit: limit},
});

const data = response?.data;

if(data?.code === 0 && data.data) {
return data.data;
}

return Promise.reject(data);
}
9 changes: 9 additions & 0 deletions src/application/services/js-services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
YjsEditorKey,
} from '@/application/types';
import { applyYDoc } from '@/application/ydoc/apply';
import { RepeatedChatMessage } from '@appflowyinc/ai-chat/dist/types';
import { nanoid } from 'nanoid';
import * as Y from 'yjs';

Expand Down Expand Up @@ -639,4 +640,12 @@ export class AFClientService implements AFService {
searchWorkspace(workspaceId: string, query: string) {
return APIService.searchWorkspace(workspaceId, query);
}

async getChatMessages(
workspaceId: string,
chatId: string,
limit?: number | undefined,
): Promise<RepeatedChatMessage> {
return APIService.getChatMessages(workspaceId, chatId, limit);
}
}
12 changes: 11 additions & 1 deletion src/application/services/services.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ import {
UploadTemplatePayload,
} from '@/application/template.type';
import { AxiosInstance } from 'axios';
import { RepeatedChatMessage } from '@appflowyinc/ai-chat/dist/types';

export type AFService = PublishService & AppService & WorkspaceService & TemplateService & QuickNoteService & {
export type AFService = PublishService & AppService & WorkspaceService & TemplateService & QuickNoteService & AIChatService & {
getClientId: () => string;
getAxiosInstance: () => AxiosInstance | null;
};
Expand Down Expand Up @@ -109,6 +110,7 @@ export interface AppService {
restoreFromTrash: (workspaceId: string, viewId?: string) => Promise<void>;
movePage: (workspaceId: string, viewId: string, parentId: string, prevViewId?: string) => Promise<void>;
uploadFile: (workspaceId: string, viewId: string, file: File, onProgress?: (progress: number) => void) => Promise<string>;

}

export interface QuickNoteService {
Expand Down Expand Up @@ -176,3 +178,11 @@ export interface PublishService {
duplicatePublishView: (params: DuplicatePublishView) => Promise<string>;

}

export interface AIChatService{
getChatMessages: (
workspaceId: string,
chatId: string,
limit?: number | undefined,
) => Promise<RepeatedChatMessage>;
}
84 changes: 55 additions & 29 deletions src/components/app/header/MoreActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@ import { ViewLayout } from '@/application/types';
import { ReactComponent as MoreIcon } from '@/assets/more.svg';
import { Popover } from '@/components/_shared/popover';
import { useAIChatContext } from '@/components/ai-chat/AIChatProvider';
import { useAppView } from '@/components/app/app.hooks';
import { useAppView, useCurrentWorkspaceId } from '@/components/app/app.hooks';
import DocumentInfo from '@/components/app/header/DocumentInfo';
import { Button, Divider, IconButton } from '@mui/material';
import React from 'react';
import { Button, Divider, IconButton, Tooltip } from '@mui/material';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import MoreActionsContent from './MoreActionsContent';
import { ReactComponent as DocForwardIcon } from '@/assets/doc-forward.svg';
import { useService } from '@/components/main/app.hooks';

function MoreActions({
viewId,
onDeleted,
}: {
viewId: string;
onDeleted?: () => void;
}) {
const {
selectionMode,
onOpenSelectionMode,
} = useAIChatContext();
function MoreActions({ viewId, onDeleted }: { viewId: string; onDeleted?: () => void }) {
const workspaceId = useCurrentWorkspaceId();
const service = useService();
const { selectionMode, onOpenSelectionMode } = useAIChatContext();
const [hasMessages, setHasMessages] = useState(false);

const view = useAppView(viewId);
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
Expand All @@ -36,7 +31,50 @@ function MoreActions({

const { t } = useTranslation();

if(view?.layout === ViewLayout.AIChat && selectionMode) {
const handleFetchChatMessages = useCallback(async () => {
if(!workspaceId || !service) {
return;
}

try {
const messages = await service.getChatMessages(workspaceId, viewId);

setHasMessages(messages.messages.length > 0);
} catch {
// do nothing
}
}, [workspaceId, service, viewId]);

useEffect(() => {
void handleFetchChatMessages();
}, [handleFetchChatMessages]);

const ChatOptions = useMemo(() => {
return view?.layout === ViewLayout.AIChat ? (
<>
<Tooltip title={hasMessages ? '' : t('web.addMessagesToPageDisabled')} placement='top' >
<div>
<Button
size={'small'}
className={'justify-start px-3 py-1'}
color={'inherit'}
disabled={!hasMessages}
onClick={() => {
onOpenSelectionMode();
handleClose();
}}
startIcon={<DocForwardIcon />}
>
{t('web.addMessagesToPage')}
</Button>
</div>
</Tooltip>
<Divider />
</>
) : null;
}, [view?.layout, hasMessages, t, onOpenSelectionMode]);

if (view?.layout === ViewLayout.AIChat && selectionMode) {
return null;
}

Expand Down Expand Up @@ -70,19 +108,7 @@ function MoreActions({
},
}}
>
{view?.layout === ViewLayout.AIChat ? <>
<Button
size={'small'}
className={'px-3 py-1 justify-start '}
color={'inherit'}
onClick={() => {
onOpenSelectionMode();
handleClose();
}}
startIcon={<DocForwardIcon />}
>{t('web.addMessagesToPage')}</Button>
<Divider />
</> : null}
{ChatOptions}
<MoreActionsContent
itemClicked={() => {
handleClose();
Expand All @@ -107,4 +133,4 @@ function MoreActions({
);
}

export default MoreActions;
export default MoreActions;
Loading