|
| 1 | +import { DialogProps } from "components/Dialogs/Dialog" |
| 2 | +import { FC, useRef, useState } from "react" |
| 3 | +import { FormFields } from "components/Form/Form" |
| 4 | +import TextField from "@material-ui/core/TextField" |
| 5 | +import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog" |
| 6 | +import { Stack } from "components/Stack/Stack" |
| 7 | +import { Template, TemplateVersion } from "api/typesGenerated" |
| 8 | +import { Loader } from "components/Loader/Loader" |
| 9 | +import Autocomplete from "@material-ui/lab/Autocomplete" |
| 10 | +import { createDayString } from "util/createDayString" |
| 11 | +import { AvatarData } from "components/AvatarData/AvatarData" |
| 12 | +import { Pill } from "components/Pill/Pill" |
| 13 | +import { Avatar } from "components/Avatar/Avatar" |
| 14 | +import CircularProgress from "@material-ui/core/CircularProgress" |
| 15 | + |
| 16 | +export type ChangeVersionDialogProps = DialogProps & { |
| 17 | + template: Template | undefined |
| 18 | + templateVersions: TemplateVersion[] | undefined |
| 19 | + defaultTemplateVersion: TemplateVersion | undefined |
| 20 | + onClose: () => void |
| 21 | + onConfirm: (templateVersion: TemplateVersion) => void |
| 22 | +} |
| 23 | + |
| 24 | +export const ChangeVersionDialog: FC<ChangeVersionDialogProps> = ({ |
| 25 | + onConfirm, |
| 26 | + onClose, |
| 27 | + template, |
| 28 | + templateVersions, |
| 29 | + defaultTemplateVersion, |
| 30 | + ...dialogProps |
| 31 | +}) => { |
| 32 | + const [isAutocompleteOpen, setIsAutocompleteOpen] = useState(false) |
| 33 | + const selectedTemplateVersion = useRef<TemplateVersion | undefined>() |
| 34 | + |
| 35 | + return ( |
| 36 | + <ConfirmDialog |
| 37 | + {...dialogProps} |
| 38 | + onClose={onClose} |
| 39 | + onConfirm={() => { |
| 40 | + if (selectedTemplateVersion.current) { |
| 41 | + onConfirm(selectedTemplateVersion.current) |
| 42 | + } |
| 43 | + }} |
| 44 | + hideCancel={false} |
| 45 | + type="success" |
| 46 | + cancelText="Cancel" |
| 47 | + confirmText="Change" |
| 48 | + title="Change version" |
| 49 | + description={ |
| 50 | + <Stack> |
| 51 | + <p>You are about to change the version of this workspace.</p> |
| 52 | + {templateVersions ? ( |
| 53 | + <FormFields> |
| 54 | + <Autocomplete |
| 55 | + disableClearable |
| 56 | + options={templateVersions} |
| 57 | + defaultValue={defaultTemplateVersion} |
| 58 | + id="template-version-autocomplete" |
| 59 | + open={isAutocompleteOpen} |
| 60 | + onChange={(_, newTemplateVersion) => { |
| 61 | + selectedTemplateVersion.current = |
| 62 | + newTemplateVersion ?? undefined |
| 63 | + }} |
| 64 | + onOpen={() => { |
| 65 | + setIsAutocompleteOpen(true) |
| 66 | + }} |
| 67 | + onClose={() => { |
| 68 | + setIsAutocompleteOpen(false) |
| 69 | + }} |
| 70 | + getOptionSelected={( |
| 71 | + option: TemplateVersion, |
| 72 | + value: TemplateVersion, |
| 73 | + ) => option.id === value.id} |
| 74 | + getOptionLabel={(option) => option.name} |
| 75 | + renderOption={(option: TemplateVersion) => ( |
| 76 | + <AvatarData |
| 77 | + avatar={ |
| 78 | + <Avatar src={option.created_by.avatar_url}> |
| 79 | + {option.name} |
| 80 | + </Avatar> |
| 81 | + } |
| 82 | + title={ |
| 83 | + <Stack |
| 84 | + direction="row" |
| 85 | + justifyContent="space-between" |
| 86 | + style={{ width: "100%" }} |
| 87 | + > |
| 88 | + {option.name} |
| 89 | + {template?.active_version_id === option.id && ( |
| 90 | + <Pill text="Active" type="success" /> |
| 91 | + )} |
| 92 | + </Stack> |
| 93 | + } |
| 94 | + subtitle={createDayString(option.created_at)} |
| 95 | + /> |
| 96 | + )} |
| 97 | + renderInput={(params) => ( |
| 98 | + <TextField |
| 99 | + {...params} |
| 100 | + fullWidth |
| 101 | + variant="outlined" |
| 102 | + placeholder="Template version name" |
| 103 | + InputProps={{ |
| 104 | + ...params.InputProps, |
| 105 | + endAdornment: ( |
| 106 | + <> |
| 107 | + {!templateVersions ? ( |
| 108 | + <CircularProgress size={16} /> |
| 109 | + ) : null} |
| 110 | + {params.InputProps.endAdornment} |
| 111 | + </> |
| 112 | + ), |
| 113 | + }} |
| 114 | + /> |
| 115 | + )} |
| 116 | + /> |
| 117 | + </FormFields> |
| 118 | + ) : ( |
| 119 | + <Loader /> |
| 120 | + )} |
| 121 | + </Stack> |
| 122 | + } |
| 123 | + /> |
| 124 | + ) |
| 125 | +} |
0 commit comments