-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mesh wide config plugin (#457)
move config to new package implements the EditConfiguration modal
- Loading branch information
Showing
23 changed files
with
289 additions
and
192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import MeshConfigPage from "./src/meshConfigPage"; | ||
|
||
export default { | ||
name: "meshwide/config", | ||
page: MeshConfigPage, | ||
isCommunityProtected: true, | ||
additionalRoutes: [["/meshwide/config", MeshConfigPage]], | ||
} as LimePlugin; |
61 changes: 61 additions & 0 deletions
61
plugins/lime-plugin-mesh-wide-config/src/components/Components.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,61 @@ | ||
import { VNode } from "preact"; | ||
|
||
import { Button } from "components/buttons/button"; | ||
import { BinIcon } from "components/icons/bin"; | ||
import { EditIcon } from "components/icons/edit"; | ||
import { StatusIcon } from "components/icons/status"; | ||
|
||
interface IStatusMessage { | ||
isError: boolean; | ||
children: VNode | string; | ||
} | ||
|
||
// todo(kon): merge with src/components/status/statusAndButton.tsx | ||
export const StatusAndButton = ({ | ||
isError, | ||
children, | ||
btn, | ||
onClick, | ||
}: { btn?: VNode | string; onClick?: () => void } & IStatusMessage) => { | ||
const containerClasses = | ||
"flex flex-col items-center justify-center text-center bg-white py-5 gap-3"; | ||
|
||
return ( | ||
<div className={containerClasses}> | ||
<StatusMessage isError={isError}>{children}</StatusMessage> | ||
{btn && <Button onClick={onClick}>{btn}</Button>} | ||
</div> | ||
); | ||
}; | ||
|
||
export const StatusMessage = ({ | ||
isError, | ||
children, | ||
classes, | ||
}: { | ||
classes?: string; | ||
} & IStatusMessage) => ( | ||
<div | ||
className={`flex flex-row gap-3 ${classes} items-center justify-center text-center`} | ||
> | ||
{isError ? ( | ||
<StatusIcon status={"warning"} /> | ||
) : ( | ||
<StatusIcon status={"success"} /> | ||
)} | ||
{children} | ||
</div> | ||
); | ||
|
||
export const EditOrDelete = ({ | ||
onEdit, | ||
onDelete, | ||
}: { | ||
onEdit: (e) => void; | ||
onDelete: (e) => void; | ||
}) => ( | ||
<div className={"flex flex-row gap-3"}> | ||
<EditIcon className={"cursor-pointer"} onClick={onEdit} /> | ||
<BinIcon className={"cursor-pointer"} onClick={onDelete} /> | ||
</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.
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
87 changes: 87 additions & 0 deletions
87
plugins/lime-plugin-mesh-wide-config/src/components/modals.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,87 @@ | ||
import { Trans } from "@lingui/macro"; | ||
import { ComponentChildren } from "preact"; | ||
import { useCallback } from "preact/compat"; | ||
import { useForm } from "react-hook-form"; | ||
|
||
import { ModalActions, useModal } from "components/Modal/Modal"; | ||
import InputField from "components/inputs/InputField"; | ||
|
||
import { dataTypeNameMapping } from "plugins/lime-plugin-mesh-wide/src/lib/utils"; | ||
import { MeshWideMapDataTypeKeys } from "plugins/lime-plugin-mesh-wide/src/meshWideTypes"; | ||
|
||
const useActionModal = ( | ||
title: ComponentChildren, | ||
btnText: ComponentChildren, | ||
actionName: ModalActions | ||
) => { | ||
const { toggleModal, setModalState } = useModal(); | ||
|
||
const actionModal = useCallback( | ||
(prop: string, actionCb: () => void) => { | ||
setModalState({ | ||
content: ( | ||
<div> | ||
<Trans> | ||
Are you sure you want to {title} the{" "} | ||
<strong>{prop}</strong> property? | ||
</Trans> | ||
</div> | ||
), | ||
title, | ||
[`${actionName}Cb`]: actionCb, | ||
[`${actionName}BtnText`]: btnText, | ||
}); | ||
toggleModal(); | ||
}, | ||
[actionName, btnText, setModalState, title, toggleModal] | ||
); | ||
return { actionModal, toggleModal }; | ||
}; | ||
|
||
export const useDeletePropModal = () => | ||
useActionModal( | ||
<Trans>Delete property</Trans>, | ||
<Trans>Delete</Trans>, | ||
"delete" | ||
); | ||
|
||
export const useEditPropModal = () => | ||
useActionModal( | ||
<Trans>Edit property</Trans>, | ||
<Trans>Edit</Trans>, | ||
"success" | ||
); | ||
|
||
export const useAddNewSectionModal = () => { | ||
const { toggleModal, setModalState } = useModal(); | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm({ | ||
defaultValues: { name: "" }, | ||
}); | ||
|
||
const actionModal = useCallback( | ||
(actionCb: (data) => void) => { | ||
setModalState({ | ||
content: ( | ||
<div> | ||
<InputField | ||
id={"name"} | ||
label={<Trans>Name</Trans>} | ||
register={register} | ||
/> | ||
</div> | ||
), | ||
title: <Trans>Add new section</Trans>, | ||
successCb: handleSubmit(actionCb), | ||
successBtnText: <Trans>Add</Trans>, | ||
}); | ||
toggleModal(); | ||
}, | ||
[handleSubmit, register, setModalState, toggleModal] | ||
); | ||
return { actionModal, toggleModal }; | ||
}; |
39 changes: 39 additions & 0 deletions
39
plugins/lime-plugin-mesh-wide-config/src/containers/EditConfiguration.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,39 @@ | ||
import { Trans } from "@lingui/macro"; | ||
|
||
import { | ||
FullScreenModal, | ||
IFullScreenModalProps, | ||
} from "components/Modal/FullScreenModal"; | ||
|
||
import { | ||
AddNewSectionBtn, | ||
ConfigSection, | ||
} from "plugins/lime-plugin-mesh-wide-config/src/components/ConfigSection"; | ||
import { MeshStatus } from "plugins/lime-plugin-mesh-wide-config/src/components/MeshStatus"; | ||
import { useMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigQueries"; | ||
|
||
const EditConfiguration = (props: Partial<IFullScreenModalProps>) => { | ||
const { data: meshWideConfig, isLoading } = useMeshWideConfig({}); | ||
|
||
return ( | ||
<FullScreenModal | ||
title={<Trans>Mesh wide config</Trans>} | ||
isLoading={isLoading} | ||
{...props} | ||
> | ||
{meshWideConfig && ( | ||
<> | ||
<div className={"flex flex-col gap-3"}> | ||
{meshWideConfig.map((dropdown, index) => ( | ||
<ConfigSection key={index} dropdown={dropdown} /> | ||
))} | ||
<AddNewSectionBtn /> | ||
</div> | ||
<MeshStatus /> | ||
</> | ||
)} | ||
</FullScreenModal> | ||
); | ||
}; | ||
|
||
export default EditConfiguration; |
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,31 @@ | ||
import { IMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigTypes"; | ||
|
||
export const getMeshWideConfig = async () => meshWideConfig; | ||
|
||
const options = { | ||
primary_interface: "eth0", | ||
main_ipv4_address: "10.170.128.0/16/17", | ||
}; | ||
|
||
const meshWideConfig: IMeshWideConfig = [ | ||
{ | ||
name: "lime system", | ||
options, | ||
}, | ||
{ | ||
name: "lime network", | ||
options, | ||
}, | ||
{ | ||
name: "lime wifi", | ||
options, | ||
}, | ||
{ | ||
name: "generic_uci_config prometheus", | ||
options, | ||
}, | ||
{ | ||
name: "run_asset prometheus_enable", | ||
options, | ||
}, | ||
]; |
19 changes: 19 additions & 0 deletions
19
plugins/lime-plugin-mesh-wide-config/src/meshConfigPage.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,19 @@ | ||
import { useState } from "preact/hooks"; | ||
import React from "react"; | ||
|
||
import { Button } from "components/elements/button"; | ||
|
||
import EditConfiguration from "plugins/lime-plugin-mesh-wide-config/src/containers/EditConfiguration"; | ||
|
||
const MeshConfigPage = () => { | ||
// State to show modal | ||
const [showEditConfig, setShowEditConfig] = useState(false); | ||
|
||
if (showEditConfig) { | ||
return <EditConfiguration onClose={() => setShowEditConfig(false)} />; | ||
} | ||
|
||
return <Button onClick={() => setShowEditConfig(true)}>Show modal</Button>; | ||
}; | ||
|
||
export default MeshConfigPage; |
14 changes: 14 additions & 0 deletions
14
plugins/lime-plugin-mesh-wide-config/src/meshConfigQueries.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,14 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
import { getMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigApi"; | ||
import { IMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigTypes"; | ||
|
||
export function useMeshWideConfig(params) { | ||
return useQuery<IMeshWideConfig>( | ||
["lime-meshwide", "get_mesh_config"], | ||
getMeshWideConfig, | ||
{ | ||
...params, | ||
} | ||
); | ||
} |
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,6 @@ | ||
export interface IMeshWideSection { | ||
name: string; | ||
options: { [key: string]: string }; | ||
} | ||
|
||
export type IMeshWideConfig = IMeshWideSection[]; |
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,10 +1,8 @@ | ||
import { MeshWideMenu } from "./src/meshWideMenu"; | ||
import MeshWidePage from "./src/meshWidePage"; | ||
import MeshWideConfigPage from "./src/screens/configPage"; | ||
|
||
export default { | ||
name: "MeshWide", | ||
page: MeshWidePage, | ||
menu: MeshWideMenu, | ||
additionalRoutes: [["/meshwide/config", MeshWideConfigPage]], | ||
} as LimePlugin; |
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
Oops, something went wrong.