-
Notifications
You must be signed in to change notification settings - Fork 453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing assistant v2 #487
base: main
Are you sure you want to change the base?
Writing assistant v2 #487
Changes from 8 commits
7617aad
c9863e8
868e425
cd761f3
1258003
fa837e7
5690753
f919081
5a861f8
c11f17e
f68dc51
03ce49a
a84c70e
94320ca
62c6ecb
4dc0047
7f2966a
a761a22
fb7f0a8
2c71e4f
79b5523
8b693cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'react' | ||
import { useCompletion } from 'ai/react' | ||
import { ArrowUp, Sparkles, ShrinkIcon, ExpandIcon, Play } from 'lucide-react' | ||
import { Button } from '@/components/ui/button' | ||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' | ||
|
||
interface AiEditMenuProps { | ||
selectedText: string | ||
onEdit: (newText: string) => void | ||
} | ||
|
||
const AiEditMenu = ({ selectedText, onEdit }: AiEditMenuProps) => { | ||
const { complete } = useCompletion({ | ||
api: '/api/generate', | ||
}) | ||
|
||
const handleAction = async (action: string) => { | ||
const prompt = `${action} the following text: ${selectedText}` | ||
const completion = await complete(prompt) | ||
if (completion) { | ||
onEdit(completion) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: no error handling for failed API calls or rate limits. Add try/catch and show error state to user |
||
|
||
return ( | ||
<Card className="w-[400px] border-gray-800 bg-[#1a1b1e]"> | ||
<CardHeader className="flex flex-row items-center justify-between pb-2"> | ||
<div className="flex items-center gap-2"> | ||
<Sparkles className="size-5 text-purple-500" /> | ||
<CardTitle className="text-sm text-gray-300">Ask AI to edit or generate...</CardTitle> | ||
</div> | ||
<Button size="icon" variant="ghost" className="text-purple-500"> | ||
<ArrowUp className="size-5" /> | ||
</Button> | ||
</CardHeader> | ||
<CardContent className="space-y-4"> | ||
<div className="space-y-1"> | ||
<p className="text-sm text-gray-400">Edit or review selection</p> | ||
<div className="space-y-2"> | ||
<Button | ||
variant="ghost" | ||
className="w-full justify-start text-gray-300 hover:bg-gray-800" | ||
onClick={() => handleAction('improve')} | ||
> | ||
<Sparkles className="mr-2 size-5 text-purple-500" /> | ||
Improve writing | ||
</Button> | ||
<Button | ||
variant="ghost" | ||
className="w-full justify-start text-gray-300 hover:bg-gray-800" | ||
onClick={() => handleAction('fix grammar in')} | ||
> | ||
Fix grammar | ||
</Button> | ||
<Button | ||
variant="ghost" | ||
className="w-full justify-start text-gray-300 hover:bg-gray-800" | ||
onClick={() => handleAction('make shorter')} | ||
> | ||
<ShrinkIcon className="mr-2 size-5 text-purple-500" /> | ||
Make shorter | ||
</Button> | ||
<Button | ||
variant="ghost" | ||
className="w-full justify-start text-gray-300 hover:bg-gray-800" | ||
onClick={() => handleAction('make longer')} | ||
> | ||
<ExpandIcon className="mr-2 size-5 text-purple-500" /> | ||
Make longer | ||
</Button> | ||
</div> | ||
</div> | ||
<div className="space-y-1"> | ||
<p className="text-sm text-gray-400">Use AI to do more</p> | ||
<Button variant="ghost" className="w-full justify-start text-gray-300 hover:bg-gray-800"> | ||
<Play className="mr-2 size-5 text-purple-500" /> | ||
Continue writing | ||
</Button> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: 'Continue writing' button has no onClick handler - appears to be non-functional |
||
</div> | ||
</CardContent> | ||
</Card> | ||
) | ||
} | ||
|
||
export default AiEditMenu |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { Editor } from '@tiptap/react' | ||
|
||
interface DocumentStatsProps { | ||
editor: Editor | null | ||
} | ||
|
||
const DocumentStats: React.FC<DocumentStatsProps> = ({ editor }) => { | ||
const [show, setShow] = useState(false) | ||
|
||
useEffect(() => { | ||
const initDocumentStats = async () => { | ||
const showStats = await window.electronStore.getDocumentStats() | ||
setShow(showStats) | ||
} | ||
|
||
initDocumentStats() | ||
|
||
const handleDocStatsChange = (event: Electron.IpcRendererEvent, value: boolean) => { | ||
setShow(value) | ||
} | ||
|
||
window.ipcRenderer.on('show-doc-stats-changed', handleDocStatsChange) | ||
}, []) | ||
Comment on lines
+11
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: missing cleanup function to remove IPC event listener on unmount |
||
|
||
if (!editor || !show) return null | ||
|
||
return ( | ||
<div className="absolute bottom-2 right-2 flex gap-4 text-sm text-gray-500"> | ||
<div>Characters: {editor.storage.characterCount.characters()}</div> | ||
<div>Words: {editor.storage.characterCount.words()}</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default DocumentStats |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
/* eslint-disable react/button-has-type */ | ||
import React, { useEffect, useState } from 'react' | ||
import { EditorContent } from '@tiptap/react' | ||
import InEditorBacklinkSuggestionsDisplay from './BacklinkSuggestionsDisplay' | ||
import { EditorContent, BubbleMenu } from '@tiptap/react' | ||
import EditorContextMenu from './EditorContextMenu' | ||
import SearchBar from './Search/SearchBar' | ||
import { useFileContext } from '@/contexts/FileContext' | ||
import { useContentContext } from '@/contexts/ContentContext' | ||
import DocumentStats from './DocumentStats' | ||
import AiEditMenu from './AIEdit' | ||
|
||
const EditorManager: React.FC = () => { | ||
const [showSearchBar, setShowSearchBar] = useState(false) | ||
const [contextMenuVisible, setContextMenuVisible] = useState(false) | ||
const [menuPosition, setMenuPosition] = useState({ x: 0, y: 0 }) | ||
const [editorFlex, setEditorFlex] = useState(true) | ||
|
||
const { editor, suggestionsState, vaultFilesFlattened } = useFileContext() | ||
const [showDocumentStats, setShowDocumentStats] = useState(false) | ||
const { openContent } = useContentContext() | ||
const [showAIPopup, setShowAIPopup] = useState(false) | ||
const { editor } = useFileContext() | ||
|
||
const handleContextMenu = (event: React.MouseEvent<HTMLDivElement>) => { | ||
event.preventDefault() | ||
|
@@ -29,15 +28,6 @@ const EditorManager: React.FC = () => { | |
if (contextMenuVisible) setContextMenuVisible(false) | ||
} | ||
|
||
const handleClick = (event: React.MouseEvent<HTMLDivElement>) => { | ||
const { target } = event | ||
if (target instanceof HTMLElement && target.getAttribute('data-backlink') === 'true') { | ||
event.preventDefault() | ||
const backlinkPath = target.textContent | ||
if (backlinkPath) openContent(backlinkPath) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
const initEditorContentCenter = async () => { | ||
const isCenter = await window.electronStore.getEditorFlexCenter() | ||
|
@@ -52,26 +42,40 @@ const EditorManager: React.FC = () => { | |
window.ipcRenderer.on('editor-flex-center-changed', handleEditorChange) | ||
}, []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: missing cleanup for ipcRenderer event listener |
||
|
||
useEffect(() => { | ||
const initDocumentStats = async () => { | ||
const showStats = await window.electronStore.getDocumentStats() | ||
setShowDocumentStats(showStats) | ||
} | ||
|
||
initDocumentStats() | ||
|
||
const handleDocStatsChange = (event: Electron.IpcRendererEvent, value: boolean) => { | ||
setShowDocumentStats(value) | ||
} | ||
|
||
window.ipcRenderer.on('show-doc-stats-changed', handleDocStatsChange) | ||
}, []) | ||
|
||
return ( | ||
<div | ||
className="relative size-full cursor-text overflow-hidden bg-dark-gray-c-eleven py-4 text-slate-400 opacity-80" | ||
onClick={() => editor?.commands.focus()} | ||
> | ||
{editor && ( | ||
<BubbleMenu | ||
className="flex gap-2 rounded-lg border border-gray-700 bg-dark-gray-c-eleven p-2 shadow-lg" | ||
editor={editor} | ||
tippyOptions={{ | ||
duration: 1000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: 1000ms duration for bubble menu animation may feel slow for frequent interactions |
||
placement: 'auto', | ||
offset: [0, 10], | ||
onHidden: () => { | ||
setShowAIPopup(false) | ||
}, | ||
}} | ||
> | ||
<div | ||
onMouseDown={(e) => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
}} | ||
> | ||
{showAIPopup ? ( | ||
<AiEditMenu selectedText={editor.getText()} onEdit={() => {}} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: onEdit prop is passed an empty function - AI edits will not be applied to the editor |
||
) : ( | ||
<button onClick={() => setShowAIPopup(true)} className="rounded p-2 hover:bg-gray-700"> | ||
Ask AI | ||
</button> | ||
)} | ||
</div> | ||
</BubbleMenu> | ||
)} | ||
<SearchBar editor={editor} showSearch={showSearchBar} setShowSearch={setShowSearchBar} /> | ||
{contextMenuVisible && ( | ||
<EditorContextMenu | ||
|
@@ -82,33 +86,19 @@ const EditorManager: React.FC = () => { | |
/> | ||
)} | ||
|
||
<div | ||
className={`relative h-full ${editorFlex ? 'flex justify-center py-4 pl-4' : ''} ${showDocumentStats ? 'pb-3' : ''}`} | ||
> | ||
<div className={`relative h-full ${editorFlex ? 'flex justify-center py-4 pl-4' : ''}`}> | ||
<div className="relative size-full overflow-y-auto"> | ||
<EditorContent | ||
className={`relative size-full bg-dark-gray-c-eleven ${editorFlex ? 'max-w-xl' : ''}`} | ||
style={{ | ||
wordBreak: 'break-word', | ||
}} | ||
onContextMenu={handleContextMenu} | ||
onClick={handleClick} | ||
editor={editor} | ||
/> | ||
</div> | ||
</div> | ||
{suggestionsState && ( | ||
<InEditorBacklinkSuggestionsDisplay | ||
suggestionsState={suggestionsState} | ||
suggestions={vaultFilesFlattened.map((file) => file.relativePath)} | ||
/> | ||
)} | ||
{editor && showDocumentStats && ( | ||
<div className="absolute bottom-2 right-2 flex gap-4 text-sm text-gray-500"> | ||
<div>Characters: {editor.storage.characterCount.characters()}</div> | ||
<div>Words: {editor.storage.characterCount.words()}</div> | ||
</div> | ||
)} | ||
<DocumentStats editor={editor} /> | ||
</div> | ||
) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: missing loading state while waiting for completion. User has no feedback during API call