|
| 1 | +import * as React from "react"; |
| 2 | +import { useDispatch, useSelector } from "react-redux"; |
| 3 | + |
| 4 | +import { ModalProps } from ".."; |
| 5 | +import BaseModal from "../BaseModal"; |
| 6 | +import { PrimaryButton, SecondaryButton } from "../../Buttons"; |
| 7 | +import ComboBox from "../../ComboBox"; |
| 8 | +import { |
| 9 | + CellFeatureExplorerBaseUrl, |
| 10 | + DatasetBucketUrl, |
| 11 | + Environment, |
| 12 | + EnvironmentOverrides, |
| 13 | + FESBaseUrl, |
| 14 | + FileStorageServiceBaseUrl, |
| 15 | + LabKeyBaseUrl, |
| 16 | + MMSBaseUrl, |
| 17 | + OverridableService, |
| 18 | + TemporaryFileServiceBaseUrl, |
| 19 | + VolEBaseUrl, |
| 20 | +} from "../../../constants"; |
| 21 | +import { interaction } from "../../../state"; |
| 22 | + |
| 23 | +import styles from "./EnvironmentSwitch.module.css"; |
| 24 | + |
| 25 | +interface ServiceDescriptor { |
| 26 | + key: OverridableService; |
| 27 | + label: string; |
| 28 | + urls: Record<Environment, string>; |
| 29 | +} |
| 30 | + |
| 31 | +const SERVICES: ServiceDescriptor[] = [ |
| 32 | + { |
| 33 | + key: OverridableService.FileExplorerService, |
| 34 | + label: "File Explorer Service (FES)", |
| 35 | + urls: FESBaseUrl, |
| 36 | + }, |
| 37 | + { |
| 38 | + key: OverridableService.MetadataManagementService, |
| 39 | + label: "Metadata Management Service (MMS)", |
| 40 | + urls: MMSBaseUrl, |
| 41 | + }, |
| 42 | + { |
| 43 | + key: OverridableService.FileStorageService, |
| 44 | + label: "File Storage Service (FSS2)", |
| 45 | + urls: FileStorageServiceBaseUrl, |
| 46 | + }, |
| 47 | + { |
| 48 | + key: OverridableService.LabKey, |
| 49 | + label: "LabKey (Plate UI links)", |
| 50 | + urls: LabKeyBaseUrl, |
| 51 | + }, |
| 52 | + { |
| 53 | + key: OverridableService.DatasetBucket, |
| 54 | + label: "Dataset Bucket (S3)", |
| 55 | + urls: DatasetBucketUrl, |
| 56 | + }, |
| 57 | + { |
| 58 | + key: OverridableService.CellFeatureExplorer, |
| 59 | + label: "Cell Feature Explorer", |
| 60 | + urls: CellFeatureExplorerBaseUrl, |
| 61 | + }, |
| 62 | + { |
| 63 | + key: OverridableService.VolE, |
| 64 | + label: "Vol-E Viewer", |
| 65 | + urls: VolEBaseUrl, |
| 66 | + }, |
| 67 | + { |
| 68 | + key: OverridableService.TemporaryFileService, |
| 69 | + label: "Temporary File Service", |
| 70 | + urls: TemporaryFileServiceBaseUrl, |
| 71 | + }, |
| 72 | +]; |
| 73 | + |
| 74 | +const ENVIRONMENT_OPTIONS = Object.values(Environment).map((env) => ({ |
| 75 | + key: env, |
| 76 | + text: env, |
| 77 | +})); |
| 78 | + |
| 79 | +/** |
| 80 | + * Developer tool for pointing one or many services at a non-default environment |
| 81 | + * (e.g., staging). Opened via keyboard shortcut; applies to the current session only. |
| 82 | + */ |
| 83 | +export default function EnvironmentSwitch({ onDismiss }: ModalProps) { |
| 84 | + const dispatch = useDispatch(); |
| 85 | + const environment = useSelector(interaction.selectors.getEnvironment) as Environment; |
| 86 | + const overrides = useSelector(interaction.selectors.getEnvironmentOverrides); |
| 87 | + |
| 88 | + const [draft, setDraft] = React.useState<Record<OverridableService, Environment>>(() => |
| 89 | + SERVICES.reduce( |
| 90 | + (accum, service) => ({ |
| 91 | + ...accum, |
| 92 | + [service.key]: overrides[service.key] ?? environment, |
| 93 | + }), |
| 94 | + {} as Record<OverridableService, Environment> |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + const hasChanges = SERVICES.some( |
| 99 | + (service) => draft[service.key] !== (overrides[service.key] ?? environment) |
| 100 | + ); |
| 101 | + |
| 102 | + // Selected key for the "all services" shortcut; null when services differ |
| 103 | + const environmentForAllServices = SERVICES.every( |
| 104 | + (service) => draft[service.key] === draft[SERVICES[0].key] |
| 105 | + ) |
| 106 | + ? draft[SERVICES[0].key] |
| 107 | + : null; |
| 108 | + |
| 109 | + const onSelectAll = (env: Environment) => { |
| 110 | + setDraft( |
| 111 | + SERVICES.reduce( |
| 112 | + (accum, service) => ({ ...accum, [service.key]: env }), |
| 113 | + {} as Record<OverridableService, Environment> |
| 114 | + ) |
| 115 | + ); |
| 116 | + }; |
| 117 | + |
| 118 | + const onSelectService = (key: OverridableService, env: Environment) => { |
| 119 | + setDraft((prev) => ({ ...prev, [key]: env })); |
| 120 | + }; |
| 121 | + |
| 122 | + const onApply = () => { |
| 123 | + // Only persist actual deviations from the app-wide environment so that |
| 124 | + // non-overridden services continue to follow it |
| 125 | + const newOverrides: EnvironmentOverrides = {}; |
| 126 | + SERVICES.forEach((service) => { |
| 127 | + if (draft[service.key] !== environment) { |
| 128 | + newOverrides[service.key] = draft[service.key]; |
| 129 | + } |
| 130 | + }); |
| 131 | + dispatch(interaction.actions.setEnvironmentOverrides(newOverrides)); |
| 132 | + dispatch(interaction.actions.refresh()); |
| 133 | + onDismiss(); |
| 134 | + }; |
| 135 | + |
| 136 | + const body = ( |
| 137 | + <div> |
| 138 | + <p className={styles.text}> |
| 139 | + Point individual services at a different environment for this session. Services left |
| 140 | + at the app default (<strong>{environment}</strong>) will continue to follow it. |
| 141 | + </p> |
| 142 | + <div className={styles.allServicesRow}> |
| 143 | + <ComboBox |
| 144 | + label="All services" |
| 145 | + placeholder="(mixed)" |
| 146 | + options={ENVIRONMENT_OPTIONS} |
| 147 | + selectedKey={environmentForAllServices} |
| 148 | + onChange={(option) => option && onSelectAll(option.key as Environment)} |
| 149 | + /> |
| 150 | + </div> |
| 151 | + {SERVICES.map((service) => ( |
| 152 | + <div className={styles.serviceRow} key={service.key}> |
| 153 | + <ComboBox |
| 154 | + label={service.label} |
| 155 | + placeholder="" |
| 156 | + options={ENVIRONMENT_OPTIONS} |
| 157 | + selectedKey={draft[service.key]} |
| 158 | + onChange={(option) => |
| 159 | + option && onSelectService(service.key, option.key as Environment) |
| 160 | + } |
| 161 | + /> |
| 162 | + <code className={styles.url}>{service.urls[draft[service.key]]}</code> |
| 163 | + </div> |
| 164 | + ))} |
| 165 | + </div> |
| 166 | + ); |
| 167 | + |
| 168 | + const footer = ( |
| 169 | + <div className={styles.footer}> |
| 170 | + <SecondaryButton |
| 171 | + text="RESET TO DEFAULT" |
| 172 | + title={`Reset all services to ${environment}`} |
| 173 | + onClick={() => onSelectAll(environment)} |
| 174 | + /> |
| 175 | + <PrimaryButton |
| 176 | + disabled={!hasChanges} |
| 177 | + text="APPLY" |
| 178 | + title="Apply environment changes and refresh" |
| 179 | + onClick={onApply} |
| 180 | + /> |
| 181 | + </div> |
| 182 | + ); |
| 183 | + |
| 184 | + return ( |
| 185 | + <BaseModal |
| 186 | + body={body} |
| 187 | + footer={footer} |
| 188 | + onDismiss={onDismiss} |
| 189 | + title="Service environments (dev)" |
| 190 | + /> |
| 191 | + ); |
| 192 | +} |
0 commit comments