Skip to content

Commit

Permalink
Remove uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
Raduc4 committed Jan 18, 2024
1 parent 04501a3 commit 49215cf
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 129 deletions.
26 changes: 26 additions & 0 deletions common/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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<Partial<ApiProviderContext>>({});

export const ApiProvider = ({ children, hooks }: ApiProviderProps) => {
const coreConfig = useMemo(() => {
return {
hooks,
};
}, [hooks]);

return (
<ApiContext.Provider value={coreConfig}>{children}</ApiContext.Provider>
);
};

export const useApiProvider = () => {
return useContext(ApiContext) as ApiProviderContext;
};
21 changes: 21 additions & 0 deletions common/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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<T> = {
data: T;
};
26 changes: 26 additions & 0 deletions common/types/c2s.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
};
};
1 change: 1 addition & 0 deletions common/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Hook<T = any> = (input: T) => any;
13 changes: 13 additions & 0 deletions common/types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface User {
email: string;
username: string;
online?: boolean;
}

export interface UsersState {
users: User[];
onlineUsersByUsername: string[];
loading: boolean;
error: string | null;
typingUsers: string[];
}
2 changes: 2 additions & 0 deletions hooks/c2s/useC2SConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { invoke } from '@tauri-apps/api/core';
export const handler = (input: any) => {};
4 changes: 4 additions & 0 deletions hooks/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const API_URL =
process.env.NEXT_PUBLIC_FRAMEWORK === 'citadel_local'
? process.env.CITADEL_PUBLIC_LOCAL_DOMAIN
: process.env.CITADEL_PUBLIC_DOMAIN;
6 changes: 6 additions & 0 deletions hooks/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { handler as useConnect_c2s } from './c2s/useC2SConnect';
export const appHooks = {
c2s: {
useConnect: useConnect_c2s,
},
};
55 changes: 55 additions & 0 deletions hooks/redux/slices/streamHandler.slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createSlice } from '@reduxjs/toolkit';

type Sessions = {
current_used_session_server: string;
current_sessions: {
[key: string]: { [key: string]: string };
};
};
export type ContextType = 'Register' | 'GetSession';

const initialState: {
context: {
[key: string]: ContextType;
};
sessions: Sessions;
} = {
context: {},
sessions: {
current_used_session_server: '',
current_sessions: {},
},
};

const streamExecSlice = createSlice({
name: 'stram_handler',
initialState,
reducers: {
addToContext: (state, action) => {
const req_id = action.payload.req_id;

const context_type: ContextType =
action.payload.context_type ?? state.context[req_id];
const payload: { [key: string]: string | number } =
action.payload.payload;

let updatedObject: { [key: string]: string | number } = {};

for (const key in payload) {
if (key != 'request_id') {
updatedObject[key] = payload[key];
}
}

state.context[req_id] = context_type;
},

setCurrentServer: (state, action) => {
state.sessions.current_used_session_server = action.payload;
},
},
});

const { reducer, actions } = streamExecSlice;
export const { addToContext, setCurrentServer } = actions;
export default reducer;
18 changes: 18 additions & 0 deletions hooks/redux/slices/uuid.slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
uuid: '',
};
const showSwitchToBusinessOverlaySlice = createSlice({
name: 'uuid',
initialState,
reducers: {
setUuid: (state, action) => {
state.uuid = action.payload;
},
},
});

const { reducer, actions } = showSwitchToBusinessOverlaySlice;
export const { setUuid } = actions;
export default reducer;
40 changes: 40 additions & 0 deletions hooks/redux/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { configureStore } from '@reduxjs/toolkit';
import uuid from './slices/uuid.slice';
import executor from './slices/streamHandler.slice';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import {
ServiceRegisterAccepted,
ServiceTCPConnectionAccepted,
} from '@common/types/c2s';
const stringMiddleware =
() =>
(next: any) =>
(action: string | { type: string; payload?: unknown }) => {
if (typeof action === 'string') {
return next({ type: action });
}
return next(action);
};

const store = configureStore({
reducer: { uuid, context: executor },
devTools: process.env.NODE_ENV !== 'production',
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(stringMiddleware),
});

export default store;

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

type Data = ServiceRegisterAccepted | ServiceTCPConnectionAccepted;

export type ContextType =
| 'RegisterAndConnect'
| 'GetSession'
| 'ADD_TO_CONTEXT'
| 'HANDLE_PACKET';
7 changes: 7 additions & 0 deletions hooks/types/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Hook } from '@common/types/hooks';

export interface ApiHooks {
c2s: {
useConnect: Hook;
};
}
13 changes: 13 additions & 0 deletions hooks/utils/invoke-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiInvokeTypes, Variables } from '@common/types/api';
import { invoke } from '@tauri-apps/api/core';

const invokeApi = async (type: ApiInvokeTypes, variables: Variables) => {
try {
const data = await invoke(type, variables);
return { data };
} catch (error: any) {
throw new Error(error);
}
};

export default invokeApi;
51 changes: 22 additions & 29 deletions src-tauri/src/commands/connect.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
use citadel_workspace_types::InternalServiceRequest::Connect;
use futures::SinkExt;
use tauri::State;
use uuid::Uuid;

use crate::structs::ConnectionState;

#[tauri::command]
pub async fn connect(
uuid: String,
username: String,
password: String,
request_id: String,
state: State<'_, ConnectionState>,
) -> Result<String, String> {
match Uuid::parse_str(&uuid) {
Ok(uuid) => {
let payload = Connect {
username,
password: password.into_bytes().into(),
connect_mode: Default::default(),
udp_mode: Default::default(),
keep_alive_timeout: Default::default(),
session_security_settings: Default::default(),
request_id: request_id.parse().unwrap(),
};
if state
.sink
.lock()
.await
.as_mut()
.unwrap()
.send(bincode2::serialize(&payload).unwrap().into())
.await
.is_ok()
{
Ok(request_id.to_string())
} else {
Err("Unable to connect".to_string())
}
}
Err(_) => return Err("Invalid UUID".to_string()),
let payload = Connect {
username,
password: password.into_bytes().into(),
connect_mode: Default::default(),
udp_mode: Default::default(),
keep_alive_timeout: Default::default(),
session_security_settings: Default::default(),
request_id: request_id.parse().unwrap(),
};
if state
.sink
.lock()
.await
.as_mut()
.unwrap()
.send(bincode2::serialize(&payload).unwrap().into())
.await
.is_ok()
{
Ok(request_id.to_string())
} else {
Err("Unable to connect".to_string())
}
}
41 changes: 17 additions & 24 deletions src-tauri/src/commands/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,30 @@ use crate::structs::ConnectionState;
use citadel_workspace_types::InternalServiceRequest::Disconnect;
use futures::SinkExt;
use tauri::State;
use uuid::Uuid;

#[tauri::command]
pub async fn disconnect(
uuid: String,
cid: u64,
request_id: String,
state: State<'_, ConnectionState>,
) -> Result<String, String> {
match Uuid::parse_str(&uuid) {
Ok(uuid) => {
let payload = Disconnect {
cid,
request_id: request_id.parse().unwrap(),
};
let payload = Disconnect {
cid,
request_id: request_id.parse().unwrap(),
};

if state
.sink
.lock()
.await
.as_mut()
.unwrap()
.send(bincode2::serialize(&payload).unwrap().into())
.await
.is_ok()
{
Ok(request_id.to_string())
} else {
Err("Unable to disconnect".to_string())
}
}
Err(_) => return Err("Invalid UUID".to_string()),
if state
.sink
.lock()
.await
.as_mut()
.unwrap()
.send(bincode2::serialize(&payload).unwrap().into())
.await
.is_ok()
{
Ok(request_id.to_string())
} else {
Err("Unable to disconnect".to_string())
}
}
Loading

0 comments on commit 49215cf

Please sign in to comment.