/******************************************************************************** * Copyright (c) 2020 TypeFox and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ import { ChangeEvent, FunctionComponent, useContext, useState, useEffect, useRef } from 'react'; import { Extension, TargetPlatformVersion, VERSION_ALIASES } from '../../extension-registry-types'; import { Box, Grid, Typography, FormControl, FormGroup, FormControlLabel, Checkbox } from '@mui/material'; import WarningIcon from '@mui/icons-material/Warning'; import { ExtensionRemoveDialog } from './extension-remove-dialog'; import { getTargetPlatformDisplayName } from '../../utils'; import { MainContext } from '../../context'; export const ExtensionVersionContainer: FunctionComponent = props => { const WILDCARD = '*'; const { extension } = props; const { service } = useContext(MainContext); const abortController = useRef(new AbortController()); const getTargetPlatformVersions = () => { const versionMap: TargetPlatformVersion[] = []; versionMap.push({ targetPlatform: WILDCARD, version: WILDCARD, checked: false }); if (extension.allTargetPlatformVersions != null) { extension.allTargetPlatformVersions .filter(i => VERSION_ALIASES.indexOf(i.version) < 0) .forEach(i => { const { version, targetPlatforms } = i; versionMap.push({ targetPlatform: WILDCARD, version, checked: false }); targetPlatforms.forEach(targetPlatform => versionMap.push({ targetPlatform, version, checked: false })); }); } return versionMap; }; useEffect(() => { return () => { abortController.current.abort(); }; }, []); const [targetPlatformVersions, setTargetPlatformVersions] = useState(getTargetPlatformVersions()); const [icon, setIcon] = useState(undefined); useEffect(() => { if (icon) { URL.revokeObjectURL(icon); } service.getExtensionIcon(abortController.current, props.extension).then(setIcon); setTargetPlatformVersions(getTargetPlatformVersions()); }, [props.extension]); const handleChange = (event: ChangeEvent) => { const newTargetPlatformVersions: TargetPlatformVersion[] = []; targetPlatformVersions.forEach((targetPlatformVersion) => { const equals = (change: string, current: string) => { return change === WILDCARD || current === change; }; const [changedTarget, changedVersion] = event.target.name.split('/'); if (equals(changedVersion, targetPlatformVersion.version) && equals(changedTarget, targetPlatformVersion.targetPlatform)) { targetPlatformVersion.checked = event.target.checked; } newTargetPlatformVersions.push(targetPlatformVersion); }); const newVersionsMap = new Map(); newTargetPlatformVersions.forEach((targetPlatformVersion) => { if (targetPlatformVersion.version !== WILDCARD && targetPlatformVersion.targetPlatform !== WILDCARD) { const checked = newVersionsMap.get(targetPlatformVersion.version) ?? true; newVersionsMap.set(targetPlatformVersion.version, checked && targetPlatformVersion.checked); } }); newVersionsMap.forEach((checked, version) => { const targetVersion = newTargetPlatformVersions.find(t => t.version === version && t.targetPlatform === WILDCARD); targetVersion!.checked = checked; }); const checkedCount = Array.from(newTargetPlatformVersions).filter(t => t.checked === true).length; if (checkedCount < newTargetPlatformVersions.length) { const allChecked = checkedCount === newTargetPlatformVersions.length - 1; const allVersions = newTargetPlatformVersions.find(t => t.version === WILDCARD && t.targetPlatform === WILDCARD); allVersions!.checked = allChecked; } setTargetPlatformVersions(newTargetPlatformVersions); }; return { icon ? : '' } {extension.displayName ?? extension.name} {extension.deprecated &&  This extension has been deprecated. } {extension.namespace}.{extension.name} {extension.description} { targetPlatformVersions.map((targetPlatformVersion, index) => { let label: string; let indent: number; if (targetPlatformVersion.version === WILDCARD && targetPlatformVersion.targetPlatform === WILDCARD) { label = 'All Versions'; indent = 0; } else if (targetPlatformVersion.targetPlatform === WILDCARD) { label = targetPlatformVersion.version; indent = 4; } else { label = getTargetPlatformDisplayName(targetPlatformVersion.targetPlatform); indent = 8; } const name = `${targetPlatformVersion.targetPlatform}/${targetPlatformVersion.version}`; return } label={label} />; }) } value.checked && value.version != WILDCARD && value.targetPlatform != WILDCARD)} /> ; }; export interface ExtensionVersionContainerProps { extension: Extension; onRemove: (targetPlatformVersions?: TargetPlatformVersion[]) => Promise; }