Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ottomated committed Dec 27, 2020
1 parent 5c9dcec commit 619a4be
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 145 deletions.
34 changes: 17 additions & 17 deletions src/common/ipc-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ import { ProgressInfo } from 'builder-util-runtime';

// Renderer --> Main (send/on)
export enum IpcMessages {
SHOW_ERROR_DIALOG = 'SHOW_ERROR_DIALOG',
OPEN_AMONG_US_GAME = 'OPEN_AMONG_US_GAME',
RESTART_CREWLINK = 'RESTART_CREWLINK',
QUIT_CREWLINK = 'QUIT_CREWLINK',
SHOW_ERROR_DIALOG = 'SHOW_ERROR_DIALOG',
OPEN_AMONG_US_GAME = 'OPEN_AMONG_US_GAME',
RESTART_CREWLINK = 'RESTART_CREWLINK',
QUIT_CREWLINK = 'QUIT_CREWLINK',
}

// Renderer --> Main (sendSync/on)
export enum IpcSyncMessages {
GET_INITIAL_STATE = 'GET_INITIAL_STATE',
GET_INITIAL_STATE = 'GET_INITIAL_STATE',
}

// Renderer --> Main (invoke/handle)
export enum IpcHandlerMessages {
START_HOOK = 'START_HOOK',
START_HOOK = 'START_HOOK',
}

// Main --> Renderer (send/on)
export enum IpcRendererMessages {
NOTIFY_GAME_OPENED = 'NOTIFY_GAME_OPENED',
NOTIFY_GAME_STATE_CHANGED = 'NOTIFY_GAME_STATE_CHANGED',
TOGGLE_DEAFEN = 'TOGGLE_DEAFEN',
TOGGLE_MUTE = 'TOGGLE_MUTE',
PUSH_TO_TALK = 'PUSH_TO_TALK',
ERROR = 'ERROR',
AUTO_UPDATER_STATE = 'AUTO_UPDATER_STATE',
NOTIFY_GAME_OPENED = 'NOTIFY_GAME_OPENED',
NOTIFY_GAME_STATE_CHANGED = 'NOTIFY_GAME_STATE_CHANGED',
TOGGLE_DEAFEN = 'TOGGLE_DEAFEN',
TOGGLE_MUTE = 'TOGGLE_MUTE',
PUSH_TO_TALK = 'PUSH_TO_TALK',
ERROR = 'ERROR',
AUTO_UPDATER_STATE = 'AUTO_UPDATER_STATE',
}

export interface AutoUpdaterState {
state: 'error' | 'available' | 'downloading' | 'downloaded' | 'unavailable';
error?: string;
progress?: ProgressInfo;
}
state: 'error' | 'available' | 'downloading' | 'downloaded' | 'unavailable';
error?: string;
progress?: ProgressInfo;
}
12 changes: 9 additions & 3 deletions src/main/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import GameReader from './GameReader';
import iohook from 'iohook';
import Store from 'electron-store';
import { ISettings } from '../common/ISettings';
import { IpcHandlerMessages, IpcRendererMessages, IpcSyncMessages } from '../common/ipc-messages';
import {
IpcHandlerMessages,
IpcRendererMessages,
IpcSyncMessages,
} from '../common/ipc-messages';

interface IOHookEvent {
type: string;
Expand All @@ -23,7 +27,9 @@ let gameReader: GameReader;

ipcMain.on(IpcSyncMessages.GET_INITIAL_STATE, (event) => {
if (!readingGame) {
console.error('Recieved GET_INITIAL_STATE message before the START_HOOK message was received');
console.error(
'Recieved GET_INITIAL_STATE message before the START_HOOK message was received'
);
event.returnValue = null;
return;
}
Expand Down Expand Up @@ -157,4 +163,4 @@ function mouseClickMatches(key: M, ev: IOHookEvent): boolean {

function isMouseButton(shortcutKey: string): boolean {
return shortcutKey.includes('MouseButton');
}
}
12 changes: 6 additions & 6 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ function createMainWindow() {
transparent: true,
webPreferences: {
nodeIntegration: true,
webSecurity: false
}
webSecurity: false,
},
});

mainWindowState.manage(window);
if (isDevelopment) {
// Force devtools into detached mode otherwise they are unusable
window.webContents.openDevTools({
mode: 'detach'
mode: 'detach',
});
}

Expand Down Expand Up @@ -91,19 +91,19 @@ if (!gotTheLock) {
autoUpdater.checkForUpdates();
autoUpdater.on('update-available', () => {
mainWindow?.webContents.send(IpcRendererMessages.AUTO_UPDATER_STATE, {
state: 'available'
state: 'available',
});
});
autoUpdater.on('error', (err: string) => {
mainWindow?.webContents.send(IpcRendererMessages.AUTO_UPDATER_STATE, {
state: 'error',
error: err
error: err,
});
});
autoUpdater.on('download-progress', (progress: ProgressInfo) => {
mainWindow?.webContents.send(IpcRendererMessages.AUTO_UPDATER_STATE, {
state: 'downloading',
progress
progress,
});
});
autoUpdater.on('update-downloaded', () => {
Expand Down
41 changes: 25 additions & 16 deletions src/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,36 @@ import path from 'path';
import { IpcMessages } from '../common/ipc-messages';

// Listeners are fire and forget, they do not have "responses" or return values
export const initializeIpcListeners = () => {
ipcMain.on(IpcMessages.SHOW_ERROR_DIALOG, (e, opts: { title: string; content: string; }) => {
if (typeof opts === 'object' && opts && typeof opts.title === 'string' && typeof opts.content === 'string') {
dialog.showErrorBox(opts.title, opts.content);
export const initializeIpcListeners = (): void => {
ipcMain.on(
IpcMessages.SHOW_ERROR_DIALOG,
(e, opts: { title: string; content: string }) => {
if (
typeof opts === 'object' &&
opts &&
typeof opts.title === 'string' &&
typeof opts.content === 'string'
) {
dialog.showErrorBox(opts.title, opts.content);
}
}
});
);

ipcMain.on(IpcMessages.OPEN_AMONG_US_GAME, () => {
// Get steam path from registry
const steamPath = enumerateValues(HKEY.HKEY_LOCAL_MACHINE,
'SOFTWARE\\WOW6432Node\\Valve\\Steam')
.find(v => v.name === 'InstallPath');
const steamPath = enumerateValues(
HKEY.HKEY_LOCAL_MACHINE,
'SOFTWARE\\WOW6432Node\\Valve\\Steam'
).find((v) => v.name === 'InstallPath');
// Check if Steam is installed
if (!steamPath) {
dialog.showErrorBox('Error', 'Could not find your Steam install path.');
} else {
try {
const process = spawn(path.join(steamPath.data as string, 'steam.exe'), [
'-applaunch',
'945360'
]);
const process = spawn(
path.join(steamPath.data as string, 'steam.exe'),
['-applaunch', '945360']
);
process.on('error', () => {
dialog.showErrorBox('Error', 'Please launch the game through Steam.');
});
Expand All @@ -35,12 +44,12 @@ export const initializeIpcListeners = () => {
}
}
});

ipcMain.on(IpcMessages.RESTART_CREWLINK, () => {
app.relaunch();
app.quit();
});

ipcMain.on(IpcMessages.QUIT_CREWLINK, () => {
for (const win of BrowserWindow.getAllWindows()) {
win.close();
Expand All @@ -52,6 +61,6 @@ export const initializeIpcListeners = () => {
// Handlers are async cross-process instructions, they should have a return value
// or the caller should be "await"'ing them. If neither of these are the case
// consider making it a "listener" instead for performance and readability
export const initializeIpcHandlers = () => {
export const initializeIpcHandlers = (): void => {
// TODO: Put handlers here
};
};
102 changes: 71 additions & 31 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import {
LobbySettingsContext,
} from './contexts';
import { ThemeProvider } from '@material-ui/core/styles';
import { AutoUpdaterState, IpcHandlerMessages, IpcMessages, IpcRendererMessages, IpcSyncMessages } from '../common/ipc-messages';
import {
AutoUpdaterState,
IpcHandlerMessages,
IpcMessages,
IpcRendererMessages,
IpcSyncMessages,
} from '../common/ipc-messages';
import theme from './theme';
import SettingsIcon from '@material-ui/icons/Settings';
import CloseIcon from '@material-ui/icons/Close';
Expand Down Expand Up @@ -110,7 +116,9 @@ function App() {
const [gameState, setGameState] = useState<AmongUsState>({} as AmongUsState);
const [settingsOpen, setSettingsOpen] = useState(false);
const [error, setError] = useState('');
const [updaterState, setUpdaterState] = useState<AutoUpdaterState>({ state: 'unavailable' });
const [updaterState, setUpdaterState] = useState<AutoUpdaterState>({
state: 'unavailable',
});
const settings = useReducer(settingsReducer, {
alwaysOnTop: false,
microphone: 'Default',
Expand All @@ -126,7 +134,10 @@ function App() {
maxDistance: 5.32,
},
});
const lobbySettings = useReducer(lobbySettingsReducer, settings[0].localLobbySettings);
const lobbySettings = useReducer(
lobbySettingsReducer,
settings[0].localLobbySettings
);

useEffect(() => {
const onOpen = (_: Electron.IpcRendererEvent, isOpen: boolean) => {
Expand All @@ -139,26 +150,38 @@ function App() {
shouldInit = false;
setError(error);
};
const onAutoUpdaterStateChange = (_: Electron.IpcRendererEvent, state: AutoUpdaterState) => {
setUpdaterState(old => ({...old, ...state}));
}
const onAutoUpdaterStateChange = (
_: Electron.IpcRendererEvent,
state: AutoUpdaterState
) => {
setUpdaterState((old) => ({ ...old, ...state }));
};
let shouldInit = true;
ipcRenderer.invoke(IpcHandlerMessages.START_HOOK).then(() => {
if (shouldInit) {
setGameState(ipcRenderer.sendSync(IpcSyncMessages.GET_INITIAL_STATE));
}
}).catch((error: Error) => {
if (shouldInit) {
shouldInit = false;
setError(error.message);
}
});
ipcRenderer.on(IpcRendererMessages.AUTO_UPDATER_STATE, onAutoUpdaterStateChange);
ipcRenderer
.invoke(IpcHandlerMessages.START_HOOK)
.then(() => {
if (shouldInit) {
setGameState(ipcRenderer.sendSync(IpcSyncMessages.GET_INITIAL_STATE));
}
})
.catch((error: Error) => {
if (shouldInit) {
shouldInit = false;
setError(error.message);
}
});
ipcRenderer.on(
IpcRendererMessages.AUTO_UPDATER_STATE,
onAutoUpdaterStateChange
);
ipcRenderer.on(IpcRendererMessages.NOTIFY_GAME_OPENED, onOpen);
ipcRenderer.on(IpcRendererMessages.NOTIFY_GAME_STATE_CHANGED, onState);
ipcRenderer.on(IpcRendererMessages.ERROR, onError);
return () => {
ipcRenderer.off(IpcRendererMessages.AUTO_UPDATER_STATE, onAutoUpdaterStateChange);
ipcRenderer.off(
IpcRendererMessages.AUTO_UPDATER_STATE,
onAutoUpdaterStateChange
);
ipcRenderer.off(IpcRendererMessages.NOTIFY_GAME_OPENED, onOpen);
ipcRenderer.off(IpcRendererMessages.NOTIFY_GAME_STATE_CHANGED, onState);
ipcRenderer.off(IpcRendererMessages.ERROR, onError);
Expand Down Expand Up @@ -192,20 +215,37 @@ function App() {
<Dialog fullWidth open={updaterState.state !== 'unavailable'}>
<DialogTitle>Updating...</DialogTitle>
<DialogContent>
{((updaterState.state === 'downloading' || updaterState.state === 'downloaded') && updaterState.progress) &&
<>
<LinearProgress variant={updaterState.state === 'downloaded' ? "indeterminate" : "determinate"} value={updaterState.progress.percent} />
<DialogContentText>{prettyBytes(updaterState.progress.transferred)} / {prettyBytes(updaterState.progress.total)}</DialogContentText>
</>
}
{
updaterState.state === 'error' &&
<DialogContentText color="error">{updaterState.error}</DialogContentText>
}
{(updaterState.state === 'downloading' ||
updaterState.state === 'downloaded') &&
updaterState.progress && (
<>
<LinearProgress
variant={
updaterState.state === 'downloaded'
? 'indeterminate'
: 'determinate'
}
value={updaterState.progress.percent}
/>
<DialogContentText>
{prettyBytes(updaterState.progress.transferred)} /{' '}
{prettyBytes(updaterState.progress.total)}
</DialogContentText>
</>
)}
{updaterState.state === 'error' && (
<DialogContentText color="error">
{updaterState.error}
</DialogContentText>
)}
</DialogContent>
{updaterState.state === 'error' &&
<DialogActions><Button href="https://github.com/ottomated/CrewLink/releases/latest">Download Manually</Button></DialogActions>
}
{updaterState.state === 'error' && (
<DialogActions>
<Button href="https://github.com/ottomated/CrewLink/releases/latest">
Download Manually
</Button>
</DialogActions>
)}
</Dialog>
{page}
</ThemeProvider>
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ const Avatar: React.FC<AvatarProps> = function ({
}
break;
case 'novoice':
icon = <LinkOff className={classes.icon} style={{background: '#e67e22', borderColor: '#694900'}}/>;
icon = (
<LinkOff
className={classes.icon}
style={{ background: '#e67e22', borderColor: '#694900' }}
/>
);
break;
case 'disconnected':
icon = <WifiOff className={classes.icon} />;
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ const useStyles = makeStyles((theme) => ({
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'start'
justifyContent: 'start',
},
waiting: {
fontSize: 20,
marginTop: 12,
marginBottom: 12
marginBottom: 12,
},
button: {
color: 'white',
Expand All @@ -40,9 +40,9 @@ const useStyles = makeStyles((theme) => ({
marginTop: 24,
'&:hover': {
borderColor: '#00ff00',
cursor: 'pointer'
}
}
cursor: 'pointer',
},
},
}));

export interface MenuProps {
Expand Down
Loading

0 comments on commit 619a4be

Please sign in to comment.