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 4 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/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 @@ -15,6 +15,8 @@ const meta: ComponentMeta<typeof Login> = {
const vscodeAPI: VSCodeWrapper = {
postMessage: () => {},
onMessage: () => () => {},
getState: () => ({}),
setState: () => {},
}

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

.recipe-button-drag {
border-top: 2px solid var(--vscode-activityBar-activeBorder
);
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
border-top: 2px solid var(--vscode-activityBar-activeBorder
);
border-top: 2px solid var(--vscode-activityBar-activeBorder);

}

.recipes-header {
display: flex;
justify-content: space-between;
Expand Down
50 changes: 48 additions & 2 deletions 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 @@ -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)',
Expand All @@ -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
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
const initalState = vscodeAPI.getState() as State | undefined
const reorderedRecipeList: RecipeListType = initalState?.reorderedRecipes ?? recipesList
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 +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">
Expand Down Expand Up @@ -99,12 +139,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 : ''
}`}
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
className={`${styles.recipeButton} ${
draggedIndex === index ? styles.recipeButtonDrag : ''
}`}
className={classNames(
styles.recipeButton,
draggedIndex === index && 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