Skip to content
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
19 changes: 10 additions & 9 deletions app/components/form/fields/SshKeysField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export function SshKeysField({
control: Control<InstanceCreateInput>
isSubmitting: boolean
}) {
const keys = usePrefetchedQuery(q(api.currentUserSshKeyList, {})).data?.items || []
const allKeys = usePrefetchedQuery(q(api.currentUserSshKeyList, {})).data.items
const [showAddSshKey, setShowAddSshKey] = useState(false)

const {
field: { value, onChange },
field: { value: selectedKeys, onChange },
fieldState: { error },
} = useController({
control,
Expand All @@ -73,6 +73,8 @@ export function SshKeysField({
},
})

const allAreSelected = allKeys.length === selectedKeys.length

return (
<div className="max-w-lg">
<div className="mb-2">
Expand All @@ -81,11 +83,11 @@ export function SshKeysField({
SSH keys can be added and removed in your user settings
</TextInputHint>
</div>
{keys.length > 0 ? (
{allKeys.length > 0 ? (
<>
<div className="space-y-2">
<div className="flex flex-col space-y-2">
{keys.map((key) => (
{allKeys.map((key) => (
<CheckboxField
name="sshPublicKeys"
control={control}
Expand All @@ -102,12 +104,10 @@ export function SshKeysField({

<Divider />
<Checkbox
checked={value.length === keys.length}
indeterminate={value.length > 0 && value.length < keys.length}
checked={allAreSelected}
indeterminate={selectedKeys.length > 0 && !allAreSelected}
// if fewer than all are checked, check all. if all are checked, check none
onChange={() =>
onChange(value.length < keys.length ? keys.map((key) => key.id) : [])
}
onChange={() => onChange(allAreSelected ? [] : allKeys.map((key) => key.id))}
disabled={isSubmitting}
>
<span className="select-none">Select all</span>
Expand Down Expand Up @@ -140,6 +140,7 @@ export function SshKeysField({
{showAddSshKey && (
<SSHKeyCreate
onDismiss={() => setShowAddSshKey(false)}
onSuccess={(sshKey) => onChange([...selectedKeys, sshKey.id])}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is the fix.

message={
<Message
variant="info"
Expand Down
12 changes: 10 additions & 2 deletions app/forms/ssh-key-create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import { useForm } from 'react-hook-form'
import { useNavigate } from 'react-router'

import { api, queryClient, useApiMutation, type SshKeyCreate } from '@oxide/api'
import {
api,
queryClient,
useApiMutation,
type SshKey,
type SshKeyCreate,
} from '@oxide/api'

import { DescriptionField } from '~/components/form/fields/DescriptionField'
import { NameField } from '~/components/form/fields/NameField'
Expand All @@ -28,17 +34,19 @@ const defaultValues: SshKeyCreate = {

type Props = {
onDismiss?: () => void
onSuccess?: (sshKey: SshKey) => void
message?: React.ReactNode
}

export function SSHKeyCreate({ onDismiss, message }: Props) {
export function SSHKeyCreate({ onDismiss, onSuccess, message }: Props) {
const navigate = useNavigate()

const handleDismiss = onDismiss ? onDismiss : () => navigate(pb.sshKeys())

const createSshKey = useApiMutation(api.currentUserSshKeyCreate, {
onSuccess(sshKey) {
queryClient.invalidateEndpoint('currentUserSshKeyList')
onSuccess?.(sshKey)
handleDismiss()
// prettier-ignore
addToast(<>SSH key <HL>{sshKey.name}</HL> created</>)
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/instance-create.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,15 @@ test('add ssh key from instance create form', async ({ page }) => {
await dialog.getByRole('button', { name: 'Add SSH Key' }).click()

await expect(newCheckbox).toBeVisible()
await expect(newCheckbox).not.toBeChecked()
await expect(newCheckbox).toBeChecked()

await closeToast(page)

// pop over to the real SSH keys page and see it there, why not
await page.getByLabel('User menu').click()
await page.getByRole('menuitem', { name: 'Settings' }).click()
// the new key being auto-checked makes the form dirty, which triggers confirm leave
await page.getByRole('button', { name: 'Leave this page' }).click()
await page.getByRole('link', { name: 'SSH Keys' }).click()
await expectRowVisible(page.getByRole('table'), { name: newKey, description: 'hi' })
})
Expand Down
Loading