-
Notifications
You must be signed in to change notification settings - Fork 237
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
Recipes drag and drop feature #314
Merged
st0nebreaker
merged 8 commits into
sourcegraph:main
from
deepak2431:deepak/recipes-drag_drop
Jul 31, 2023
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f5c8a3
implement drag and drop
deepak2431 51ca015
add drag css
deepak2431 784209a
implement recipe order state
deepak2431 d27cb96
fix conflicts
deepak2431 4539543
Merge branch 'main' of github-personal:sourcegraph/cody into deepak/r…
deepak2431 9784ae0
minor fix
deepak2431 b9d9c6a
add changelog
deepak2431 8810f39
update changelog
deepak2431 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,5 @@ | ||||||||||||||||
import { useState } from 'react' | ||||||||||||||||
|
||||||||||||||||
import { VSCodeButton } from '@vscode/webview-ui-toolkit/react' | ||||||||||||||||
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.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
import { RecipeID } from '@sourcegraph/cody-shared/src/chat/recipes/recipe' | ||||||||||||||||
|
@@ -6,6 +8,12 @@ import { VSCodeWrapper } from './utils/VSCodeApi' | |||||||||||||||
|
||||||||||||||||
import styles from './Recipes.module.css' | ||||||||||||||||
|
||||||||||||||||
type RecipeListType = Record<string, string> | ||||||||||||||||
|
||||||||||||||||
interface State { | ||||||||||||||||
reorderedRecipes: RecipeListType | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
export const recipesList = { | ||||||||||||||||
'explain-code-detailed': 'Explain selected code (detailed)', | ||||||||||||||||
'explain-code-high-level': 'Explain selected code (high level)', | ||||||||||||||||
|
@@ -25,6 +33,10 @@ export const Recipes: React.FunctionComponent<{ | |||||||||||||||
vscodeAPI: VSCodeWrapper | ||||||||||||||||
myPrompts: string[] | null | ||||||||||||||||
}> = ({ vscodeAPI, myPrompts }) => { | ||||||||||||||||
const initalState = vscodeAPI.getState() as State | undefined | ||||||||||||||||
const reorderedRecipeList: RecipeListType = initalState?.reorderedRecipes ?? recipesList | ||||||||||||||||
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.
Suggested change
|
||||||||||||||||
const [recipes, setRecipes] = useState<RecipeListType>(reorderedRecipeList) | ||||||||||||||||
const [draggedIndex, setDraggedIndex] = useState<number | null>(null) | ||||||||||||||||
const onRecipeClick = (recipeID: RecipeID): void => { | ||||||||||||||||
vscodeAPI.postMessage({ command: 'executeRecipe', recipe: recipeID }) | ||||||||||||||||
} | ||||||||||||||||
|
@@ -33,6 +45,34 @@ export const Recipes: React.FunctionComponent<{ | |||||||||||||||
} | ||||||||||||||||
const myPromptsEnabled = myPrompts !== null | ||||||||||||||||
|
||||||||||||||||
const handleDragStart = (event: React.DragEvent<HTMLElement>, index: number): void => { | ||||||||||||||||
setDraggedIndex(index) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const handleDragOver = (event: React.DragEvent<HTMLElement>, index: number): void => { | ||||||||||||||||
event.preventDefault() | ||||||||||||||||
|
||||||||||||||||
if (draggedIndex !== null && draggedIndex !== index) { | ||||||||||||||||
const newRecipes = Object.entries(recipes) | ||||||||||||||||
const [removedRecipe] = newRecipes.splice(draggedIndex, 1) | ||||||||||||||||
newRecipes.splice(index, 0, removedRecipe) | ||||||||||||||||
|
||||||||||||||||
const reorderedRecipes: RecipeListType = {} as RecipeListType | ||||||||||||||||
|
||||||||||||||||
for (const recipe of newRecipes) { | ||||||||||||||||
reorderedRecipes[recipe[0]] = recipe[1] | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
setRecipes(reorderedRecipes) | ||||||||||||||||
vscodeAPI.setState({ reorderedRecipes }) | ||||||||||||||||
setDraggedIndex(index) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const handleDragEnd = (): void => { | ||||||||||||||||
setDraggedIndex(null) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return ( | ||||||||||||||||
<div className="inner-container"> | ||||||||||||||||
<div className="non-transcript-container"> | ||||||||||||||||
|
@@ -100,12 +140,18 @@ export const Recipes: React.FunctionComponent<{ | |||||||||||||||
</div> | ||||||||||||||||
</> | ||||||||||||||||
)} | ||||||||||||||||
{Object.entries(recipesList).map(([key, value]) => ( | ||||||||||||||||
{Object.entries(recipes).map(([key, value], index) => ( | ||||||||||||||||
<VSCodeButton | ||||||||||||||||
key={key} | ||||||||||||||||
className={styles.recipeButton} | ||||||||||||||||
className={`${styles.recipeButton} ${ | ||||||||||||||||
draggedIndex === index ? styles.recipeButtonDrag : '' | ||||||||||||||||
}`} | ||||||||||||||||
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.
Suggested change
|
||||||||||||||||
type="button" | ||||||||||||||||
onClick={() => onRecipeClick(key as RecipeID)} | ||||||||||||||||
draggable={true} | ||||||||||||||||
onDragStart={e => handleDragStart(e, index)} | ||||||||||||||||
onDragOver={e => handleDragOver(e, index)} | ||||||||||||||||
onDragEnd={handleDragEnd} | ||||||||||||||||
> | ||||||||||||||||
{value} | ||||||||||||||||
</VSCodeButton> | ||||||||||||||||
|
This file contains 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
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.