Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds button on each profile mapping to copy from base profile #891

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions www/src/Locales/en/PinMapping.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
'pin-header-label': 'Pin',
'profile-pins-warning':
'Try to avoid changing the buttons and/or directions used for the switch profile hotkeys. Otherwise, it will be difficult to understand what profile is being selected!',
'profile-copy-base': 'Copy base profile',
errors: {
conflict: 'Pin {{pin}} is already assigned to {{conflictedMappings}}',
required: '{{button}} is required',
Expand Down
52 changes: 35 additions & 17 deletions www/src/Pages/PinMapping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ type PinsFormTypes = {
savePins: () => void;
pins: { [key: string]: PinActionValues };
setPinAction: (pin: string, action: PinActionValues) => void;
onCopy?: () => void;
};

const PinsForm = ({ savePins, pins, setPinAction }: PinsFormTypes) => {
const PinsForm = ({ savePins, pins, setPinAction, onCopy }: PinsFormTypes) => {
const { buttonLabels, updateUsedPins } = useContext(AppContext);
const [saveMessage, setSaveMessage] = useState('');

Expand Down Expand Up @@ -115,20 +116,29 @@ const PinsForm = ({ savePins, pins, setPinAction }: PinsFormTypes) => {

return (
<Form onSubmit={handleSubmit}>
<div className="py-3">
<CaptureButton
labels={Object.values(buttonNames)}
onChange={(label, pin) =>
setPinAction(
// Convert getHeldPins format to setPinMappings format
parseInt(pin) < 10 ? `pin0${pin}` : `pin${pin}`,
// Maps current mode buttons to actions
BUTTON_ACTIONS[
`BUTTON_PRESS_${invert(buttonNames)[label].toUpperCase()}`
],
)
}
/>
<div className="d-flex">
<div className="py-3">
<CaptureButton
labels={Object.values(buttonNames)}
onChange={(label, pin) =>
setPinAction(
// Convert getHeldPins format to setPinMappings format
parseInt(pin) < 10 ? `pin0${pin}` : `pin${pin}`,
// Maps current mode buttons to actions
BUTTON_ACTIONS[
`BUTTON_PRESS_${invert(buttonNames)[label].toUpperCase()}`
],
)
}
/>
</div>
{onCopy && (
<div className="py-3 mx-3">
<Button variant="secondary" onClick={() => onCopy()}>
{t(`PinMapping:profile-copy-base`)}
</Button>
</div>
)}
</div>
<div className="gx-3">
{pinList.map(([cell1, cell2], i) => (
Expand All @@ -148,8 +158,13 @@ const PinsForm = ({ savePins, pins, setPinAction }: PinsFormTypes) => {

export default function PinMappingPage() {
const { fetchPins, pins, savePins, setPinAction } = usePinStore();
const { fetchProfiles, profiles, saveProfiles, setProfileAction } =
useProfilesStore();
const {
fetchProfiles,
profiles,
saveProfiles,
setProfileAction,
setProfile,
} = useProfilesStore();

const [pressedPin, setPressedPin] = useState<number | null>(null);

Expand Down Expand Up @@ -213,6 +228,9 @@ export default function PinMappingPage() {
setPinAction={(pin, action) => {
setProfileAction(profileIndex, pin, action);
}}
onCopy={() => {
setProfile(profileIndex, pins);
}}
/>
</Tab>
))}
Expand Down
12 changes: 9 additions & 3 deletions www/src/Store/useProfilesStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from 'axios';
import { create } from 'zustand';
import WebApi, { baseUrl } from '../Services/WebApi';
import WebApi from '../Services/WebApi';
import { PinActionValues } from '../Data/Pins';

type ProfileType = { [key: string]: PinActionValues };
Expand All @@ -17,6 +16,7 @@ type Actions = {
action: PinActionValues,
) => void;
saveProfiles: () => Promise<object>;
setProfile: (profileIndex: number, pins: ProfileType) => void;
};

const defaultProfilePins: ProfileType = {
Expand Down Expand Up @@ -71,11 +71,17 @@ const useProfilesStore = create<State & Actions>()((set, get) => ({
setProfileAction: (profileIndex, pin, action) =>
set((state) => ({
...state,
// Update selected profile state
profiles: state.profiles.map((profile, index) =>
index === profileIndex ? { ...profile, [pin]: action } : profile,
),
})),
setProfile: (profileIndex, pins) =>
set((state) => ({
...state,
profiles: state.profiles.map((profile, index) =>
index === profileIndex ? { ...profile, ...pins } : profile,
),
})),
saveProfiles: async () => WebApi.setProfileOptions(get().profiles),
}));

Expand Down