From 3f1102513b16442bc1939f3baf494f18c1004a89 Mon Sep 17 00:00:00 2001 From: Radu Cazacu Date: Sat, 20 Jan 2024 02:39:05 +0200 Subject: [PATCH] Added hooks for p2p and c2s --- common/index.tsx | 26 - common/types/api.ts | 21 - common/types/c2s.ts | 26 - .../c2s.ts => common/types/c2sResponses.ts | 21 + common/types/errors.ts | 6 + common/types/hooks.ts | 1 - common/types/p2pResponses.ts | 16 + common/types/user.ts | 13 - components/common/Layout/Layout.tsx | 839 +++++++++--------- components/ui/AddServer.tsx | 21 +- components/ui/workspacesBar/WorkspaceBar.tsx | 10 +- framework/app/api/config.ts | 22 - framework/app/c2s/index.ts | 4 - framework/app/c2s/useConnect_c2s.ts | 38 - framework/app/c2s/useDisconnect_c2s.ts | 35 - framework/app/c2s/useGetAccountInfo_c2s.ts | 44 - framework/app/c2s/useRegister_c2s.ts | 56 -- framework/app/const.ts | 4 - framework/app/hooks.ts | 14 - framework/app/index.tsx | 21 - framework/app/messaging/index.ts | 1 - framework/app/messaging/useMessage.ts | 36 - framework/app/types/hooks.ts | 11 - framework/app/utils/invoke-api.ts | 13 - framework/common/c2s/index.ts | 3 - framework/common/c2s/useConnect_c2s.ts | 13 - framework/common/c2s/useDisconnect_c2s.ts | 12 - framework/common/c2s/useGetAccountInfo_c2s.ts | 12 - framework/common/c2s/useRegister_c2s.ts | 12 - framework/common/index.tsx | 28 - framework/common/messaging/index.ts | 1 - framework/common/messaging/useMessage.ts | 13 - framework/common/types/api.ts | 30 - framework/common/types/hooks.ts | 28 - framework/common/types/messages.ts | 9 - framework/common/types/user.ts | 13 - framework/common/utils/useHook.ts | 21 - framework/redux/store.ts | 40 - hooks/c2s/useC2SConnect.ts | 36 +- hooks/c2s/useDisconnect.ts | 28 + hooks/c2s/useListAllPeers.ts | 25 + hooks/hooks.ts | 6 - hooks/p2p/usePeerConnect.ts | 28 + hooks/p2p/usePeerDisconnect.ts | 28 + hooks/p2p/usePeerRegister.ts | 28 + hooks/redux/slices/streamHandler.slice.ts | 55 -- hooks/redux/slices/uuid.slice.ts | 18 - hooks/types/hooks.ts | 7 - hooks/utils/invoke-api.ts | 26 +- pages/_app.tsx | 29 +- pages/index.tsx | 37 +- pages/server/[serverCid].tsx | 2 +- .../server/findPeers/[findPeersServerCid].tsx | 15 + pages/storage.tsx | 4 +- .../slices/streamHandler.slice.ts | 47 +- .../redux => redux}/slices/uuid.slice.ts | 0 {hooks/redux => redux}/store.ts | 13 +- src-tauri/src/commands/clear_all_kv.rs | 32 - src-tauri/src/commands/del_kv.rs | 34 - src-tauri/src/commands/download_file.rs | 37 - src-tauri/src/commands/get_all_kv.rs | 32 - src-tauri/src/commands/get_kv.rs | 34 - src-tauri/src/commands/get_session.rs | 2 +- src-tauri/src/commands/list_all_peers.rs | 35 + src-tauri/src/commands/mod.rs | 4 + src-tauri/src/commands/peer_connect.rs | 21 +- src-tauri/src/commands/peer_disconnect.rs | 13 +- src-tauri/src/commands/peer_register.rs | 20 +- src-tauri/src/commands/register.rs | 1 - src-tauri/src/commands/send_file.rs | 36 - src-tauri/src/commands/set_kv.rs | 36 - src-tauri/src/lib.rs | 13 +- tsconfig.json | 10 +- 73 files changed, 823 insertions(+), 1503 deletions(-) delete mode 100644 common/index.tsx delete mode 100644 common/types/api.ts delete mode 100644 common/types/c2s.ts rename framework/common/types/c2s.ts => common/types/c2sResponses.ts (51%) create mode 100644 common/types/errors.ts delete mode 100644 common/types/hooks.ts create mode 100644 common/types/p2pResponses.ts delete mode 100644 common/types/user.ts delete mode 100644 framework/app/api/config.ts delete mode 100644 framework/app/c2s/index.ts delete mode 100644 framework/app/c2s/useConnect_c2s.ts delete mode 100644 framework/app/c2s/useDisconnect_c2s.ts delete mode 100644 framework/app/c2s/useGetAccountInfo_c2s.ts delete mode 100644 framework/app/c2s/useRegister_c2s.ts delete mode 100644 framework/app/const.ts delete mode 100644 framework/app/hooks.ts delete mode 100644 framework/app/index.tsx delete mode 100644 framework/app/messaging/index.ts delete mode 100644 framework/app/messaging/useMessage.ts delete mode 100644 framework/app/types/hooks.ts delete mode 100644 framework/app/utils/invoke-api.ts delete mode 100644 framework/common/c2s/index.ts delete mode 100644 framework/common/c2s/useConnect_c2s.ts delete mode 100644 framework/common/c2s/useDisconnect_c2s.ts delete mode 100644 framework/common/c2s/useGetAccountInfo_c2s.ts delete mode 100644 framework/common/c2s/useRegister_c2s.ts delete mode 100644 framework/common/index.tsx delete mode 100644 framework/common/messaging/index.ts delete mode 100644 framework/common/messaging/useMessage.ts delete mode 100644 framework/common/types/api.ts delete mode 100644 framework/common/types/hooks.ts delete mode 100644 framework/common/types/messages.ts delete mode 100644 framework/common/types/user.ts delete mode 100644 framework/common/utils/useHook.ts delete mode 100644 framework/redux/store.ts create mode 100644 hooks/c2s/useDisconnect.ts create mode 100644 hooks/c2s/useListAllPeers.ts delete mode 100644 hooks/hooks.ts create mode 100644 hooks/p2p/usePeerConnect.ts create mode 100644 hooks/p2p/usePeerDisconnect.ts create mode 100644 hooks/p2p/usePeerRegister.ts delete mode 100644 hooks/redux/slices/streamHandler.slice.ts delete mode 100644 hooks/redux/slices/uuid.slice.ts delete mode 100644 hooks/types/hooks.ts rename {framework/redux => redux}/slices/streamHandler.slice.ts (61%) rename {framework/redux => redux}/slices/uuid.slice.ts (100%) rename {hooks/redux => redux}/store.ts (77%) delete mode 100644 src-tauri/src/commands/clear_all_kv.rs delete mode 100644 src-tauri/src/commands/del_kv.rs delete mode 100644 src-tauri/src/commands/download_file.rs delete mode 100644 src-tauri/src/commands/get_all_kv.rs delete mode 100644 src-tauri/src/commands/get_kv.rs create mode 100644 src-tauri/src/commands/list_all_peers.rs delete mode 100644 src-tauri/src/commands/send_file.rs delete mode 100644 src-tauri/src/commands/set_kv.rs diff --git a/common/index.tsx b/common/index.tsx deleted file mode 100644 index 0c14ddf..0000000 --- a/common/index.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { createContext, ReactNode, useContext, useMemo } from 'react'; -import { ApiProviderContext } from './types/api'; -import { ApiHooks } from 'hooks/types/hooks'; - -interface ApiProviderProps { - children: ReactNode | ReactNode[]; - hooks: ApiHooks; -} - -export const ApiContext = createContext>({}); - -export const ApiProvider = ({ children, hooks }: ApiProviderProps) => { - const coreConfig = useMemo(() => { - return { - hooks, - }; - }, [hooks]); - - return ( - {children} - ); -}; - -export const useApiProvider = () => { - return useContext(ApiContext) as ApiProviderContext; -}; diff --git a/common/types/api.ts b/common/types/api.ts deleted file mode 100644 index f51520d..0000000 --- a/common/types/api.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { ApiHooks } from 'hooks/types/hooks'; - -export interface ApiInvokerOptions { - type: ApiInvokeTypes; -} - -export type Variables = { [key: string]: string }; -export type ApiInvokeTypes = - | 'message' - | 'register' - | 'connect' - | 'disconnect' - | 'getSession' - | 'getAccInfo'; - -export interface ApiProviderContext { - hooks: ApiHooks; -} -export type ApiInvokerResults = { - data: T; -}; diff --git a/common/types/c2s.ts b/common/types/c2s.ts deleted file mode 100644 index 075d9ff..0000000 --- a/common/types/c2s.ts +++ /dev/null @@ -1,26 +0,0 @@ -export type ServiceTCPConnectionAccepted = { - ServiceConnectionAccepted: { - id: string; - }; -}; - -export type ServiceRegisterAccepted = { - ServiceRegisterAccepted: { - id: string; - request_id: string; - }; -}; - -export type ServiceConnectionAccepted = { - ServiceConnectionAccepted: { - id: string; - request_id: string; - }; -}; - -export type ServiceDisconnect = { - ServiceDisconnectAccepted: { - uuid: string; - request_id: string; - }; -}; diff --git a/framework/common/types/c2s.ts b/common/types/c2sResponses.ts similarity index 51% rename from framework/common/types/c2s.ts rename to common/types/c2sResponses.ts index 075d9ff..1d9ebdd 100644 --- a/framework/common/types/c2s.ts +++ b/common/types/c2sResponses.ts @@ -1,3 +1,5 @@ +export type Payload = GetSessions | ListAllPeers; + export type ServiceTCPConnectionAccepted = { ServiceConnectionAccepted: { id: string; @@ -24,3 +26,22 @@ export type ServiceDisconnect = { request_id: string; }; }; + +export type ListAllPeers = { + cid: number; + online_status: { [key: number]: boolean }; + request_id: string; +}; +export type PeerSessionInformation = { + cid: number; + peer_cid: number; + peer_username: string; +}; + +export type GetSessions = { + sessions: Array<{ + cid: string; + peer_connections: { [key: number]: PeerSessionInformation }; + }>; + request_id: string; +}; diff --git a/common/types/errors.ts b/common/types/errors.ts new file mode 100644 index 0000000..7ee1812 --- /dev/null +++ b/common/types/errors.ts @@ -0,0 +1,6 @@ +export interface ConnectionFailure { + ConnectionFailure: { + message: string; + request_id: string; + }; +} diff --git a/common/types/hooks.ts b/common/types/hooks.ts deleted file mode 100644 index ebf29c7..0000000 --- a/common/types/hooks.ts +++ /dev/null @@ -1 +0,0 @@ -export type Hook = (input: T) => any; diff --git a/common/types/p2pResponses.ts b/common/types/p2pResponses.ts new file mode 100644 index 0000000..c0f374e --- /dev/null +++ b/common/types/p2pResponses.ts @@ -0,0 +1,16 @@ +export type PeerConnectSuccess = { + cid: number; + request_id: string; +}; + +export type PeerDisconnectSuccess = { + cid: number; + request_id: string; +}; + +export type PeerRegisterSuccess = { + cid: number; + peer_cid: number; + peer_username: string; + request_id: string; +}; diff --git a/common/types/user.ts b/common/types/user.ts deleted file mode 100644 index 2c5f3eb..0000000 --- a/common/types/user.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface User { - email: string; - username: string; - online?: boolean; -} - -export interface UsersState { - users: User[]; - onlineUsersByUsername: string[]; - loading: boolean; - error: string | null; - typingUsers: string[]; -} diff --git a/components/common/Layout/Layout.tsx b/components/common/Layout/Layout.tsx index 2f5f365..9ba6716 100644 --- a/components/common/Layout/Layout.tsx +++ b/components/common/Layout/Layout.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import { ApiProvider } from '@framework/index'; import { Fragment, useState } from 'react'; import { Dialog, Menu, Transition } from '@headlessui/react'; import { Button, Tooltip } from 'flowbite-react'; @@ -19,7 +18,7 @@ import classNames from 'classnames'; import WorkspaceBar from '@components/ui/workspacesBar/WorkspaceBar'; import Link from 'next/link'; import AddServerModal from '@components/ui/AddServer'; -import { useAppSelector } from 'framework/redux/store'; +import { useAppSelector } from 'redux/store'; import { usePathname } from 'next/navigation'; const userNavigation = [ { name: 'Your profile', href: '#' }, @@ -36,6 +35,7 @@ export const Layout = ({ children }: Props) => { const currentUsedSessionCid: string = useAppSelector( (state) => state.context.sessions.current_used_session_server ); + const pathname = usePathname(); console.log(pathname); const [navigation, _] = useState([ @@ -61,462 +61,459 @@ export const Layout = ({ children }: Props) => { current_sessions[currentUsedSessionCid] ?? {} ).length; + console.log('Peers', peers); + return ( - -
- - + + + +
+ + +
-
- - -
- - - -
- -
-
- {/* Sidebar component, swap this element with another sidebar if you like */} -
- - -
- - Citadel Workspace - -
-
-
+ )) + )} + + +
  • +
    + Security type +
    +
    +
      +
    • +
    • +
    • { - navigation.forEach((e) => { - if (e.current === true) e.current = false; - if (e.name === item.name) { - e.current = true; - } - setSidebarOpen(false); - }); - }} - href={item.href + currentUsedSessionCid} - className={classNames( - item.current - ? 'bg-gray-800 text-white' - : 'text-gray-400 hover:text-white hover:bg-gray-800', - 'group flex gap-x-3 rounded-md p-2 text-sm leading-6 font-semibold' - )} + onClick={() => setSidebarOpen(false)} + href="/settings" + className="group -mx-2 flex gap-x-3 rounded-md p-2 text-sm font-semibold leading-6 text-gray-400 hover:bg-gray-800 hover:text-white" > -
    • - ))} -
    -
  • - )} -
  • -
    - Your Peers + +
    -
      - {peers === 0 ? ( - <> - ) : ( - Object.keys(current_sessions).map((key) => ( -
    • - - - - - - {key} - -
    • - )) - )} -
    -
  • - {/* {sec type} */} +
    + + + + + + + {addServerOpen && ( +
    + +
    + )} + {/* Static sidebar for desktop */} +
    + + {/* Sidebar component, swap this element with another sidebar if you like */} +
    + + Citadel Workspace + + -
    + + +
    + +
  • + setSidebarOpen(false)} + href="/settings" + className="group -mx-2 flex gap-x-3 rounded-md p-2 text-sm font-semibold leading-6 text-gray-400 hover:bg-gray-800 hover:text-white" + > +
  • + + + -
    -
    - +
    +
    + - {/* Separator */} -