This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
DEVPROD-1973: Update public keys table #2174
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
|
135 changes: 135 additions & 0 deletions
135
src/pages/preferences/preferencesTabs/publicKeysTab/PublicKeysTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { useMemo, 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 = useMemo( | ||
() => [ | ||
{ | ||
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> | ||
<Button | ||
size="small" | ||
data-cy="edit-btn" | ||
leftGlyph={<Icon glyph="Edit" />} | ||
onClick={() => { | ||
setEditModalProps({ | ||
initialPublicKey: { key, name }, | ||
visible: true, | ||
}); | ||
}} | ||
/> | ||
<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> | ||
); | ||
}, | ||
}, | ||
], | ||
[loadingRemovePublicKey, removePublicKey, sendEvent, setEditModalProps] | ||
); | ||
|
||
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 | ||
emptyComponent={<TablePlaceholder glyph="Key" message="No keys saved." />} | ||
loading={loadingMyPublicKeys} | ||
shouldAlternateRowColor | ||
table={table} | ||
/> | ||
); | ||
}; | ||
|
||
const ButtonContainer = styled.div` | ||
display: flex; | ||
gap: ${size.xs}; | ||
`; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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