|
| 1 | +import React, { useContext, useMemo } from "react"; |
| 2 | +import { PorterApp } from "@porter-dev/api-contracts"; |
| 3 | +import { useQuery } from "@tanstack/react-query"; |
| 4 | +import { |
| 5 | + useFieldArray, |
| 6 | + useFormContext, |
| 7 | + type UseFieldArrayAppend, |
| 8 | +} from "react-hook-form"; |
| 9 | +import styled from "styled-components"; |
| 10 | +import { z } from "zod"; |
| 11 | + |
| 12 | +import Button from "components/porter/Button"; |
| 13 | +import Container from "components/porter/Container"; |
| 14 | +import Icon from "components/porter/Icon"; |
| 15 | +import Spacer from "components/porter/Spacer"; |
| 16 | +import Text from "components/porter/Text"; |
| 17 | +import { type ButtonStatus } from "main/home/app-dashboard/app-view/AppDataContainer"; |
| 18 | +import { useLatestRevision } from "main/home/app-dashboard/app-view/LatestRevisionContext"; |
| 19 | +import { AppIcon, AppSource } from "main/home/app-dashboard/apps/AppMeta"; |
| 20 | +import { |
| 21 | + appRevisionWithSourceValidator, |
| 22 | + type AppRevisionWithSource, |
| 23 | +} from "main/home/app-dashboard/apps/types"; |
| 24 | +import { type PorterAppFormData } from "lib/porter-apps"; |
| 25 | + |
| 26 | +import api from "shared/api"; |
| 27 | +import { Context } from "shared/Context"; |
| 28 | +import healthy from "assets/status-healthy.png"; |
| 29 | + |
| 30 | +type RowProps = { |
| 31 | + idx: number; |
| 32 | + app: AppRevisionWithSource; |
| 33 | + append: UseFieldArrayAppend<PorterAppFormData, "app.requiredApps">; |
| 34 | + remove: (index: number) => void; |
| 35 | + selected?: boolean; |
| 36 | +}; |
| 37 | + |
| 38 | +const RequiredAppRow: React.FC<RowProps> = ({ |
| 39 | + idx, |
| 40 | + app, |
| 41 | + selected, |
| 42 | + append, |
| 43 | + remove, |
| 44 | +}) => { |
| 45 | + const proto = useMemo(() => { |
| 46 | + return PorterApp.fromJsonString(atob(app.app_revision.b64_app_proto), { |
| 47 | + ignoreUnknownFields: true, |
| 48 | + }); |
| 49 | + }, [app.app_revision.b64_app_proto]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <ResourceOption |
| 53 | + selected={selected} |
| 54 | + onClick={() => { |
| 55 | + if (selected) { |
| 56 | + remove(idx); |
| 57 | + } else { |
| 58 | + append({ name: app.source.name }); |
| 59 | + } |
| 60 | + }} |
| 61 | + > |
| 62 | + <div> |
| 63 | + <Container row> |
| 64 | + <Spacer inline width="1px" /> |
| 65 | + <AppIcon buildpacks={proto.build?.buildpacks ?? []} /> |
| 66 | + <Spacer inline width="12px" /> |
| 67 | + <Text size={14}>{proto.name}</Text> |
| 68 | + <Spacer inline x={1} /> |
| 69 | + </Container> |
| 70 | + <Spacer height="15px" /> |
| 71 | + <Container row> |
| 72 | + <AppSource source={app.source} /> |
| 73 | + <Spacer inline x={1} /> |
| 74 | + </Container> |
| 75 | + </div> |
| 76 | + {selected && <Icon height="18px" src={healthy} />} |
| 77 | + </ResourceOption> |
| 78 | + ); |
| 79 | +}; |
| 80 | + |
| 81 | +type Props = { |
| 82 | + buttonStatus: ButtonStatus; |
| 83 | +}; |
| 84 | + |
| 85 | +export const RequiredApps: React.FC<Props> = ({ buttonStatus }) => { |
| 86 | + const { currentCluster, currentProject } = useContext(Context); |
| 87 | + |
| 88 | + const { |
| 89 | + control, |
| 90 | + formState: { isSubmitting }, |
| 91 | + } = useFormContext<PorterAppFormData>(); |
| 92 | + const { append, remove, fields } = useFieldArray({ |
| 93 | + control, |
| 94 | + name: "app.requiredApps", |
| 95 | + }); |
| 96 | + |
| 97 | + const { porterApp } = useLatestRevision(); |
| 98 | + |
| 99 | + const { data: apps = [] } = useQuery( |
| 100 | + [ |
| 101 | + "getLatestAppRevisions", |
| 102 | + { |
| 103 | + cluster_id: currentCluster?.id, |
| 104 | + project_id: currentProject?.id, |
| 105 | + }, |
| 106 | + ], |
| 107 | + async () => { |
| 108 | + if ( |
| 109 | + !currentCluster || |
| 110 | + !currentProject || |
| 111 | + currentCluster.id === -1 || |
| 112 | + currentProject.id === -1 |
| 113 | + ) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const res = await api.getLatestAppRevisions( |
| 118 | + "<token>", |
| 119 | + { |
| 120 | + deployment_target_id: undefined, |
| 121 | + ignore_preview_apps: true, |
| 122 | + }, |
| 123 | + { cluster_id: currentCluster.id, project_id: currentProject.id } |
| 124 | + ); |
| 125 | + |
| 126 | + const apps = await z |
| 127 | + .object({ |
| 128 | + app_revisions: z.array(appRevisionWithSourceValidator), |
| 129 | + }) |
| 130 | + .parseAsync(res.data); |
| 131 | + |
| 132 | + return apps.app_revisions; |
| 133 | + }, |
| 134 | + { |
| 135 | + refetchOnWindowFocus: false, |
| 136 | + enabled: !!currentCluster && !!currentProject, |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + const remainingApps = useMemo(() => { |
| 141 | + return apps.filter((a) => a.source.name !== porterApp.name); |
| 142 | + }, [apps, porterApp]); |
| 143 | + |
| 144 | + return ( |
| 145 | + <div> |
| 146 | + <Text size={16}>Required Apps</Text> |
| 147 | + <Spacer y={0.5} /> |
| 148 | + <RequiredAppList> |
| 149 | + {remainingApps.map((ra, i) => ( |
| 150 | + <RequiredAppRow |
| 151 | + idx={i} |
| 152 | + key={ra.source.name} |
| 153 | + app={ra} |
| 154 | + selected={fields.some((f) => f.name === ra.source.name)} |
| 155 | + append={append} |
| 156 | + remove={remove} |
| 157 | + /> |
| 158 | + ))} |
| 159 | + </RequiredAppList> |
| 160 | + <Spacer y={0.75} /> |
| 161 | + <Button |
| 162 | + type="submit" |
| 163 | + status={buttonStatus} |
| 164 | + loadingText={"Updating..."} |
| 165 | + disabled={isSubmitting} |
| 166 | + > |
| 167 | + Update app |
| 168 | + </Button> |
| 169 | + </div> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +const RequiredAppList = styled.div` |
| 174 | + display: flex; |
| 175 | + row-gap: 10px; |
| 176 | + flex-direction: column; |
| 177 | +`; |
| 178 | + |
| 179 | +const ResourceOption = styled.div<{ selected?: boolean }>` |
| 180 | + background: ${(props) => props.theme.clickable.bg}; |
| 181 | + border: 1px solid |
| 182 | + ${(props) => (props.selected ? "#ffffff" : props.theme.border)}; |
| 183 | + width: 100%; |
| 184 | + padding: 10px 15px; |
| 185 | + border-radius: 5px; |
| 186 | + display: flex; |
| 187 | + justify-content: space-between; |
| 188 | + align-items: center; |
| 189 | + cursor: pointer; |
| 190 | + :hover { |
| 191 | + border: 1px solid #ffffff; |
| 192 | + } |
| 193 | +`; |
0 commit comments