import { useEffect, useState, useContext } from 'react' import { customCommandsConfig } from '../customCommands' import { ButtonWithMatchesAlert, Context } from './KeybindingsScreen' import Button from './Button' import styles from './KeybindingsScreen.module.css' import Input from './Input' export type CustomCommand = { keys: undefined | string[] gamepad: undefined | string[] type: string inputs: any[] } export type CustomCommandsMap = Record export default ( { customCommands, updateCurrBind, resetBinding, }: { customCommands: CustomCommandsMap, updateCurrBind: (group: string, action: string) => void, resetBinding: (group: string, action: string, inputType: string) => void, } ) => { const { userConfig, setUserConfig } = useContext(Context) const [customConfig, setCustomConfig] = useState({ ...customCommands }) useEffect(() => { setUserConfig({ ...userConfig, custom: { ...customConfig } }) }, [customConfig]) const addNewCommand = (type: string) => { // max key + 1 const newKey = String(Math.max(...Object.keys(customConfig).map(Number).filter(key => !isNaN(key)), 0) + 1) setCustomConfig(prev => { const newCustomConf = { ...prev } newCustomConf[newKey] = { keys: undefined as string[] | undefined, gamepad: undefined as string[] | undefined, type, inputs: [] as any[] } return newCustomConf }) } return <>
{Object.entries(customCommandsConfig).map(([group, { input }]) => (
{group}
{Object.entries(customConfig).filter(([key, data]) => data.type === group).map((commandData, indexOption) => { return })}
))}
} const CustomCommandContainer = ( { indexOption, commandData, updateCurrBind, setCustomConfig, resetBinding, groupData } ) => { const { userConfig } = useContext(Context) const [commandKey, { keys, gamepad, inputs }] = commandData const [group, { input }] = groupData const setInputValue = (optionKey, indexInput, value) => { setCustomConfig(prev => { const newConfig = { ...prev } newConfig[optionKey].inputs = [...prev[optionKey].inputs] newConfig[optionKey].inputs[indexInput] = value return newConfig }) } return
{input.map((obj, indexInput) => { const config = typeof obj === 'function' ? obj(inputs) : obj if (!config) return null return config.type === 'select' ? : setInputValue(commandKey, indexInput, e.target.value)} /> })}
{ userConfig?.['custom']?.[commandKey]?.keys ?
}