Skip to content
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
merged 8 commits into from
Jul 31, 2023
Merged
Changes from 1 commit
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
39 changes: 38 additions & 1 deletion vscode/webviews/Recipes.tsx
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { VSCodeButton } from '@vscode/webview-ui-toolkit/react'
import { VSCodeButton } from '@vscode/webview-ui-toolkit/react'
import classNames from 'classnames'


import { RecipeID } from '@sourcegraph/cody-shared/src/chat/recipes/recipe'
Expand All @@ -8,6 +10,8 @@ import { VSCodeWrapper } from './utils/VSCodeApi'

import styles from './Recipes.module.css'

type RecipeListType = Record<string, string>

export const recipesList = {
'explain-code-detailed': 'Explain selected code (detailed)',
'explain-code-high-level': 'Explain selected code (high level)',
Expand All @@ -28,6 +32,8 @@ export const Recipes: React.FunctionComponent<{
myPrompts: string[]
endpoint: string
}> = ({ vscodeAPI, myPrompts, endpoint }) => {
const [recipes, setRecipes] = useState<RecipeListType>(recipesList)
const [draggedIndex, setDraggedIndex] = useState<number | null>(null)
const onRecipeClick = (recipeID: RecipeID): void => {
vscodeAPI.postMessage({ command: 'executeRecipe', recipe: recipeID })
}
Expand All @@ -36,6 +42,33 @@ export const Recipes: React.FunctionComponent<{
}
const myPromptsEnabled = isInternalUser(endpoint)

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)
setDraggedIndex(index)
}
}

const handleDragEnd = (): void => {
setDraggedIndex(null)
}

return (
<div className="inner-container">
<div className="non-transcript-container">
Expand Down Expand Up @@ -103,12 +136,16 @@ 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}
type="button"
onClick={() => onRecipeClick(key as RecipeID)}
draggable={true}
onDragStart={e => handleDragStart(e, index)}
onDragOver={e => handleDragOver(e, index)}
onDragEnd={handleDragEnd}
>
{value}
</VSCodeButton>
Expand Down