-
Notifications
You must be signed in to change notification settings - Fork 25
DEVPROD-1973: Update public keys table #2174
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,14 +53,9 @@ export const EditModal: React.FC<EditModalProps> = ({ | |
`There was an error editing the public key: ${error.message}` | ||
); | ||
}, | ||
onCompleted() {}, | ||
update(cache, { data }) { | ||
cache.writeQuery<MyPublicKeysQuery, MyPublicKeysQueryVariables>({ | ||
query: MY_PUBLIC_KEYS, | ||
data: { myPublicKeys: [...data.updatePublicKey] }, | ||
}); | ||
}, | ||
refetchQueries: ["MyPublicKeys"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :D |
||
}); | ||
|
||
const [createPublicKey] = useMutation< | ||
CreatePublicKeyMutation, | ||
CreatePublicKeyMutationVariables | ||
|
@@ -70,13 +65,7 @@ export const EditModal: React.FC<EditModalProps> = ({ | |
`There was an error creating the public key: ${error.message}` | ||
); | ||
}, | ||
onCompleted() {}, | ||
update(cache, { data }) { | ||
cache.writeQuery<MyPublicKeysQuery, MyPublicKeysQueryVariables>({ | ||
query: MY_PUBLIC_KEYS, | ||
data: { myPublicKeys: [...data.createPublicKey] }, | ||
}); | ||
}, | ||
refetchQueries: ["MyPublicKeys"], | ||
}); | ||
|
||
const [keyName, setKeyName] = useState<string>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { useRef } from "react"; | ||
import { useQuery, useMutation } from "@apollo/client"; | ||
import styled from "@emotion/styled"; | ||
import Button from "@leafygreen-ui/button"; | ||
import Icon from "@leafygreen-ui/icon"; | ||
import { useLeafyGreenTable } from "@leafygreen-ui/table"; | ||
import { usePreferencesAnalytics } from "analytics"; | ||
import Popconfirm from "components/Popconfirm"; | ||
import { WordBreak } from "components/styles"; | ||
import { BaseTable } from "components/Table/BaseTable"; | ||
import { TablePlaceholder } from "components/Table/TablePlaceholder"; | ||
import { size } from "constants/tokens"; | ||
import { useToastContext } from "context/toast"; | ||
import { | ||
MyPublicKeysQuery, | ||
MyPublicKeysQueryVariables, | ||
PublicKey, | ||
RemovePublicKeyMutation, | ||
RemovePublicKeyMutationVariables, | ||
} from "gql/generated/types"; | ||
import { REMOVE_PUBLIC_KEY } from "gql/mutations"; | ||
import { MY_PUBLIC_KEYS } from "gql/queries"; | ||
import { EditModalPropsState } from "./EditModal"; | ||
|
||
type PublicKeysTableProps = { | ||
setEditModalProps: React.Dispatch<React.SetStateAction<EditModalPropsState>>; | ||
}; | ||
|
||
export const PublicKeysTable: React.FC<PublicKeysTableProps> = ({ | ||
setEditModalProps, | ||
}) => { | ||
const dispatchToast = useToastContext(); | ||
const { sendEvent } = usePreferencesAnalytics(); | ||
|
||
const { data: myKeysData, loading: loadingMyPublicKeys } = useQuery< | ||
MyPublicKeysQuery, | ||
MyPublicKeysQueryVariables | ||
>(MY_PUBLIC_KEYS, { | ||
onError(error) { | ||
dispatchToast.error( | ||
`There was an error fetching your public keys: ${error.message}` | ||
); | ||
}, | ||
}); | ||
|
||
const [removePublicKey, { loading: loadingRemovePublicKey }] = useMutation< | ||
RemovePublicKeyMutation, | ||
RemovePublicKeyMutationVariables | ||
>(REMOVE_PUBLIC_KEY, { | ||
onError(error) { | ||
dispatchToast.error( | ||
`There was an error removing the public key: ${error.message}` | ||
); | ||
}, | ||
refetchQueries: ["MyPublicKeys"], | ||
}); | ||
|
||
const columns = [ | ||
{ | ||
header: "Name", | ||
accessorKey: "name", | ||
cell: ({ getValue }) => ( | ||
<WordBreak data-cy="table-key-name">{getValue()}</WordBreak> | ||
), | ||
}, | ||
{ | ||
header: "Actions", | ||
cell: ({ row }) => { | ||
const { key, name } = row.original; | ||
return ( | ||
<ButtonContainer className="w-[100px]"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just double checking that you mean to keep this className here |
||
<Button | ||
size="small" | ||
data-cy="edit-btn" | ||
leftGlyph={<Icon glyph="Edit" />} | ||
onClick={() => { | ||
setEditModalProps({ | ||
initialPublicKey: { key, name }, | ||
visible: true, | ||
}); | ||
}} | ||
/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly could look nice if we turned these into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alas it looked a bit weird :( we use this action button pattern elsewhere too so I think the consistency is good! |
||
<Popconfirm | ||
align="right" | ||
onConfirm={() => { | ||
sendEvent({ name: "Delete public key" }); | ||
removePublicKey({ variables: { keyName: name } }); | ||
}} | ||
trigger={ | ||
<Button | ||
size="small" | ||
data-cy="delete-btn" | ||
disabled={loadingRemovePublicKey} | ||
> | ||
<Icon glyph="Trash" /> | ||
</Button> | ||
} | ||
> | ||
Delete this public key? | ||
</Popconfirm> | ||
</ButtonContainer> | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
const tableContainerRef = useRef<HTMLDivElement>(null); | ||
const table = useLeafyGreenTable<PublicKey>({ | ||
columns, | ||
containerRef: tableContainerRef, | ||
data: myKeysData?.myPublicKeys ?? [], | ||
defaultColumn: { | ||
// Workaround for react-table auto sizing limitations. | ||
// https://github.com/TanStack/table/discussions/4179#discussioncomment-7142606 | ||
size: "auto" as unknown as number, | ||
}, | ||
}); | ||
|
||
return ( | ||
<BaseTable | ||
data-cy="hosts-table" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
emptyComponent={<TablePlaceholder glyph="Key" message="No keys saved." />} | ||
loading={loadingMyPublicKeys} | ||
shouldAlternateRowColor | ||
table={table} | ||
/> | ||
); | ||
}; | ||
|
||
const ButtonContainer = styled.div` | ||
display: flex; | ||
gap: ${size.xs}; | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could possibly remove the TableContainer here, the space between the button and table seems like a lot