-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4762 from abhinandan13jan/terminal
Feat(cli-terminal): Create cli-terminal
- Loading branch information
Showing
28 changed files
with
497 additions
and
113 deletions.
There are no files selected for viewing
49 changes: 33 additions & 16 deletions
49
frontend/packages/console-app/src/components/cloud-shell/CloudShell.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 |
---|---|---|
@@ -1,21 +1,38 @@ | ||
import * as React from 'react'; | ||
import { Button } from '@patternfly/react-core'; | ||
import CloudShellBody from './CloudShellBody'; | ||
import { Dispatch } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { isCloudShellExpanded } from '../../redux/reducers/cloud-shell-reducer'; | ||
import { toggleCloudShellExpanded } from '../../redux/actions/cloud-shell-actions'; | ||
import cloudShellConfirmationModal from './cloudShellConfirmationModal'; | ||
import CloudShellDrawer from './CloudShellDrawer'; | ||
import CloudShellTerminal from './CloudShellTerminal'; | ||
|
||
const CloudShell: React.FC = () => { | ||
const [open, setOpen] = React.useState(false); | ||
return ( | ||
<> | ||
{/* Remove this button once actual terminal is in place */} | ||
<Button variant="control" onClick={() => setOpen(!open)}> | ||
Open Drawer | ||
</Button> | ||
<CloudShellDrawer open={open} onClose={() => setOpen(false)}> | ||
<CloudShellBody /> | ||
</CloudShellDrawer> | ||
</> | ||
); | ||
type StateProps = { | ||
open: boolean; | ||
}; | ||
|
||
export default CloudShell; | ||
type DispatchProps = { | ||
onClose: () => void; | ||
}; | ||
|
||
type CloudShellProps = StateProps & DispatchProps; | ||
|
||
const CloudShell: React.FC<CloudShellProps> = ({ open, onClose }) => { | ||
const toggleWithModal = () => cloudShellConfirmationModal(onClose); | ||
return open ? ( | ||
<CloudShellDrawer onClose={toggleWithModal}> | ||
<CloudShellTerminal /> | ||
</CloudShellDrawer> | ||
) : null; | ||
}; | ||
|
||
const stateToProps = (state: RootState): StateProps => ({ | ||
open: isCloudShellExpanded(state), | ||
}); | ||
|
||
const dispatchToProps = (dispatch: Dispatch): DispatchProps => ({ | ||
onClose: () => dispatch(toggleCloudShellExpanded()), | ||
}); | ||
|
||
export default connect<StateProps, DispatchProps>(stateToProps, dispatchToProps)(CloudShell); |
15 changes: 0 additions & 15 deletions
15
frontend/packages/console-app/src/components/cloud-shell/CloudShellBody.tsx
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
frontend/packages/console-app/src/components/cloud-shell/CloudShellDrawer.scss
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.co-cloud-shell-drawer { | ||
&__heading { | ||
padding-left: var(--pf-global--spacer--md); | ||
font-weight: bold; | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
frontend/packages/console-app/src/components/cloud-shell/CloudShellMastheadButton.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,71 @@ | ||
import * as React from 'react'; | ||
import { Dispatch } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { TerminalIcon } from '@patternfly/react-icons'; | ||
import { isCloudShellExpanded } from '../../redux/reducers/cloud-shell-reducer'; | ||
import { Button, ToolbarItem, Tooltip } from '@patternfly/react-core'; | ||
import { connectToFlags, WithFlagsProps } from '@console/internal/reducers/features'; | ||
import { FLAG_DEVWORKSPACE } from '../../consts'; | ||
import { toggleCloudShellExpanded } from '../../redux/actions/cloud-shell-actions'; | ||
import { useAccessReview } from '@console/internal/components/utils'; | ||
import { WorkspaceModel } from '../../models'; | ||
import cloudShellConfirmationModal from './cloudShellConfirmationModal'; | ||
|
||
type DispatchProps = { | ||
onClick: () => void; | ||
}; | ||
|
||
type StateProps = { | ||
open?: boolean; | ||
}; | ||
|
||
type Props = WithFlagsProps & StateProps & DispatchProps; | ||
|
||
// TODO use proper namespace and resource name | ||
const namespace = 'che-workspace-controller'; | ||
const name = 'cloudshell-userid'; | ||
|
||
const ClouldShellMastheadButton: React.FC<Props> = ({ flags, onClick, open }) => { | ||
const editAccess = useAccessReview({ | ||
group: WorkspaceModel.apiGroup, | ||
resource: WorkspaceModel.plural, | ||
verb: 'create', | ||
name, | ||
namespace, | ||
}); | ||
|
||
const toggleTerminal = () => { | ||
if (open) { | ||
return cloudShellConfirmationModal(onClick); | ||
} | ||
return onClick(); | ||
}; | ||
|
||
if (!editAccess || !flags[FLAG_DEVWORKSPACE]) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ToolbarItem> | ||
<Tooltip content={open ? 'Close Terminal' : 'Open command line terminal'}> | ||
<Button variant="plain" aria-label="Command Line Terminal" onClick={toggleTerminal}> | ||
<TerminalIcon className="co-masthead-icon" /> | ||
</Button> | ||
</Tooltip> | ||
</ToolbarItem> | ||
); | ||
}; | ||
|
||
const cloudshellStateToProps = (state: RootState): StateProps => ({ | ||
open: isCloudShellExpanded(state), | ||
}); | ||
|
||
const cloudshellPropsToState = (dispatch: Dispatch): DispatchProps => ({ | ||
onClick: () => dispatch(toggleCloudShellExpanded()), | ||
}); | ||
|
||
export default connect<StateProps, DispatchProps>( | ||
cloudshellStateToProps, | ||
cloudshellPropsToState, | ||
)(connectToFlags(FLAG_DEVWORKSPACE)(ClouldShellMastheadButton)); |
45 changes: 45 additions & 0 deletions
45
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminal.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,45 @@ | ||
import * as React from 'react'; | ||
import { referenceForModel, k8sCreate } from '@console/internal/module/k8s'; | ||
import { Firehose, FirehoseResource, FirehoseResult } from '@console/internal/components/utils'; | ||
|
||
import { WorkspaceModel } from '../../models'; | ||
import { newCloudShellWorkSpace, CloudShellResource } from './utils/cloudshell-resource'; | ||
import CloudShellTerminalFrame from './CloudShellTerminalFrame'; | ||
|
||
// TODO use proper namespace and resource name | ||
const namespace = 'che-workspace-controller'; | ||
const name = 'cloudshell-userid'; | ||
|
||
const Inner: React.FC<{ cloudShell?: FirehoseResult<CloudShellResource[]> }> = ({ cloudShell }) => { | ||
const loaded = cloudShell?.loaded; | ||
const data = cloudShell?.data?.[0]; | ||
React.useEffect(() => { | ||
if (loaded && data == null) { | ||
k8sCreate(WorkspaceModel, newCloudShellWorkSpace(name, namespace)); | ||
} | ||
}, [loaded, data]); | ||
|
||
const running = data?.status?.phase === 'Running'; | ||
const url = data?.status?.ideUrl; | ||
return <CloudShellTerminalFrame loading={!running} url={url} />; | ||
}; | ||
|
||
const CloudShellTerminal: React.FC = () => { | ||
const resources: FirehoseResource[] = [ | ||
{ | ||
kind: referenceForModel(WorkspaceModel), | ||
namespace, | ||
prop: `cloudShell`, | ||
isList: true, | ||
fieldSelector: `metadata.name=${name}`, | ||
}, | ||
]; | ||
|
||
return ( | ||
<Firehose resources={resources}> | ||
<Inner /> | ||
</Firehose> | ||
); | ||
}; | ||
|
||
export default CloudShellTerminal; |
11 changes: 11 additions & 0 deletions
11
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminalFrame.scss
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 @@ | ||
.co-cloud-shell-terminal-frame { | ||
background-color: #000; | ||
height: 100%; | ||
|
||
& > iframe { | ||
height: 100%; | ||
width: 100%; | ||
border: 0; | ||
padding: 4px; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminalFrame.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,16 @@ | ||
import * as React from 'react'; | ||
import { LoadingBox } from '@console/internal/components/utils'; | ||
import './CloudShellTerminalFrame.scss'; | ||
|
||
type CloudShellTerminalFrameProps = { | ||
loading?: boolean; | ||
url?: string; | ||
}; | ||
|
||
const CloudShellTerminalFrame: React.FC<CloudShellTerminalFrameProps> = ({ loading, url }) => ( | ||
<div className="co-cloud-shell-terminal-frame"> | ||
{loading ? <LoadingBox /> : <iframe title="Command Line Terminal" src={url} />} | ||
</div> | ||
); | ||
|
||
export default CloudShellTerminalFrame; |
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
21 changes: 21 additions & 0 deletions
21
...ackages/console-app/src/components/cloud-shell/__tests__/CloudShellTerminalFrame.spec.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,21 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { LoadingBox } from '@console/internal/components/utils'; | ||
import CloudShellTerminalFrame from '../CloudShellTerminalFrame'; | ||
|
||
describe('CloudShellTerminalFrame', () => { | ||
it('should render LoadingBox', () => { | ||
const wrapper = shallow(<CloudShellTerminalFrame loading />); | ||
expect(wrapper.find(LoadingBox).exists()).toBe(true); | ||
expect(wrapper.find('iframe').exists()).toBe(false); | ||
|
||
wrapper.setProps({ loading: false }); | ||
expect(wrapper.find(LoadingBox).exists()).toBe(false); | ||
}); | ||
|
||
it('should render iframe', () => { | ||
const wrapper = shallow(<CloudShellTerminalFrame url="test" />); | ||
const iframe = wrapper.find('iframe'); | ||
expect(iframe.props().src).toBe('test'); | ||
}); | ||
}); |
Oops, something went wrong.