-
Notifications
You must be signed in to change notification settings - Fork 187
Add edit functionality to KnowledgeSuggestionDetailPage #1163
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ca205d5
Add edit functionality to KnowledgeSuggestionDetailPage
devin-ai-integration[bot] d22a284
Fix form submission to use standard Next.js server action pattern
devin-ai-integration[bot] e6a9eda
Fix trailing whitespace issues
devin-ai-integration[bot] e353ef5
Fix trailing whitespace in EditableContent.tsx
devin-ai-integration[bot] 55ddb37
Refactor EditableContent to use Render Hooks Pattern and fix display …
devin-ai-integration[bot] 101c153
Fix whitespace issues to resolve lint errors
devin-ai-integration[bot] 18dbdcc
Revert "Fix whitespace issues to resolve lint errors"
NoritakaIkeda 1085600
Revert "Refactor EditableContent to use Render Hooks Pattern and fix …
NoritakaIkeda b007409
🔧 Add a placeholder to the EditableContent component and update the d…
NoritakaIkeda 1d40569
🔧 Implement DiffDisplay component and integrate it into EditableConte…
NoritakaIkeda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
frontend/apps/app/features/projects/actions/updateKnowledgeSuggestionContent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 'use server' | ||
|
|
||
| import { createClient } from '@/libs/db/server' | ||
| import * as v from 'valibot' | ||
|
|
||
| const formDataSchema = v.object({ | ||
| suggestionId: v.pipe( | ||
| v.string(), | ||
| v.transform((value) => Number(value)), | ||
| ), | ||
| content: v.string(), | ||
| }) | ||
|
|
||
| export const updateKnowledgeSuggestionContent = async (formData: FormData) => { | ||
| const formDataObject = { | ||
| suggestionId: formData.get('suggestionId'), | ||
| content: formData.get('content'), | ||
| } | ||
|
|
||
| const parsedData = v.safeParse(formDataSchema, formDataObject) | ||
|
|
||
| if (!parsedData.success) { | ||
| throw new Error(`Invalid form data: ${JSON.stringify(parsedData.issues)}`) | ||
| } | ||
|
|
||
| const { suggestionId, content } = parsedData.output | ||
|
|
||
| try { | ||
| const supabase = await createClient() | ||
|
|
||
| const { error: updateError } = await supabase | ||
| .from('KnowledgeSuggestion') | ||
| .update({ content, updatedAt: new Date().toISOString() }) | ||
| .eq('id', suggestionId) | ||
|
|
||
| if (updateError) { | ||
| throw new Error('Failed to update knowledge suggestion content') | ||
| } | ||
|
|
||
| return { success: true } | ||
| } catch (error) { | ||
| console.error('Error updating knowledge suggestion content:', error) | ||
| throw error | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
frontend/apps/app/features/projects/components/DiffDisplay/DiffDisplay.module.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| .diffContent { | ||
| font-family: monospace; | ||
| white-space: pre-wrap; | ||
| overflow-x: auto; | ||
| padding: 1rem; | ||
| border-radius: 4px; | ||
| background-color: var(--color-background-secondary); | ||
| } | ||
|
|
||
| .diffAdded { | ||
| background-color: rgba(0, 255, 0, 0.1); | ||
| color: var(--color-success); | ||
| } | ||
|
|
||
| .diffRemoved { | ||
| background-color: rgba(255, 0, 0, 0.1); | ||
| color: var(--color-error); | ||
| } | ||
|
|
||
| .diffUnchanged { | ||
| color: var(--color-text-primary); | ||
| } |
62 changes: 62 additions & 0 deletions
62
frontend/apps/app/features/projects/components/DiffDisplay/DiffDisplay.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import * as diffLib from 'diff' | ||
| import type { FC } from 'react' | ||
| import styles from './DiffDisplay.module.css' | ||
|
|
||
| interface DiffDisplayProps { | ||
| originalContent: string | null | ||
| newContent: string | ||
| } | ||
|
|
||
| export const DiffDisplay: FC<DiffDisplayProps> = ({ | ||
| originalContent, | ||
| newContent, | ||
| }) => { | ||
| if (!originalContent) { | ||
| return ( | ||
| <div className={styles.diffContent}> | ||
| {newContent.split('\n').map((line, index) => ( | ||
| <div | ||
| key={`added-${line}-${ | ||
| // biome-ignore lint/suspicious/noArrayIndexKey: <explanation> | ||
| index | ||
| }`} | ||
| className={styles.diffAdded} | ||
| > | ||
| + {line} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const diff = diffLib.diffTrimmedLines(originalContent, newContent) | ||
|
|
||
| return ( | ||
| <div className={styles.diffContent}> | ||
| {diff.map((part, index) => { | ||
| const className = part.added | ||
| ? styles.diffAdded | ||
| : part.removed | ||
| ? styles.diffRemoved | ||
| : styles.diffUnchanged | ||
|
|
||
| const prefix = part.added ? '+ ' : part.removed ? '- ' : ' ' | ||
|
|
||
| return part.value.split('\n').map((line, lineIndex) => { | ||
| if (lineIndex === part.value.split('\n').length - 1 && line === '') { | ||
| return null | ||
| } | ||
| return ( | ||
| <div | ||
| key={`${part.added ? 'added' : part.removed ? 'removed' : 'unchanged'}-${index}-${lineIndex}`} | ||
| className={className} | ||
| > | ||
| {prefix} | ||
| {line} | ||
| </div> | ||
| ) | ||
| }) | ||
| })} | ||
| </div> | ||
| ) | ||
| } |
121 changes: 121 additions & 0 deletions
121
frontend/apps/app/features/projects/components/EditableContent/EditableContent.module.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| .container { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 1rem; | ||
| } | ||
|
|
||
| .header { | ||
| display: flex; | ||
| justify-content: space-between; | ||
| align-items: center; | ||
| } | ||
|
|
||
| .sectionTitle { | ||
| font-size: 1.25rem; | ||
| font-weight: 600; | ||
| margin: 0; | ||
| color: var(--color-primary); | ||
| padding-bottom: 0.5rem; | ||
| border-bottom: 1px solid var(--color-border); | ||
| } | ||
|
|
||
| .editButton { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 0.5rem 1rem; | ||
| background-color: var(--color-background-tertiary); | ||
| color: var(--color-primary); | ||
| border: 1px solid var(--color-border); | ||
| border-radius: 0.375rem; | ||
| font-size: 0.875rem; | ||
| font-weight: 500; | ||
| cursor: pointer; | ||
| transition: all 0.2s; | ||
| } | ||
|
|
||
| .editButton:hover { | ||
| background-color: var(--color-background-tertiary-hover); | ||
| transform: translateY(-1px); | ||
| } | ||
|
|
||
| .form { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 1rem; | ||
| } | ||
|
|
||
| .contentTextarea { | ||
| padding: 1.25rem; | ||
| background-color: var(--color-background-code); | ||
| border-radius: 0.5rem; | ||
| font-family: monospace; | ||
| font-size: 0.875rem; | ||
| line-height: 1.6; | ||
| border: 1px solid rgba(0, 0, 0, 0.1); | ||
| resize: vertical; | ||
| min-height: 200px; | ||
| width: 100%; | ||
| } | ||
|
|
||
| .actionButtons { | ||
| display: flex; | ||
| justify-content: flex-end; | ||
| gap: 0.75rem; | ||
| margin-top: 1rem; | ||
| } | ||
|
|
||
| .cancelButton { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 0.5rem 1rem; | ||
| background-color: var(--color-background-tertiary); | ||
| color: var(--color-text-primary); | ||
| border: 1px solid var(--color-border); | ||
| border-radius: 0.375rem; | ||
| font-size: 0.875rem; | ||
| font-weight: 500; | ||
| cursor: pointer; | ||
| transition: all 0.2s; | ||
| } | ||
|
|
||
| .cancelButton:hover { | ||
| background-color: var(--color-background-tertiary-hover); | ||
| } | ||
|
|
||
| .saveButton { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| padding: 0.5rem 1rem; | ||
| background-color: var(--color-primary); | ||
| color: white; | ||
| border: none; | ||
| border-radius: 0.375rem; | ||
| font-size: 0.875rem; | ||
| font-weight: 500; | ||
| cursor: pointer; | ||
| transition: all 0.2s; | ||
| box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
| } | ||
|
|
||
| .saveButton:hover { | ||
| background-color: var(--color-primary-dark); | ||
| transform: translateY(-1px); | ||
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.15); | ||
| } | ||
|
|
||
| .saveButton:disabled, | ||
| .cancelButton:disabled { | ||
| opacity: 0.7; | ||
| cursor: not-allowed; | ||
| transform: none; | ||
| box-shadow: none; | ||
| } | ||
|
|
||
| .content { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 2rem; | ||
| } |
106 changes: 106 additions & 0 deletions
106
frontend/apps/app/features/projects/components/EditableContent/EditableContent.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| 'use client' | ||
|
|
||
| import React, { type ReactNode, useState } from 'react' | ||
| import { updateKnowledgeSuggestionContent } from '../../actions/updateKnowledgeSuggestionContent' | ||
| import { DiffDisplay } from '../DiffDisplay/DiffDisplay' | ||
| import styles from './EditableContent.module.css' | ||
|
|
||
| type EditableContentProps = { | ||
| content: string | ||
| suggestionId: number | ||
| className?: string | ||
| originalContent: string | null | ||
| isApproved: boolean | ||
| } | ||
|
|
||
| export const EditableContent = ({ | ||
| content, | ||
| suggestionId, | ||
| className, | ||
| originalContent, | ||
| isApproved, | ||
| }: EditableContentProps) => { | ||
| const [isEditing, setIsEditing] = useState(false) | ||
| const [editedContent, setEditedContent] = useState(content) | ||
| const [isSaving, setIsSaving] = useState(false) | ||
| const [savedContent, setSavedContent] = useState(content) | ||
|
|
||
| const handleEditClick = () => { | ||
| setIsEditing(true) | ||
| } | ||
|
|
||
| const handleCancelClick = () => { | ||
| setEditedContent(savedContent) | ||
| setIsEditing(false) | ||
| } | ||
|
|
||
| const handleSave = async (formData: FormData) => { | ||
| try { | ||
| setIsSaving(true) | ||
| await updateKnowledgeSuggestionContent(formData) | ||
| setSavedContent(editedContent) | ||
| setIsEditing(false) | ||
| } catch (error) { | ||
| console.error('Error saving content:', error) | ||
| } finally { | ||
| setIsSaving(false) | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+48
Member
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. We're using optimistic updates. |
||
|
|
||
| return ( | ||
| <div className={styles.container}> | ||
| <div className={styles.header}> | ||
| <div className={styles.sectionTitle}>Content</div> | ||
| {!isEditing && ( | ||
| <button | ||
| type="button" | ||
| onClick={handleEditClick} | ||
| className={styles.editButton} | ||
| aria-label="Edit content" | ||
| > | ||
| Edit | ||
| </button> | ||
| )} | ||
| </div> | ||
|
|
||
| {isEditing ? ( | ||
| <form action={handleSave} className={styles.form}> | ||
| <input type="hidden" name="suggestionId" value={suggestionId} /> | ||
| <textarea | ||
| name="content" | ||
| value={editedContent} | ||
| onChange={(e) => setEditedContent(e.target.value)} | ||
| className={`${styles.contentTextarea} ${className || ''}`} | ||
| rows={10} | ||
| /> | ||
| <div className={styles.actionButtons}> | ||
| <button | ||
| type="button" | ||
| onClick={handleCancelClick} | ||
| className={styles.cancelButton} | ||
| disabled={isSaving} | ||
| > | ||
| Cancel | ||
| </button> | ||
| <button | ||
| type="submit" | ||
| className={styles.saveButton} | ||
| disabled={isSaving} | ||
| > | ||
| {isSaving ? 'Saving...' : 'Save'} | ||
| </button> | ||
| </div> | ||
| </form> | ||
| ) : isApproved ? ( | ||
| <div className={styles.content}>{editedContent}</div> | ||
| ) : ( | ||
| <div className={styles.content}> | ||
| <DiffDisplay | ||
| originalContent={originalContent} | ||
| newContent={editedContent} | ||
|
Member
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. The optimistically updated content is also shown in the diff. |
||
| /> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
(aside)
For now, to keep things simple, we're updating the same record in the database. In the future, it might be better to create a new record instead — to keep a change history and improve LLM suggestions.
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.
You're right. I think so too!