diff --git a/.changeset/many-gorillas-juggle.md b/.changeset/many-gorillas-juggle.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/many-gorillas-juggle.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/apps/dev-wallet/src/Components/Aside/Aside.tsx b/packages/apps/dev-wallet/src/Components/Aside/Aside.tsx index cf16b7e00e..4d6b201062 100644 --- a/packages/apps/dev-wallet/src/Components/Aside/Aside.tsx +++ b/packages/apps/dev-wallet/src/Components/Aside/Aside.tsx @@ -3,6 +3,10 @@ import { useLocation } from 'react-router-dom'; const importView = async (key: string) => { switch (key) { + case 'AddNetwork': + return lazy(() => + import(`./views/AddNetwork`).catch(() => import(`./views/Error`)), + ); case 'AddContact': return lazy(() => import(`./views/AddContact`).catch(() => import(`./views/Error`)), diff --git a/packages/apps/dev-wallet/src/Components/Aside/views/AddNetwork.tsx b/packages/apps/dev-wallet/src/Components/Aside/views/AddNetwork.tsx new file mode 100644 index 0000000000..f1cc0aac86 --- /dev/null +++ b/packages/apps/dev-wallet/src/Components/Aside/views/AddNetwork.tsx @@ -0,0 +1,52 @@ +import { networkRepository } from '@/modules/network/network.repository'; +import { useWallet } from '@/modules/wallet/wallet.hook'; +import { + INetworkWithOptionalUuid, + NetworkForm, +} from '@/pages/networks/Components/network-form'; +import { useLayout } from '@kadena/kode-ui/patterns'; +import { useMemo } from 'react'; + +const getNewNetwork = (): INetworkWithOptionalUuid => ({ + uuid: undefined, + networkId: '', + name: '', + hosts: [ + { + url: '', + submit: false, + read: false, + confirm: false, + }, + ], +}); + +const AddNetwork = ({ networkUuid }: { networkUuid: string }) => { + const { handleSetAsideExpanded } = useLayout(); + const { getNetwork } = useWallet(); + + const network = useMemo(() => { + return getNetwork(networkUuid) ?? getNewNetwork(); + }, [networkUuid]); + + return ( + <> + { + if (updNetwork.uuid) { + await networkRepository.updateNetwork(updNetwork); + } else { + await networkRepository.addNetwork({ + ...updNetwork, + uuid: crypto.randomUUID(), + }); + } + handleSetAsideExpanded(false); + }} + network={network} + /> + + ); +}; + +export default AddNetwork; diff --git a/packages/apps/dev-wallet/src/modules/wallet/wallet.hook.tsx b/packages/apps/dev-wallet/src/modules/wallet/wallet.hook.tsx index 45d0e16fcb..2dc58d4926 100644 --- a/packages/apps/dev-wallet/src/modules/wallet/wallet.hook.tsx +++ b/packages/apps/dev-wallet/src/modules/wallet/wallet.hook.tsx @@ -296,9 +296,13 @@ export const useWallet = () => { const getContact = (id: string) => { return context.contacts.find((contact) => contact.uuid === id); }; + const getNetwork = (id: string) => { + return context.networks.find((network) => network.uuid === id); + }; return { getContact, + getNetwork, createProfile, unlockProfile, createKey, diff --git a/packages/apps/dev-wallet/src/pages/networks/Components/network-form.tsx b/packages/apps/dev-wallet/src/pages/networks/Components/network-form.tsx index 1db4a9323c..f911533aab 100644 --- a/packages/apps/dev-wallet/src/pages/networks/Components/network-form.tsx +++ b/packages/apps/dev-wallet/src/pages/networks/Components/network-form.tsx @@ -69,6 +69,8 @@ export function NetworkForm({ const hosts = watch('hosts'); const networkId = watch('networkId'); + console.log({ hosts, networkId }); + const isNetworkIdValid = hosts.length > 0 && networkId.length > 0 && diff --git a/packages/apps/dev-wallet/src/pages/networks/networks.tsx b/packages/apps/dev-wallet/src/pages/networks/networks.tsx index e9a0e938c2..b7e683e324 100644 --- a/packages/apps/dev-wallet/src/pages/networks/networks.tsx +++ b/packages/apps/dev-wallet/src/pages/networks/networks.tsx @@ -1,50 +1,18 @@ import { ListItem } from '@/Components/ListItem/ListItem'; -import { networkRepository } from '@/modules/network/network.repository'; import { useWallet } from '@/modules/wallet/wallet.hook'; import { createAsideUrl } from '@/utils/createAsideUrl'; import { MonoWifiTethering, MonoWorkspaces } from '@kadena/kode-icons/system'; -import { - Button, - Dialog, - DialogContent, - DialogHeader, - Heading, - Link, - Stack, - Text, -} from '@kadena/kode-ui'; +import { Heading, Link, Link as LinkUI, Stack, Text } from '@kadena/kode-ui'; import { useLayout } from '@kadena/kode-ui/patterns'; -import { useState } from 'react'; import { panelClass } from '../home/style.css'; -import { - INetworkWithOptionalUuid, - NetworkForm, -} from './Components/network-form'; - -const getNewNetwork = (): INetworkWithOptionalUuid => ({ - uuid: undefined, - networkId: '', - name: '', - hosts: [ - { - url: '', - submit: false, - read: false, - confirm: false, - }, - ], -}); export function Networks() { const { networks } = useWallet(); - const [showNetworkModal, setShowNetworkModal] = useState(false); - const [selectedNetwork, setSelectedNetwork] = - useState(() => getNewNetwork()); useLayout({ appContext: { visual: , label: 'Add Network', - href: createAsideUrl('KeySource'), + href: createAsideUrl('AddNetwork'), component: Link, }, breadCrumbs: [ @@ -55,29 +23,6 @@ export function Networks() { return ( <> - - Add Network - - { - if (updNetwork.uuid) { - await networkRepository.updateNetwork(updNetwork); - } else { - await networkRepository.addNetwork({ - ...updNetwork, - uuid: crypto.randomUUID(), - }); - } - setShowNetworkModal(false); - }} - network={selectedNetwork} - /> - - Networks - + {networks.map((network) => ( @@ -121,16 +64,16 @@ export function Networks() { ? ` +${network.hosts.length - 1}` : ''} - + diff --git a/packages/libs/kode-ui/src/patterns/SideBarLayout/components/LayoutProvider.tsx b/packages/libs/kode-ui/src/patterns/SideBarLayout/components/LayoutProvider.tsx index f64806148f..0081d3b622 100644 --- a/packages/libs/kode-ui/src/patterns/SideBarLayout/components/LayoutProvider.tsx +++ b/packages/libs/kode-ui/src/patterns/SideBarLayout/components/LayoutProvider.tsx @@ -52,9 +52,7 @@ export const LayoutContext = createContext({ isActiveUrl: () => {}, }); -export interface IuseLayoutProps extends ILayoutContext { - initPage: (props: Pick) => void; -} +export interface IuseLayoutProps extends ILayoutContext {} export const useLayout = ( props?: Pick, @@ -64,14 +62,25 @@ export const useLayout = ( useEffect(() => { if (!props) return; - context.setAppContext(props.appContext); - }, [props?.appContext]); - - useEffect(() => { - if (!props) return; - - context.setBreadCrumbs(props.breadCrumbs); - }, [props?.breadCrumbs]); + if (props.appContext?.href !== context.appContext?.href) { + context.setAppContext(props.appContext); + } + + //check if the content of the breadcrumbs has changed + const breadCrumbsHasChanged = props.breadCrumbs.reduce((acc, val, idx) => { + const oldVal = context.breadCrumbs[idx]; + if (val.url !== oldVal?.url) return true; + + return acc; + }, false); + + if ( + props.breadCrumbs.length !== context.breadCrumbs.length || + breadCrumbsHasChanged + ) { + context.setBreadCrumbs(props.breadCrumbs); + } + }, [props?.appContext, props?.breadCrumbs]); return { ...context }; };