Skip to content

Commit 5540afe

Browse files
committed
refactor: Hide File Download Request if capability is not present
- Hide the File Download Request form if the device does not have the right capabilities. - Limit the File Destination Types based on the device capabilities Signed-off-by: Omar <omar.brbutovic@secomind.com>
1 parent d097005 commit 5540afe

4 files changed

Lines changed: 77 additions & 29 deletions

File tree

frontend/src/components/DeviceTabs/FilesUploadTab.tsx

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ import { RECORDS_TO_LOAD_FIRST } from "@/constants";
5656
import type { FileDownloadRequestFormValues } from "@/forms/ManualFileDownloadRequestForm";
5757
import ManualFileDownloadRequestForm from "@/forms/ManualFileDownloadRequestForm";
5858
import ManualFileDownloadRequestFromRepositoryForm from "@/forms/ManualFileDownloadRequestFromRepositoryForm";
59-
import type { ManualFileDownloadRequestFromRepositoryData } from "@/forms/validation";
59+
import type {
60+
FileDestinationType,
61+
ManualFileDownloadRequestFromRepositoryData,
62+
} from "@/forms/validation";
6063
import { computeDigest, createTarGzArchive } from "@/lib/files";
6164

6265
// We use graphql fields below in columns configuration
@@ -190,6 +193,11 @@ const FILE_DOWNLOAD_REQUEST_UPDATED_SUBSCRIPTION = graphql`
190193
}
191194
`;
192195

196+
type DestinationTypeOption = {
197+
value: FileDestinationType;
198+
label: string;
199+
};
200+
193201
const formatRelayErrors = (
194202
errors: ReadonlyArray<{
195203
fields?: ReadonlyArray<string> | null;
@@ -206,12 +214,14 @@ type ManualFileDownloadRequestFormWrapperProps = {
206214
setErrorFeedback: (feedback: React.ReactNode) => void;
207215
deviceId: string;
208216
showAdvancedOptions: boolean;
217+
destinationTypeOptions: DestinationTypeOption[];
209218
};
210219

211220
const ManualFileDownloadRequestFormWrapper = ({
212221
setErrorFeedback,
213222
deviceId,
214223
showAdvancedOptions,
224+
destinationTypeOptions,
215225
}: ManualFileDownloadRequestFormWrapperProps) => {
216226
const intl = useIntl();
217227
const [isUploading, setIsUploading] = useState(false);
@@ -455,6 +465,7 @@ const ManualFileDownloadRequestFormWrapper = ({
455465
isLoading={isUploading}
456466
onFileSubmit={handleFileUpload}
457467
showAdvancedOptions={showAdvancedOptions}
468+
destinationTypeOptions={destinationTypeOptions}
458469
/>
459470
);
460471
};
@@ -464,13 +475,15 @@ type ManualFileDownloadRequestFromRepositoryFormWrapperProps = {
464475
repositoriesQueryRef: PreloadedQuery<FilesUploadTab_getRepositories_Query>;
465476
deviceId: string;
466477
showAdvancedOptions: boolean;
478+
destinationTypeOptions: DestinationTypeOption[];
467479
};
468480

469481
const ManualFileDownloadRequestFromRepositoryFormWrapper = ({
470482
setErrorFeedback,
471483
repositoriesQueryRef,
472484
deviceId,
473485
showAdvancedOptions,
486+
destinationTypeOptions,
474487
}: ManualFileDownloadRequestFromRepositoryFormWrapperProps) => {
475488
const intl = useIntl();
476489
const [isUploading, setIsUploading] = useState(false);
@@ -588,6 +601,7 @@ const ManualFileDownloadRequestFromRepositoryFormWrapper = ({
588601
isLoading={isUploading}
589602
onFileSubmit={handleFileUpload}
590603
showAdvancedOptions={showAdvancedOptions}
604+
destinationTypeOptions={destinationTypeOptions}
591605
/>
592606
);
593607
};
@@ -598,7 +612,7 @@ type FilesUploadTabProps = {
598612

599613
const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
600614
const intl = useIntl();
601-
const { deviceId } = useParams();
615+
const { deviceId = "" } = useParams();
602616

603617
const [updateMode, setUpdateMode] = useState<"repository" | "file">("file");
604618

@@ -624,10 +638,6 @@ const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
624638
[data.fileDownloadRequests],
625639
);
626640

627-
const showAdvancedOptions =
628-
data.capabilities.includes("POSIX_FILE_TRANSFER_STORAGE") ||
629-
data.capabilities.includes("POSIX_FILE_TRANSFER_STREAM");
630-
631641
const [getRepositoriesQuery, getRepositories] =
632642
useQueryLoader<FilesUploadTab_getRepositories_Query>(
633643
GET_REPOSITORIES_QUERY,
@@ -644,6 +654,41 @@ const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
644654

645655
useEffect(fetchRepositories, [fetchRepositories]);
646656

657+
const { capabilities } = data;
658+
659+
const hasStorage =
660+
capabilities.includes("POSIX_FILE_TRANSFER_STORAGE") ||
661+
capabilities.includes("WINDOWS_FILE_TRANSFER_STORAGE");
662+
663+
const hasStream =
664+
capabilities.includes("POSIX_FILE_TRANSFER_STREAM") ||
665+
capabilities.includes("WINDOWS_FILE_TRANSFER_STREAM");
666+
667+
const showAdvancedOptions =
668+
capabilities.includes("POSIX_FILE_TRANSFER_STORAGE") ||
669+
capabilities.includes("POSIX_FILE_TRANSFER_STREAM");
670+
671+
const destinationTypeOptions = useMemo<DestinationTypeOption[]>(() => {
672+
const options: DestinationTypeOption[] = [];
673+
674+
if (hasStorage) {
675+
options.push({ value: "STORAGE", label: "Storage" });
676+
}
677+
678+
if (hasStream) {
679+
options.push(
680+
{ value: "STREAMING", label: "Streaming" },
681+
{ value: "FILESYSTEM", label: "File System" },
682+
);
683+
}
684+
685+
return options;
686+
}, [hasStorage, hasStream]);
687+
688+
if (!hasStorage && !hasStream) {
689+
return null;
690+
}
691+
647692
return (
648693
<Tab
649694
eventKey="device-files-upload-tab"
@@ -675,7 +720,7 @@ const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
675720
type="radio"
676721
name="updateMode"
677722
value={updateMode}
678-
onChange={(mode) => setUpdateMode(mode)}
723+
onChange={setUpdateMode}
679724
size="sm"
680725
>
681726
<ToggleButton
@@ -709,19 +754,22 @@ const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
709754
</ToggleButton>
710755
</ToggleButtonGroup>
711756
</div>
757+
712758
{updateMode === "file" ? (
713759
<ManualFileDownloadRequestFormWrapper
714760
setErrorFeedback={setErrorFeedback}
715-
deviceId={deviceId || ""}
761+
deviceId={deviceId}
716762
showAdvancedOptions={showAdvancedOptions}
763+
destinationTypeOptions={destinationTypeOptions}
717764
/>
718765
) : (
719766
getRepositoriesQuery && (
720767
<ManualFileDownloadRequestFromRepositoryFormWrapper
721768
repositoriesQueryRef={getRepositoriesQuery}
722769
setErrorFeedback={setErrorFeedback}
723-
deviceId={deviceId || ""}
770+
deviceId={deviceId}
724771
showAdvancedOptions={showAdvancedOptions}
772+
destinationTypeOptions={destinationTypeOptions}
725773
/>
726774
)
727775
)}
@@ -745,4 +793,6 @@ const FilesUploadTab = ({ deviceRef }: FilesUploadTabProps) => {
745793
);
746794
};
747795

796+
export type { DestinationTypeOption };
797+
748798
export default FilesUploadTab;

frontend/src/forms/ManualFileDownloadRequestForm.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ import Select from "react-select";
2727
import Button from "@/components/Button";
2828
import Col from "@/components/Col";
2929
import CollapseItem, { useCollapseToggle } from "@/components/CollapseItem";
30+
import type { DestinationTypeOption } from "@/components/DeviceTabs/FilesUploadTab";
3031
import FileDropzone from "@/components/FileDropzone";
3132
import Form from "@/components/Form";
3233
import { FormRowWithMargin as FormRow } from "@/components/FormRow";
3334
import Row from "@/components/Row";
3435
import Spinner from "@/components/Spinner";
3536
import FormFeedback from "@/forms/FormFeedback";
36-
import { fileDownloadRequestFormSchema } from "@/forms/validation";
37-
38-
type FileDestination = "STORAGE" | "STREAMING" | "FILESYSTEM";
37+
import {
38+
fileDownloadRequestFormSchema,
39+
type FileDestinationType,
40+
} from "@/forms/validation";
3941

4042
type FileDownloadRequestFormValues = {
4143
files: File[];
4244
archiveName?: string;
43-
destinationType: FileDestination;
45+
destinationType: FileDestinationType;
4446
destination: string | null;
4547
ttlSeconds: number;
4648
progressTracked: boolean;
@@ -54,19 +56,15 @@ type ManualFileDownloadRequestFormProps = {
5456
isLoading: boolean;
5557
onFileSubmit: (values: FileDownloadRequestFormValues) => void;
5658
showAdvancedOptions?: boolean;
59+
destinationTypeOptions: DestinationTypeOption[];
5760
};
5861

59-
const destinationTypeOptions = [
60-
{ value: "STORAGE", label: "Storage" },
61-
{ value: "STREAMING", label: "Streaming" },
62-
{ value: "FILESYSTEM", label: "File System" },
63-
];
64-
6562
const ManualFileDownloadRequestForm = ({
6663
className,
6764
isLoading,
6865
onFileSubmit,
6966
showAdvancedOptions = false,
67+
destinationTypeOptions,
7068
}: ManualFileDownloadRequestFormProps) => {
7169
const [selectedFiles, setSelectedFiles] = useState<File[]>([]);
7270
const { open: advancedOptionsOpen, toggle: toggleAdvancedOptions } =
@@ -84,7 +82,7 @@ const ManualFileDownloadRequestForm = ({
8482
defaultValues: {
8583
file: undefined,
8684
archiveName: "",
87-
destinationType: "STORAGE" as FileDestination,
85+
destinationType: "STORAGE",
8886
destination: null,
8987
ttlSeconds: 0,
9088
progress: false,
@@ -116,7 +114,7 @@ const ManualFileDownloadRequestForm = ({
116114
selectedFiles.length > 1 && data.archiveName
117115
? data.archiveName
118116
: undefined,
119-
destinationType: data.destinationType as FileDestination,
117+
destinationType: data.destinationType,
120118
destination: data.destination,
121119
ttlSeconds: data.ttlSeconds,
122120
progressTracked: data.progress,

frontend/src/forms/ManualFileDownloadRequestFromRepositoryForm.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import type {
3535
import Button from "@/components/Button";
3636
import Col from "@/components/Col";
3737
import CollapseItem, { useCollapseToggle } from "@/components/CollapseItem";
38+
import type { DestinationTypeOption } from "@/components/DeviceTabs/FilesUploadTab";
3839
import FileSelect from "@/components/FileSelect";
3940
import Form from "@/components/Form";
4041
import { FormRowWithMargin as FormRow } from "@/components/FormRow";
@@ -85,18 +86,13 @@ const fromRepositoryInitialData: ManualFileDownloadRequestFromRepositoryData = {
8586
groupId: undefined,
8687
};
8788

88-
const destinationOptions = [
89-
{ value: "STORAGE", label: "Storage" },
90-
{ value: "STREAMING", label: "Streaming" },
91-
{ value: "FILESYSTEM", label: "File System" },
92-
];
93-
9489
type ManualFileDownloadRequestFromRepositoryFormProps = {
9590
className?: string;
9691
repositoriesData?: ManualFileDownloadRequestFromRepositoryForm_repositories_Fragment$key;
9792
isLoading: boolean;
9893
onFileSubmit: (values: ManualFileDownloadRequestFromRepositoryData) => void;
9994
showAdvancedOptions: boolean;
95+
destinationTypeOptions: DestinationTypeOption[];
10096
};
10197

10298
const ManualFileDownloadRequestFromRepositoryForm = ({
@@ -105,6 +101,7 @@ const ManualFileDownloadRequestFromRepositoryForm = ({
105101
isLoading,
106102
onFileSubmit,
107103
showAdvancedOptions,
104+
destinationTypeOptions,
108105
}: ManualFileDownloadRequestFromRepositoryFormProps) => {
109106
const intl = useIntl();
110107
const { open: advancedOptionsOpen, toggle: toggleAdvancedOptions } =
@@ -308,7 +305,7 @@ const ManualFileDownloadRequestFromRepositoryForm = ({
308305
name="destinationType"
309306
render={({ field }) => {
310307
const selectedOption =
311-
destinationOptions.find((opt) => opt.value === field.value) ||
308+
destinationTypeOptions.find((opt) => opt.value === field.value) ||
312309
null;
313310

314311
return (
@@ -317,7 +314,7 @@ const ManualFileDownloadRequestFromRepositoryForm = ({
317314
onChange={(option) => {
318315
field.onChange(option ? option.value : null);
319316
}}
320-
options={destinationOptions}
317+
options={destinationTypeOptions}
321318
/>
322319
);
323320
}}

frontend/src/forms/validation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ const fileDestinationTypeSchema = z.enum([
257257
"FILESYSTEM",
258258
]);
259259

260+
type FileDestinationType = z.infer<typeof fileDestinationTypeSchema>;
261+
260262
const nullableDestinationSchema = z.preprocess((value) => {
261263
if (typeof value !== "string") {
262264
return value ?? null;
@@ -1062,6 +1064,7 @@ export type {
10621064
RepositoryUpdateFormData,
10631065
FileFormData,
10641066
ManualFileDownloadRequestFromRepositoryData,
1067+
FileDestinationType,
10651068
};
10661069

10671070
export {

0 commit comments

Comments
 (0)