Skip to content

actually allow to select custom IDE image #2507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2020
Merged
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
89 changes: 60 additions & 29 deletions components/dashboard/src/components/ide-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,35 @@ function isIDEAlias(ide: string | undefined): ide is IDEAlias {
}
type IDEKind = IDEAlias | 'image';

export class IDESettings extends React.Component<IDESettingsProps> {
export interface IDESettingsState {
value?: IDEKind
image?: string
}

export class IDESettings extends React.Component<IDESettingsProps, IDESettingsState> {

constructor(props: IDESettingsProps) {
super(props);
this.state = this.updateStateFromProps({})
}

componentDidUpdate(prevProps: IDESettingsProps): void {
if (this.props.user === prevProps.user) {
return;
}
this.setState(state => this.updateStateFromProps(state));
}

private updateStateFromProps(current: IDESettingsState): IDESettingsState {
const defaultIde = this.props.user.additionalData?.ideSettings?.defaultIde;
if (isIDEAlias(defaultIde)) {
return { ...current, value: defaultIde };
}
if (defaultIde === undefined) {
return { ...current, value: 'theia' };
}
return { ...current, value: 'image', image: defaultIde };
}

render() {
return <React.Fragment>
Expand All @@ -38,50 +66,53 @@ export class IDESettings extends React.Component<IDESettingsProps> {
}

private renderRadio(label: string, value: IDEKind) {
const checked = value === this.value;
return <Grid item xs={12}>
<FormControlLabel control={<Radio color="default" />} label={label} value={value} checked={checked} onChange={this.updateDefaultIde} />
{value === 'image' && <Input value={this.image} onChange={this.updateDefaultIde} />}
</Grid>;
const checked = value === this.state.value;
return <Grid container>
<Grid item xs={1}>
<FormControlLabel control={<Radio color="default" />} label={label} value={value} checked={checked} onChange={this.updateState} />
</Grid>
<Grid item xs={11}>
{value === 'image' && this.renderImage()}
</Grid>
</Grid>
}

private get value(): IDEKind {
const defaultIde = this.props.user.additionalData?.ideSettings?.defaultIde;
if (isIDEAlias(defaultIde)) {
return defaultIde;
}
if (defaultIde) {
return 'image';
}
return 'theia';
}

private get image(): string | undefined {
const defaultIde = this.props.user.additionalData?.ideSettings?.defaultIde;
if (isIDEAlias(defaultIde)) {
return undefined;
}
return defaultIde;
private renderImage() {
return <Input
value={this.state.image}
onChange={this.updateState}
placeholder="Type a reference to docker image, e.g. index.docker.io/gitpod-io/theia-ide:latest"
error={this.state.value === 'image' && (this.state.image === undefined || this.state.image.trim() === "")}
fullWidth={true}
/>;
}

private updateDefaultIde = (event: React.ChangeEvent<HTMLInputElement>) => {
let value = this.value;
let image = this.image;
private updateState = (event: React.ChangeEvent<HTMLInputElement>) => {
const state: IDESettingsState = {}
if (event.target.type === 'radio') {
value = event.target.value as IDEKind;
state.value = event.target.value as IDEKind;
} else {
image = event.target.value;
state.image = event.target.value;
}
this.setState(state, () => this.fireStateChange());
}

private fireStateChange(): void {
const { value, image } = this.state;

const additionalData = (this.props.user.additionalData || {});
const settings = additionalData.ideSettings || {};
if (value === 'theia') {
delete settings.defaultIde;
} else if (value === 'image') {
settings.defaultIde = image;
settings.defaultIde = image || '';
} else {
settings.defaultIde = value;
}
if (settings.defaultIde?.trim() === "") {
// invalid
return;
}
additionalData.ideSettings = settings;
this.props.onChange({ additionalData });
}
Expand Down