-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buttons to interface drawer yaml tab
- Loading branch information
1 parent
b737d2e
commit 15175e5
Showing
6 changed files
with
98 additions
and
3 deletions.
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
49 changes: 49 additions & 0 deletions
49
src/views/states/list/components/InterfaceDrawer/InterfaceDrawerFooter.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,49 @@ | ||
import React, { FC, useCallback, useState } from 'react'; | ||
|
||
import { Button, ButtonVariant, debounce, Tooltip } from '@patternfly/react-core'; | ||
import { NodeNetworkConfigurationInterface } from '@types'; | ||
import { useNMStateTranslation } from '@utils/hooks/useNMStateTranslation'; | ||
|
||
import { copyToClipboard, downloadYAML } from './utils'; | ||
|
||
type InterfaceDrawerFooterProps = { | ||
selectedInterface: NodeNetworkConfigurationInterface; | ||
}; | ||
|
||
const InterfaceDrawerYAMLFooter: FC<InterfaceDrawerFooterProps> = ({ selectedInterface }) => { | ||
const { t } = useNMStateTranslation(); | ||
const [copied, setCopied] = useState(false); | ||
|
||
const resetCopied = useCallback( | ||
debounce(() => { | ||
setCopied(false); | ||
}, 3000), | ||
[], | ||
); | ||
|
||
const onCopy = useCallback(() => { | ||
copyToClipboard(selectedInterface); | ||
setCopied(true); | ||
|
||
resetCopied(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Tooltip content={copied ? 'Copied' : t('Copy YAML to clipboard')}> | ||
<Button variant={ButtonVariant.primary} onClick={onCopy}> | ||
{t('Copy')} | ||
</Button> | ||
</Tooltip> | ||
<Button | ||
variant={ButtonVariant.secondary} | ||
onClick={() => downloadYAML(selectedInterface)} | ||
className="pf-u-ml-md" | ||
> | ||
{t('Download YAML')} | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
export default InterfaceDrawerYAMLFooter; |
11 changes: 11 additions & 0 deletions
11
src/views/states/list/components/InterfaceDrawer/constants.ts
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,11 @@ | ||
import { FC } from 'react'; | ||
|
||
import { NodeNetworkConfigurationInterface } from '@types'; | ||
|
||
export type InterfaceDrawerTabId = 'drawer-details' | 'drawer-yaml'; | ||
|
||
export type InterfaceDrawerTabProps = { | ||
id: InterfaceDrawerTabId; | ||
title: string; | ||
component: FC<{ selectedInterface: NodeNetworkConfigurationInterface }>; | ||
}; |
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,25 @@ | ||
import { dump } from 'js-yaml'; | ||
|
||
import { NodeNetworkConfigurationInterface } from '@types'; | ||
|
||
export const copyToClipboard = (selectedInterface: NodeNetworkConfigurationInterface) => { | ||
const el = document.createElement('textarea'); | ||
el.value = dump(selectedInterface); | ||
document.body.appendChild(el); | ||
el.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(el); | ||
}; | ||
|
||
export const downloadYAML = (selectedInterface: NodeNetworkConfigurationInterface) => { | ||
const el = document.createElement('a'); | ||
el.setAttribute( | ||
'href', | ||
`data:text/plain;charset=utf-8,${encodeURIComponent(dump(selectedInterface))}`, | ||
); | ||
el.setAttribute('download', 'interface.yaml'); | ||
el.style.display = 'none'; | ||
document.body.appendChild(el); | ||
el.click(); | ||
document.body.removeChild(el); | ||
}; |
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