-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
328 additions
and
129 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,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; | ||
}; |
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,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; | ||
}; |
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,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; | ||
}; | ||
}; |
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 @@ | ||
export type Hook<T = any> = (input: T) => any; |
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,13 @@ | ||
export interface User { | ||
email: string; | ||
username: string; | ||
online?: boolean; | ||
} | ||
|
||
export interface UsersState { | ||
users: User[]; | ||
onlineUsersByUsername: string[]; | ||
loading: boolean; | ||
error: string | null; | ||
typingUsers: string[]; | ||
} |
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,2 @@ | ||
import { invoke } from '@tauri-apps/api/core'; | ||
export const handler = (input: any) => {}; |
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,4 @@ | ||
export const API_URL = | ||
process.env.NEXT_PUBLIC_FRAMEWORK === 'citadel_local' | ||
? process.env.CITADEL_PUBLIC_LOCAL_DOMAIN | ||
: process.env.CITADEL_PUBLIC_DOMAIN; |
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 @@ | ||
import { handler as useConnect_c2s } from './c2s/useC2SConnect'; | ||
export const appHooks = { | ||
c2s: { | ||
useConnect: useConnect_c2s, | ||
}, | ||
}; |
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,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; |
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,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; |
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,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'; |
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,7 @@ | ||
import { Hook } from '@common/types/hooks'; | ||
|
||
export interface ApiHooks { | ||
c2s: { | ||
useConnect: Hook; | ||
}; | ||
} |
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,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; |
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,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()) | ||
} | ||
} |
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.