From cdf157c72bfe2d00b979c75113bd4f836816c10f Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Tue, 11 Feb 2025 16:51:42 +0100 Subject: [PATCH] wip --- .../doc-editor/components/AIPdfButton.tsx | 38 ++++++ .../doc-editor/components/Ai/AiButton.tsx | 129 ++++++++++++------ .../components/BlockNoteToolbar.tsx | 24 ++-- 3 files changed, 141 insertions(+), 50 deletions(-) create mode 100644 src/frontend/apps/impress/src/features/docs/doc-editor/components/AIPdfButton.tsx diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/components/AIPdfButton.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/components/AIPdfButton.tsx new file mode 100644 index 00000000..05fa2217 --- /dev/null +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/components/AIPdfButton.tsx @@ -0,0 +1,38 @@ +import { + useBlockNoteEditor, + useComponentsContext, + useSelectedBlocks, +} from '@blocknote/react'; + +import { Text } from '@/components'; + +export const AIPdfButton = () => { + const editor = useBlockNoteEditor(); + const Components = useComponentsContext(); + const selectedBlocks = useSelectedBlocks(editor); + if (!Components) { + return null; + } + + const show = selectedBlocks.length === 1 && selectedBlocks[0].type === 'file'; + if (!show) { + return null; + } + + return ( + + auto_awesome + + } + onClick={() => { + console.log('selectedBlocks', selectedBlocks); + }} + /> + ); +}; diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/components/Ai/AiButton.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/components/Ai/AiButton.tsx index 6d87781a..f7fc22cb 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/components/Ai/AiButton.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/components/Ai/AiButton.tsx @@ -1,7 +1,6 @@ -import { Button, Input, Loader, TextArea } from '@openfun/cunningham-react'; +import { Button, Input, Loader } from '@openfun/cunningham-react'; import { marked } from 'marked'; import { useState } from 'react'; -import { ButtonProps } from 'react-aria-components'; import styled from 'styled-components'; import { fetchAPI } from '@/api'; @@ -49,7 +48,7 @@ export const SuggestionButton = styled.button` `; export const AiButton = ({ doc }: { doc: Doc }) => { - const [isOpen, setIsOpen] = useState(true); + const [isOpen, setIsOpen] = useState(false); return ( <> @@ -65,7 +64,7 @@ export const AiButton = ({ doc }: { doc: Doc }) => { `} > setIsOpen(true)} /> @@ -90,22 +89,16 @@ const AiChat = (props: { isOpen: boolean; onClose: () => void; doc: Doc }) => { return null; } - const submit = async (e: React.FormEvent) => { - e.preventDefault(); - + const newPrompt = async (prompt: string) => { if (!editor) { return; } setIsLoading(true); setMessages([...messages, { role: 'user', content: prompt }]); - setPrompt(''); // Clear the prompt after submitting the form const editorContentFormatted = await editor.blocksToMarkdownLossy(); - console.log('submit', e, prompt); - console.log('editorContentFormatted', editorContentFormatted); - const response = await fetchAPI(`documents/${props.doc.id}/ai-proxy/`, { method: 'POST', body: JSON.stringify({ @@ -116,7 +109,7 @@ const AiChat = (props: { isOpen: boolean; onClose: () => void; doc: Doc }) => { }), }); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const data = await response.json(); + const data = (await response.json()) as string; console.log('response', data); setMessages((messages) => [ @@ -126,6 +119,12 @@ const AiChat = (props: { isOpen: boolean; onClose: () => void; doc: Doc }) => { setIsLoading(false); }; + const submit = async (e: React.FormEvent) => { + e.preventDefault(); + setPrompt(''); // Clear the prompt after submitting the form + await newPrompt(prompt); + }; + return ( void; doc: Doc }) => { Suggestions - + + void newPrompt( + 'Resume ce document sous forme textuelle uniquement', + ) + } + > description Résumer cette page - + + void newPrompt('Quel est le sujet principal de ce document ?') + } + > help_center - Poser des questions surcette page + Poser des questions sur cette page @@ -201,37 +210,16 @@ const AiChat = (props: { isOpen: boolean; onClose: () => void; doc: Doc }) => { mask-image: linear-gradient(black calc(100% - 32px), transparent calc(100% - 4px)); padding-bottom: 32px; `} + aria-live="polite" > - {messages.map((message, index) => { - if (message.role === 'user') { - return ( - - {message.content} - - ); - } - return ( - - ); - })} + {messages.map((message, index) => ( + + ))} {(isLoading || false) && ( - Thinking ... + Albert réfléchit ... )} @@ -251,3 +239,62 @@ const AiChat = (props: { isOpen: boolean; onClose: () => void; doc: Doc }) => { ); }; + +const Message = ({ message }: { message: Message }) => { + return ( + + + {message.role === 'user' ? ( + + VD + + ) : ( + + )} + + {message.role === 'user' ? 'Vous' : 'Albert'} + + + + + ); +}; diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteToolbar.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteToolbar.tsx index b9c66473..06454d36 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteToolbar.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteToolbar.tsx @@ -8,21 +8,27 @@ import { import React, { useCallback } from 'react'; import { AIGroupButton } from './AIButton'; +import { AIPdfButton } from './AIPdfButton'; import { MarkdownButton } from './MarkdownButton'; export const BlockNoteToolbar = () => { const formattingToolbar = useCallback( - ({ blockTypeSelectItems }: FormattingToolbarProps) => ( - - {getFormattingToolbarItems(blockTypeSelectItems)} + ({ blockTypeSelectItems }: FormattingToolbarProps) => { + console.log('formattingToolbar', blockTypeSelectItems); + return ( + + {getFormattingToolbarItems(blockTypeSelectItems)} - {/* Extra button to do some AI powered actions */} - + {/* Extra button to do some AI powered actions */} + - {/* Extra button to convert from markdown to json */} - - - ), + {/* Extra button to convert from markdown to json */} + + + + + ); + }, [], );