Skip to content

Commit

Permalink
review: review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Mason Hu <[email protected]>
  • Loading branch information
MasWho committed Feb 13, 2025
1 parent 0c86b2d commit c43c795
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 141 deletions.
47 changes: 25 additions & 22 deletions src/components/ConfigurationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ export const getConfigurationRow = ({
}
};

const isDisabled = () => {
return disabled || !!formik.values.editRestriction;
};

const getDisabledReasonOrTitle = (title?: string) => {
if (formik.values.editRestriction) {
return formik.values.editRestriction;
}

if (disabledReason) {
return disabledReason;
}

return title;
};

const getForm = (): ReactNode => {
return (
<div className="override-form">
Expand All @@ -86,7 +102,7 @@ export const getConfigurationRow = ({
onBlur: formik.handleBlur,
onChange: formik.handleChange,
value,
disabled: disabled || !!formik.values.editRestriction,
disabled: isDisabled(),
help: (
<ConfigFieldDescription
description={
Expand All @@ -104,10 +120,8 @@ export const getConfigurationRow = ({
onClick={toggleDefault}
type="button"
appearance="base"
title={
formik.values.editRestriction ?? disabledReason ?? "Clear override"
}
disabled={disabled || !!formik.values.editRestriction}
title={getDisabledReasonOrTitle("Clear override")}
disabled={isDisabled()}
hasIcon
className="u-no-margin--bottom"
>
Expand All @@ -128,10 +142,7 @@ export const getConfigurationRow = ({
const wrapDisabledTooltip = (children: ReactNode): ReactNode => {
if ((disabled && disabledReason) || formik.values.editRestriction) {
return (
<Tooltip
message={formik.values.editRestriction ?? disabledReason}
position="right"
>
<Tooltip message={getDisabledReasonOrTitle()} position="right">
{children}
</Tooltip>
);
Expand All @@ -141,16 +152,6 @@ export const getConfigurationRow = ({

const renderOverride = (): ReactNode => {
if (formik.values.readOnly) {
const getTitle = () => {
if (formik.values.editRestriction) {
return formik.values.editRestriction;
}
if (disabled) {
return disabledReason;
}

return isOverridden ? "Edit" : "Create override";
};
return (
<>
{overrideValue}
Expand All @@ -165,8 +166,10 @@ export const getConfigurationRow = ({
className="u-no-margin--bottom"
type="button"
appearance="base"
title={getTitle()}
disabled={disabled || !!formik.values.editRestriction}
title={getDisabledReasonOrTitle(
isOverridden ? "Edit" : "Create override",
)}
disabled={isDisabled()}
hasIcon
>
<Icon name="edit" />
Expand All @@ -182,7 +185,7 @@ export const getConfigurationRow = ({
onClick={toggleDefault}
className="u-no-margin--bottom"
type="button"
disabled={disabled || !!formik.values.editRestriction}
disabled={isDisabled()}
appearance="base"
title={formik.values.editRestriction ?? "Create override"}
hasIcon
Expand Down
8 changes: 1 addition & 7 deletions src/components/forms/FormSubmitBtn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ const FormSubmitBtn: FC<Props> = ({ formik, disabled, isYaml = false }) => {
<ActionButton
appearance="positive"
loading={formik.isSubmitting}
disabled={
!formik.isValid ||
disabled ||
changeCount === 0 ||
!!formik.values.editRestriction
}
disabled={!formik.isValid || disabled || changeCount === 0}
onClick={() => void formik.submitForm()}
title={formik.values.editRestriction}
>
{changeCount === 0 || isYaml
? "Save changes"
Expand Down
79 changes: 40 additions & 39 deletions src/components/forms/InheritedDeviceRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,46 +83,47 @@ export const getInheritedDeviceRow = ({
)}
</div>
),
override:
readOnly || disabledReason ? (
overrideValue ? (
<div className="mono-font">
<b>{overrideValue}</b>
</div>
) : (
""
)
) : overrideValue ? (
<div className="override-form">
<div>{overrideForm}</div>
{clearOverride && (
<div>
<Button
onClick={clearOverride}
type="button"
appearance="base"
title="Clear override"
hasIcon
className="u-no-margin--bottom"
>
<Icon name="close" className="clear-configuration-icon" />
</Button>
</div>
)}
override: readOnly ? (
overrideValue ? (
<div className="mono-font">
<b>{overrideValue}</b>
</div>
) : (
addOverride && (
<Button
onClick={addOverride}
type="button"
appearance="base"
title="Create override"
className="u-no-margin--bottom"
hasIcon
>
<Icon name="edit" />
</Button>
)
),
""
)
) : overrideValue ? (
<div className="override-form">
<div>{overrideForm}</div>
{clearOverride && (
<div>
<Button
onClick={clearOverride}
type="button"
appearance="base"
title={disabledReason ?? "Clear override"}
hasIcon
className="u-no-margin--bottom"
disabled={!!disabledReason}
>
<Icon name="close" className="clear-configuration-icon" />
</Button>
</div>
)}
</div>
) : (
addOverride && (
<Button
onClick={addOverride}
type="button"
appearance="base"
title={disabledReason ?? "Create override"}
className="u-no-margin--bottom"
hasIcon
disabled={!!disabledReason}
>
<Icon name="edit" />
</Button>
)
),
});
};
13 changes: 4 additions & 9 deletions src/components/forms/NetworkDevicesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ or remove the originating item"
}}
type="button"
appearance="base"
title={formik.values.editRestriction || "Edit network"}
title={formik.values.editRestriction ?? "Edit network"}
className="u-no-margin--top"
hasIcon
dense
Expand All @@ -235,7 +235,7 @@ or remove the originating item"
appearance="base"
hasIcon
dense
title={formik.values.editRestriction || "Detach network"}
title={formik.values.editRestriction ?? "Detach network"}
disabled={!!formik.values.editRestriction}
>
<Icon name="disconnect" />
Expand All @@ -258,13 +258,8 @@ or remove the originating item"
}}
type="button"
hasIcon
disabled={
!!formik.values.editRestriction || !managedNetworks.length
}
title={
formik.values.editRestriction ??
(!managedNetworks.length ? "No networks available" : "")
}
disabled={!!formik.values.editRestriction}
title={formik.values.editRestriction}
>
<Icon name="plus" />
<span>Attach network</span>
Expand Down
4 changes: 2 additions & 2 deletions src/components/forms/OtherDeviceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const OtherDeviceForm: FC<Props> = ({ formik, project }) => {
appearance="base"
hasIcon
dense
title={formik.values.editRestriction || "Detach device"}
title={formik.values.editRestriction ?? "Detach device"}
disabled={!!formik.values.editRestriction}
>
<Icon name="disconnect" />
Expand Down Expand Up @@ -207,7 +207,7 @@ const OtherDeviceForm: FC<Props> = ({ formik, project }) => {
appearance="base"
hasIcon
dense
title={formik.values.editRestriction || "Detach GPU"}
title={formik.values.editRestriction ?? "Detach GPU"}
disabled={!!formik.values.editRestriction}
>
<Icon name="disconnect" />
Expand Down
4 changes: 1 addition & 3 deletions src/components/forms/ProxyDeviceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ const ProxyDeviceForm: FC<Props> = ({ formik, project }) => {
options={options}
help={<ConfigFieldDescription description={help} />}
className="u-no-margin--bottom"
disabled={
disabledText != undefined || !!formik.values.editRestriction
}
disabled={!!disabledText || !!formik.values.editRestriction}
title={formik.values.editRestriction ?? disabledText}
/>
),
Expand Down
9 changes: 6 additions & 3 deletions src/pages/instances/CreateInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import BootForm, {
BootFormValues,
bootPayload,
} from "components/forms/BootForm";
import { instanceProfilesWarning } from "util/instanceEdit";
import InstanceProfilesWarning from "./InstanceProfilesWarning";

export type CreateInstanceFormValues = InstanceDetailsFormValues &
FormDeviceValues &
Expand Down Expand Up @@ -439,7 +439,6 @@ const CreateInstance: FC = () => {
const networkError = hasNetworkError(formik);
const hasErrors =
!formik.isValid || !formik.values.image || diskError || networkError;
const isCreating = true;

return (
<BaseLayout title="Create an instance" contentClassName="create-instance">
Expand All @@ -455,7 +454,11 @@ const CreateInstance: FC = () => {
)}
<Row className="form-contents" key={section}>
<Col size={12}>
{instanceProfilesWarning([], profiles, isCreating)}
<InstanceProfilesWarning
instanceProfiles={[]}
profiles={profiles}
isCreating={true}
/>
<NotificationRow />
{section === MAIN_CONFIGURATION && (
<InstanceCreateDetailsForm
Expand Down
7 changes: 5 additions & 2 deletions src/pages/instances/EditInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
getInstanceEditValues,
getInstancePayload,
InstanceEditSchema,
instanceProfilesWarning,
} from "util/instanceEdit";
import { slugify } from "util/slugify";
import { useEventQueue } from "context/eventQueue";
Expand All @@ -68,6 +67,7 @@ import InstanceLinkChip from "./InstanceLinkChip";
import BootForm, { BootFormValues } from "components/forms/BootForm";
import { useInstanceEntitlements } from "util/entitlements/instances";
import { fetchProfiles } from "api/profiles";
import InstanceProfilesWarning from "./InstanceProfilesWarning";

export interface InstanceEditDetailsFormValues {
name: string;
Expand Down Expand Up @@ -214,7 +214,10 @@ const EditInstance: FC<Props> = ({ instance }) => {
)}
<Row className="form-contents" key={section}>
<Col size={12}>
{instanceProfilesWarning(instance.profiles, profiles)}
<InstanceProfilesWarning
instanceProfiles={instance.profiles}
profiles={profiles}
/>
{(section === slugify(MAIN_CONFIGURATION) || !section) && (
<EditInstanceDetails formik={formik} project={project} />
)}
Expand Down
12 changes: 10 additions & 2 deletions src/pages/instances/InstanceConsole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ const InstanceConsole: FC<Props> = ({ instance }) => {

const { handleStart, isLoading } = useInstanceStart(instance);

if (!canAccessInstanceConsole(instance)) {
return (
<Notification severity="caution" title="Restricted permissions">
You do not have permission to access the console for this instance.
</Notification>
);
}

return (
<div className="instance-console-tab">
{isVm && canAccessInstanceConsole(instance) && (
Expand Down Expand Up @@ -147,11 +155,11 @@ const InstanceConsole: FC<Props> = ({ instance }) => {
showNotRunningInfo={showNotRunningInfo}
/>
)}
{!canAccessInstanceConsole(instance) && (
{/* {!canAccessInstanceConsole(instance) && (
<Notification severity="caution" title="Restricted permissions">
You do not have permission to access the console for this instance.
</Notification>
)}
)} */}
</div>
);
};
Expand Down
9 changes: 7 additions & 2 deletions src/pages/instances/InstanceOverviewProfiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Loader from "components/Loader";
import type { LxdInstance } from "types/instance";
import { fetchProfiles } from "api/profiles";
import ResourceLink from "components/ResourceLink";
import { instanceProfilesWarning } from "util/instanceEdit";
import InstanceProfilesWarning from "./InstanceProfilesWarning";

interface Props {
instance: LxdInstance;
Expand Down Expand Up @@ -78,7 +78,12 @@ const InstanceOverviewProfiles: FC<Props> = ({ instance, onFailure }) => {
}

if (!profiles.length) {
return instanceProfilesWarning(instance.profiles, profiles);
return (
<InstanceProfilesWarning
instanceProfiles={instance.profiles}
profiles={profiles}
/>
);
}

return <MainTable headers={profileHeaders} rows={profileRows} sortable />;
Expand Down
Loading

0 comments on commit c43c795

Please sign in to comment.