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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Starting from `0.2.0`, Cody is using `major.EVEN_NUMBER.patch` for release versi

### Added

- Added the functionality to drag and reorder the recipes. [pull/314](https://github.com/sourcegraph/cody/pull/314)

### Fixed

### Changed
Expand Down
2 changes: 2 additions & 0 deletions vscode/webviews/App.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ const dummyVSCodeAPI: VSCodeWrapper = {
return () => {}
},
postMessage: () => {},
getState: () => ({}),
setState: () => {},
}
2 changes: 2 additions & 0 deletions vscode/webviews/Login.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const meta: ComponentMeta<typeof Login> = {
const vscodeAPI: VSCodeWrapper = {
postMessage: () => {},
onMessage: () => () => {},
getState: () => ({}),
setState: () => {},
}

const validAuthStatus: AuthStatus = {
Expand Down
4 changes: 4 additions & 0 deletions vscode/webviews/Recipes.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
padding-bottom: 0.5rem;
}

.recipe-button-drag {
border-top: 2px solid var(--vscode-activityBar-activeBorder);
}

.recipes-header {
display: flex;
justify-content: space-between;
Expand Down
52 changes: 50 additions & 2 deletions vscode/webviews/Recipes.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
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 classNames from 'classnames'

import { RecipeID } from '@sourcegraph/cody-shared/src/chat/recipes/recipe'

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)',
Expand All @@ -25,6 +34,10 @@ export const Recipes: React.FunctionComponent<{
vscodeAPI: VSCodeWrapper
myPrompts: string[] | null
}> = ({ vscodeAPI, myPrompts }) => {
const initialState = vscodeAPI.getState() as State | undefined
const reorderedRecipeList: RecipeListType = initialState?.reorderedRecipes ?? recipesList
const [recipes, setRecipes] = useState<RecipeListType>(reorderedRecipeList)
const [draggedIndex, setDraggedIndex] = useState<number | null>(null)
const onRecipeClick = (recipeID: RecipeID): void => {
vscodeAPI.postMessage({ command: 'executeRecipe', recipe: recipeID })
}
Expand All @@ -33,6 +46,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">
Expand Down Expand Up @@ -100,12 +141,19 @@ 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={classNames(
styles.recipeButton,
index === draggedIndex && styles.recipeButtonDrag
)}
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
4 changes: 4 additions & 0 deletions vscode/webviews/utils/VSCodeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface VSCodeApi {
export interface VSCodeWrapper {
postMessage(message: WebviewMessage): void
onMessage(callback: (message: ExtensionMessage) => void): () => void
getState(): unknown
setState(newState: unknown): void
}

let api: VSCodeWrapper
Expand All @@ -27,6 +29,8 @@ export function getVSCodeAPI(): VSCodeWrapper {
window.addEventListener('message', listener)
return () => window.removeEventListener('message', listener)
},
setState: newState => vsCodeApi.setState(newState),
getState: () => vsCodeApi.getState(),
}
}
return api
Expand Down
Loading