import Store from 'electron-store'; import React, { useContext, useEffect, useReducer, useState } from "react"; import { SettingsContext } from "./App"; import Ajv from 'ajv'; import './css/settings.css'; import MicrophoneSoundBar from './MicrophoneSoundBar'; import TestSpeakersButton from './TestSpeakersButton'; const keys = new Set(['Space', 'Backspace', 'Delete', 'Enter', 'Up', 'Down', 'Left', 'Right', 'Home', 'End', 'PageUp', 'PageDown', 'Escape', 'LControl', 'LShift', 'LAlt', 'RControl', 'RShift', 'RAlt']); const validateURL = new Ajv({ allErrors: true, format: 'full' }).compile({ type: 'string', format: 'uri' }); const store = new Store({ migrations: { '1.1.3': store => { const serverIP = store.get('serverIP'); if (typeof serverIP === 'string') { const serverURL = `http://${serverIP}`; if (validateURL(serverURL)) { store.set('serverURL', serverURL) } else { console.warn('Error while parsing the old serverIP property. Default URL will be used instead.'); } // @ts-ignore: Old serverIP property no longer exists in ISettings store.delete('serverIP') } }, '1.1.5': store => { const serverURL = store.get('serverURL'); if (serverURL === 'http://54.193.94.35:9736') { store.set('serverURL', 'https://crewl.ink'); } } }, schema: { alwaysOnTop: { type: 'boolean', default: false }, microphone: { type: 'string', default: 'Default' }, speaker: { type: 'string', default: 'Default' }, pushToTalk: { type: 'boolean', default: false, }, serverURL: { type: 'string', default: 'https://crewl.ink', format: 'uri' }, pushToTalkShortcut: { type: 'string', default: 'V' }, deafenShortcut: { type: 'string', default: 'RControl' }, offsets: { type: 'object', properties: { version: { type: 'string', default: '' }, data: { type: 'string', default: '' } } }, hideCode: { type: 'boolean', default: false }, stereoInLobby: { type: 'boolean', default: true } } }); store.onDidChange('serverURL', (newUrl, oldUrl) => { if (newUrl === 'http://54.193.94.35:9736') { store.set('serverURL', 'https://crewl.ink'); } }); export interface SettingsProps { open: boolean; onClose: any; } export interface ISettings { alwaysOnTop: boolean; microphone: string; speaker: string; pushToTalk: boolean; serverURL: string; pushToTalkShortcut: string; deafenShortcut: string; offsets: { version: string; data: string; }, hideCode: boolean; stereoInLobby: boolean; } export const settingsReducer = (state: ISettings, action: { type: 'set' | 'setOne', action: [string, any] | ISettings }): ISettings => { if (action.type === 'set') return action.action as ISettings; let v = (action.action as [string, any]); store.set(v[0], v[1]); return { ...state, [v[0]]: v[1] }; } interface MediaDevice { id: string; kind: MediaDeviceKind; label: string; } type URLInputProps = { initialURL: string, onValidURL: (url: string) => void }; function URLInput({ initialURL, onValidURL }: URLInputProps) { const [isValidURL, setURLValid] = useState(true); const [currentURL, setCurrentURL] = useState(initialURL); useEffect(() => { setCurrentURL(initialURL); }, [initialURL]); function onChange(event: React.ChangeEvent) { setCurrentURL(event.target.value); if (validateURL(event.target.value)) { setURLValid(true); onValidURL(event.target.value); } else { setURLValid(false); } } return } export default function Settings({ open, onClose }: SettingsProps) { const [settings, setSettings] = useContext(SettingsContext); const [unsavedCount, setUnsavedCount] = useState(0); const unsaved = unsavedCount > 2; useEffect(() => { setSettings({ type: 'set', action: store.store }); }, []); useEffect(() => { setUnsavedCount(s => s + 1); }, [settings.microphone, settings.speaker, settings.serverURL]); const [devices, setDevices] = useState([]); const [_, updateDevices] = useReducer((state) => state + 1, 0); useEffect(() => { navigator.mediaDevices.enumerateDevices() .then(devices => setDevices(devices .map(d => { let label = d.label; if (d.deviceId === 'default') { label = "Default"; } else { let match = /\((.+?)\)/.exec(d.label); if (match && match[1]) label = match[1]; } return { id: d.deviceId, kind: d.kind, label }; }) )); }, [_]); const setShortcut = (ev: React.KeyboardEvent, shortcut: string) => { let k = ev.key; if (k.length === 1) k = k.toUpperCase(); else if (k.startsWith('Arrow')) k = k.substring(5); if (k === ' ') k = 'Space'; if (k === 'Control' || k === 'Alt' || k === 'Shift') k = (ev.location === 1 ? 'L' : 'R') + k; if (/^[0-9A-Z]$/.test(k) || /^F[0-9]{1,2}$/.test(k) || keys.has(k) ) { setSettings({ type: 'setOne', action: [shortcut, k] }); } }; const microphones = devices.filter(d => d.kind === 'audioinput'); const speakers = devices.filter(d => d.kind === 'audiooutput'); return
{ if (unsaved) { onClose(); location.reload(); } else onClose(); }}> {/*
{ ipcRenderer.send('alwaysOnTop', !settings.alwaysOnTop); setSettings({ type: 'setOne', action: ['alwaysOnTop', !settings.alwaysOnTop] }); }}> Always on Top
*/}
{open && }
{open && }
setSettings({ type: 'setOne', action: ['pushToTalk', false] })}>
setSettings({ type: 'setOne', action: ['pushToTalk', true] })}>
{settings.pushToTalk &&
setShortcut(ev, 'pushToTalkShortcut')} />
}
setShortcut(ev, 'deafenShortcut')} />
{ setSettings({ type: 'setOne', action: ['serverURL', url] }) }} />
setSettings({ type: 'setOne', action: ['hideCode', !settings.hideCode] })}>
setSettings({ type: 'setOne', action: ['stereoInLobby', !settings.stereoInLobby] })}>
}