import React from 'react'; import type { Option } from 'react-select'; import VirtualizedSelect from 'react-virtualized-select'; import type { ISecurityGroup } from '@spinnaker/core'; import { DeckRuntimeContext, FirewallLabels, HelpField, InfrastructureCaches, timestamp } from '@spinnaker/core'; import type { IAmazonServerGroupCommand } from '../../serverGroupConfiguration.service'; export interface ISecurityGroupSelectorProps { command: IAmazonServerGroupCommand; availableGroups: ISecurityGroup[]; hideLabel?: boolean; refresh?: () => Promise; groupsToEdit: string[]; helpKey?: string; onChange: (securityGroups: string[]) => void; } export interface ISecurityGroupSelectorState { refreshing: boolean; refreshTime: number; } export class SecurityGroupSelector extends React.Component { public static contextType = DeckRuntimeContext; public declare context: React.ContextType; constructor(props: ISecurityGroupSelectorProps) { super(props); this.state = { refreshing: false, refreshTime: this.getRefreshTime(), }; } private getRefreshTime = () => InfrastructureCaches.get('securityGroups')?.getStats().ageMax || 0; private refreshSecurityGroups = () => { this.setState({ refreshing: true }); if (this.props.refresh) { this.props.refresh().then(() => this.setState({ refreshing: false })); } else { (this.context.services.providerServiceDelegate.getDelegate( this.props.command.selectedProvider, 'serverGroup.configurationService', ) as any) .refreshSecurityGroups(this.props.command) .then(() => { this.setState({ refreshing: false, refreshTime: this.getRefreshTime(), }); }); } }; private onChange = (options: Option[]) => { const securityGroups = options.map((o) => o.value as string); this.props.onChange(securityGroups); }; public render() { const { availableGroups, groupsToEdit, helpKey, hideLabel } = this.props; const { refreshing, refreshTime } = this.state; const availableGroupOptions = availableGroups.map((g) => ({ label: `${g.name} (${g.id})`, value: g.id })); return ( <>
{!hideLabel && (
{FirewallLabels.get('Firewalls')} {helpKey && }
)}

{refreshing && ( )} {FirewallLabels.get('Firewalls')} {!refreshing && last refreshed {timestamp(refreshTime)}} {refreshing && refreshing...}

If you're not finding a {FirewallLabels.get('firewall')} that was recently added,{' '} click here {' '} to refresh the list.

); } }