-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(host-directories): 🎉 add selectable logic at cf-advanced-settings
- Loading branch information
1 parent
0ba0b89
commit 1028c6b
Showing
15 changed files
with
287 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/components/CFDirectoriesSelectInput/CFDirectoriesSelectInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { IDetails } from "../../interfaces/robotInterfaces"; | ||
import { ReactElement, useEffect, useState } from "react"; | ||
import useFunctions from "../../hooks/useFunctions"; | ||
import InputError from "../InputError/InputError"; | ||
import InfoTip from "../InfoTip/InfoTip"; | ||
import { FormikProps } from "formik"; | ||
import Select from "react-select"; | ||
|
||
interface ICFDirectoriesSelectInput { | ||
type?: "host" | "mount"; | ||
formik: FormikProps<IDetails>; | ||
index?: number; | ||
dataTut?: string; | ||
labelName?: string; | ||
labelInfoTip?: string; | ||
rightTip?: boolean; | ||
classNameContainer?: string; | ||
classNameInput?: string; | ||
inputError?: string; | ||
inputTouched?: boolean; | ||
} | ||
|
||
export default function CFDirectoriesSelectInput({ | ||
type, | ||
formik, | ||
index, | ||
dataTut, | ||
labelName, | ||
labelInfoTip, | ||
rightTip, | ||
classNameContainer, | ||
classNameInput, | ||
inputError, | ||
inputTouched, | ||
}: ICFDirectoriesSelectInput): ReactElement { | ||
const [selectableItems, setSelectableItems] = useState<string[]>([]); | ||
const [selectedItems, setSelectedItems] = useState<string[]>([]); | ||
const [menuIsOpen, setMenuIsOpen] = useState<boolean>(false); | ||
const { getFiles } = useFunctions(); | ||
|
||
useEffect(() => { | ||
console.log("selectableItems", selectableItems); | ||
console.log("selectedItems", selectedItems); | ||
}, [selectableItems, selectedItems]); | ||
|
||
useEffect(() => { | ||
if (type === "host") { | ||
formik.setFieldValue( | ||
`hostDirectories.[${index}].hostDirectory`, | ||
selectedItems?.join(""), | ||
); | ||
} else { | ||
formik.setFieldValue( | ||
`hostDirectories.[${index}].mountPath`, | ||
selectedItems?.join(""), | ||
); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [index, selectedItems, type]); | ||
|
||
async function handleGetSelectableItems(paths?: string[]) { | ||
const { items } = await getFiles(paths); | ||
|
||
if (items?.length) { | ||
setSelectableItems( | ||
items | ||
?.filter((item: any) => item.isDir) | ||
?.map((item: any) => `/${item.name}`), | ||
); | ||
} else { | ||
setSelectableItems([]); | ||
setMenuIsOpen(false); | ||
} | ||
} | ||
|
||
return ( | ||
<div data-tut={dataTut} className={classNameContainer}> | ||
<div | ||
className={`flex min-w-fit gap-1 pb-3 text-xs font-medium text-light-700 ${classNameInput}`} | ||
> | ||
{labelName} | ||
<InfoTip content={labelInfoTip} rightTip={rightTip} /> | ||
</div> | ||
<Select | ||
menuIsOpen={menuIsOpen} | ||
options={ | ||
selectableItems?.map((item) => ({ | ||
value: item, | ||
label: selectedItems?.join() + item, | ||
})) ?? [] | ||
} | ||
onMenuOpen={() => { | ||
handleGetSelectableItems(selectedItems); | ||
setMenuIsOpen(true); | ||
}} | ||
onBlur={() => setMenuIsOpen(false)} | ||
onChange={(selected) => { | ||
if (selected) { | ||
setSelectedItems([...selectedItems, selected.value]); | ||
handleGetSelectableItems([...selectedItems, selected.value]); | ||
setMenuIsOpen(true); | ||
} else { | ||
setSelectedItems([]); | ||
handleGetSelectableItems(); | ||
} | ||
console.log(selected); | ||
}} | ||
maxMenuHeight={160} | ||
isSearchable | ||
isClearable | ||
className="text-xs" | ||
classNamePrefix="text-xs" | ||
placeholder="Select a directory..." | ||
required | ||
/> | ||
<InputError error={inputError} touched={inputTouched} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,52 @@ | ||
import { ReactElement, useEffect, useState } from "react"; | ||
import useFunctions from "../../hooks/useFunctions"; | ||
import Select from "react-select"; | ||
import { ReactElement } from "react"; | ||
import CFDirectoriesInputGroup from "../CFHostDirectoriesInputGroup/CFHostDirectoriesInputGroup"; | ||
import CFInfoBar from "../CFInfoBar/CFInfoBar"; | ||
import CreateRobotFormAddButton from "../CreateRobotFormAddButton/CreateRobotFormAddButton"; | ||
import { FormikProps } from "formik"; | ||
import { IDetails } from "../../interfaces/robotInterfaces"; | ||
|
||
export default function CFHostDirectories(): ReactElement { | ||
const [menuIsOpen, setMenuIsOpen] = useState<boolean>(false); | ||
const [selectableItems, setSelectableItems] = useState<string[]>([]); | ||
const [selectedItems, setSelectedItems] = useState<string[]>([]); | ||
const { getFiles } = useFunctions(); | ||
|
||
useEffect(() => { | ||
handleGetSelectableItems(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
useEffect(() => { | ||
console.log("selectableItems", selectableItems); | ||
console.log("selectedItems", selectedItems); | ||
}, [selectableItems, selectedItems]); | ||
|
||
async function handleGetSelectableItems(paths?: string[]) { | ||
const { items } = await getFiles(paths || []); | ||
|
||
if (items?.length) { | ||
setSelectableItems( | ||
items | ||
?.filter((item: any) => item.isDir) | ||
?.map((item: any) => `/${item.name}`), | ||
); | ||
} else { | ||
setSelectableItems([]); | ||
setMenuIsOpen(false); | ||
} | ||
} | ||
interface ICFDirectories { | ||
formik: FormikProps<IDetails>; | ||
disabled?: boolean; | ||
} | ||
|
||
export default function CFDirectories({ | ||
formik, | ||
disabled, | ||
}: ICFDirectories): ReactElement { | ||
return ( | ||
<> | ||
<Select | ||
menuIsOpen={menuIsOpen} | ||
options={ | ||
selectableItems?.map((item) => ({ | ||
value: item, | ||
label: selectedItems?.join("") || item, | ||
})) || [] | ||
} | ||
onChange={(e) => { | ||
if (e) { | ||
handleGetSelectableItems([...selectedItems, e.value]); | ||
setSelectedItems([...selectedItems, e.value]); | ||
} | ||
}} | ||
onFocus={() => setMenuIsOpen(true)} | ||
onBlur={() => setMenuIsOpen(false)} | ||
value={{ | ||
value: selectedItems?.join("") || "", | ||
label: selectedItems?.join("") || "", | ||
<div> | ||
<CFInfoBar | ||
label="Host Directories:" | ||
tip="You can link a directory on the host to the directory specified in the application here." | ||
vertical | ||
> | ||
<div className="flex flex-col gap-2"> | ||
{formik.values.hostDirectories?.map((_, index) => { | ||
return ( | ||
<CFDirectoriesInputGroup | ||
key={index} | ||
formik={formik} | ||
disabled={disabled} | ||
index={index} | ||
/> | ||
); | ||
})} | ||
</div> | ||
</CFInfoBar> | ||
|
||
<CreateRobotFormAddButton | ||
onClick={() => { | ||
formik.setFieldValue("hostDirectories", [ | ||
...formik.values.hostDirectories, | ||
{ | ||
hostDirectory: "", | ||
mountPath: "", | ||
}, | ||
]); | ||
}} | ||
isClearable | ||
disabled={disabled} | ||
/> | ||
</> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
59 changes: 59 additions & 0 deletions
59
src/components/CFHostDirectoriesInputGroup/CFHostDirectoriesInputGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import CFDirectoriesSelectInput from "../CFDirectoriesSelectInput/CFDirectoriesSelectInput"; | ||
import { IDetails } from "../../interfaces/robotInterfaces"; | ||
import CFDellButton from "../CFDellButton/CFDellButton"; | ||
import { ReactElement } from "react"; | ||
import { FormikProps } from "formik"; | ||
|
||
interface ICFDirectoriesInputGroup { | ||
formik: FormikProps<IDetails>; | ||
disabled?: boolean; | ||
index: number; | ||
} | ||
|
||
export default function CFDirectoriesInputGroup({ | ||
formik, | ||
disabled, | ||
index, | ||
}: ICFDirectoriesInputGroup): ReactElement { | ||
return ( | ||
<div className="flex w-full gap-4 rounded-md border border-light-100 p-4 shadow-sm"> | ||
<CFDirectoriesSelectInput | ||
type="host" | ||
formik={formik} | ||
index={index} | ||
labelName="Host Directory:" | ||
labelInfoTip="Select a directory on your host machine to share with the environment." | ||
classNameContainer="w-full" | ||
inputError={ | ||
// @ts-ignore | ||
formik.errors.hostDirectories?.[index]?.hostDirectory | ||
} | ||
inputTouched={true} | ||
/> | ||
<CFDirectoriesSelectInput | ||
type="mount" | ||
formik={formik} | ||
index={index} | ||
labelName="Mount Path:" | ||
labelInfoTip="Select a directory on your host machine to share with the environment." | ||
classNameContainer="w-full" | ||
rightTip | ||
inputError={ | ||
// @ts-ignore | ||
formik.errors.hostDirectories?.[index]?.mountPath | ||
} | ||
inputTouched={true} | ||
/> | ||
<div className="flex items-center justify-center pt-3 text-sm text-light-800"> | ||
<CFDellButton | ||
disabled={disabled} | ||
onClick={() => { | ||
const hostDirectories = [...formik.values.hostDirectories]; | ||
hostDirectories.splice(index, 1); | ||
formik.setFieldValue("hostDirectories", hostDirectories); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.