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

fix: hide edit prompts on the web #6815

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 24 additions & 10 deletions vscode/webviews/components/promptList/PromptList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { ActionItem } from './ActionItem'
import styles from './PromptList.module.css'
import { usePromptsQuery } from './usePromptsQuery'
import { commandRowValue } from './utils'
import { commandRowValue, shouldShowAction } from './utils'

Check failure on line 21 in vscode/webviews/components/promptList/PromptList.tsx

View workflow job for this annotation

GitHub Actions / build

This import is unused.

Check failure on line 21 in vscode/webviews/components/promptList/PromptList.tsx

View workflow job for this annotation

GitHub Actions / JetBrains tests

'shouldShowAction' is declared but its value is never read.

Check failure on line 21 in vscode/webviews/components/promptList/PromptList.tsx

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 20)

'shouldShowAction' is declared but its value is never read.

Check failure on line 21 in vscode/webviews/components/promptList/PromptList.tsx

View workflow job for this annotation

GitHub Actions / test-unit (windows, 20)

'shouldShowAction' is declared but its value is never read.

Check failure on line 21 in vscode/webviews/components/promptList/PromptList.tsx

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu, 18)

'shouldShowAction' is declared but its value is never read.

const BUILT_IN_PROMPTS_CODE: Record<string, number> = {
'document-code': 1,
Expand Down Expand Up @@ -156,23 +156,37 @@
error,
]
)

const filteredActions = useCallback(
(actions: Action[]) => {
const { clientCapabilities } = useConfig()
const isEditEnabled = clientCapabilities.edit !== 'none'

function isEditActionLike(action: Action): boolean {
return action.actionType === 'prompt' ? action.mode !== 'CHAT' : action.mode !== 'ask'
}
Comment on lines +164 to +166
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should either be defined outside the component or it should replace shouldShowAction which you don't need anymore.


// First pass: filter out edit-requiring actions if edit is disabled
const filteredActions = isEditEnabled
? actions
: actions.filter(action => !isEditActionLike(action))
Comment on lines +169 to +171
Copy link
Contributor

@fkling fkling Jan 27, 2025

Choose a reason for hiding this comment

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

I really would suggest to implement this as

if (!isEditEnabled) {
    actions = actions.filter(action => !isEditActionLike(action))
}

Otherwise we have two variables in scope that represent actions (actions and filteredActions) and it seems too easy to make a mistake and reference actions instead of filteredActions below.
In the worst case you could accidentally do return actions and throw away the whole filtering.

Of course we can also build just a single filter condition to avoid this, but that seems more tricky.


// Second pass: apply prompt filters based on configuration
if (promptFilters?.core) {
return actions.filter(action => action.actionType === 'prompt' && action.builtin)
return filteredActions.filter(action => action.actionType === 'prompt' && action.builtin)
}
const shouldExcludeBuiltinCommands =

const hasCustomFilters =
Copy link
Contributor

Choose a reason for hiding this comment

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

The naming seems to imply that promptFilters.core is not a custom filter. It's not clear to me what a 'custom filter' is in this case. I'd leave the name as before.

promptFilters?.promoted || promptFilters?.owner || promptFilters?.tags
if (shouldExcludeBuiltinCommands) {
return actions.filter(action => action.actionType === 'prompt' && !action.builtin)
if (hasCustomFilters) {
return filteredActions.filter(
action => action.actionType === 'prompt' && !action.builtin
)
}
return actions

return filteredActions
},
[promptFilters]
)

// Don't show builtin commands to insert in the prompt editor.
) // Don't show builtin commands to insert in the prompt editor.
Copy link
Contributor

Choose a reason for hiding this comment

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

Keep the empty line and the comment above the following line.

const allActions = showOnlyPromptInsertableCommands
? result?.actions.filter(action => action.actionType === 'prompt' || action.mode === 'ask') ?? []
: result?.actions ?? []
Expand Down
7 changes: 7 additions & 0 deletions vscode/webviews/components/promptList/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ import type { Action } from '@sourcegraph/cody-shared'
export function commandRowValue(row: Action): string {
return row.actionType === 'prompt' ? `prompt-${row.id}` : `command-${row.key}`
}

export function shouldShowAction(action: Action, isEditEnabled: boolean): boolean {
const isActionEditLike =
action.actionType === 'prompt' ? action.mode !== 'CHAT' : action.mode !== 'ask'

return isEditEnabled || !isActionEditLike
}
Loading