Skip to content

Commit ce0616c

Browse files
committed
🎨 refactor device types & libhmk APIs
1 parent f1effb3 commit ce0616c

42 files changed

Lines changed: 1247 additions & 980 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/api/use-get-keymap-with-advanced-keys.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceAdvancedKey, DeviceAKType } from "@/types/devices"
1817
import { Keycode } from "@/types/keycodes"
18+
import { HMKAdvancedKey, HMKAKType } from "@/types/libhmk"
1919
import { produce } from "immer"
2020
import { useGetAdvancedKeys } from "./use-get-advanced-keys"
2121
import { useGetKeymap } from "./use-get-keymap"
@@ -32,7 +32,7 @@ type GetKeymapWithAdvancedKeysReturnType =
3232
isSuccess: true
3333
keymap: number[][]
3434
normalKeymap: number[][]
35-
advancedKeys: DeviceAdvancedKey[]
35+
advancedKeys: HMKAdvancedKey[]
3636
akIndices: (number | null)[][]
3737
}
3838

@@ -56,26 +56,26 @@ export function useGetKeymapWithAdvancedKeys(
5656
for (let i = 0; i < advancedKeys.length; i++) {
5757
const { layer, key, ak } = advancedKeys[i]
5858

59-
if (ak.type !== DeviceAKType.NONE) {
59+
if (ak.type !== HMKAKType.NONE) {
6060
akIndices[layer][key] = i
6161
}
6262

6363
switch (ak.type) {
64-
case DeviceAKType.NULL_BIND:
64+
case HMKAKType.NULL_BIND:
6565
draft[layer][key] = Keycode.AK_NULL_BIND_PRIMARY
6666
draft[layer][ak.secondaryKey] = Keycode.AK_NULL_BIND_SECONDARY
6767
akIndices[layer][ak.secondaryKey] = i
6868
break
6969

70-
case DeviceAKType.DYNAMIC_KEYSTROKE:
70+
case HMKAKType.DYNAMIC_KEYSTROKE:
7171
draft[layer][key] = Keycode.AK_DYNAMIC_KEYSTROKE
7272
break
7373

74-
case DeviceAKType.TAP_HOLD:
74+
case HMKAKType.TAP_HOLD:
7575
draft[layer][key] = Keycode.AK_TAP_HOLD
7676
break
7777

78-
case DeviceAKType.TOGGLE:
78+
case HMKAKType.TOGGLE:
7979
draft[layer][key] = Keycode.AK_TOGGLE
8080
break
8181

src/api/use-set-actuation-map.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceActuation } from "@/types/devices"
17+
import { HMKActuation } from "@/types/libhmk"
1818
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919
import { produce } from "immer"
2020

2121
type SetActuationMapParams = {
2222
start: number
23-
actuationMap: DeviceActuation[]
23+
actuationMap: HMKActuation[]
2424
}
2525

2626
export function useSetActuationMap(profile: number) {
@@ -35,7 +35,7 @@ export function useSetActuationMap(profile: number) {
3535
onMutate: async ({ start, actuationMap }) => {
3636
await queryClient.cancelQueries({ queryKey })
3737
const previousActuationMap =
38-
queryClient.getQueryData<DeviceActuation[]>(queryKey)
38+
queryClient.getQueryData<HMKActuation[]>(queryKey)
3939
queryClient.setQueryData(
4040
queryKey,
4141
produce(previousActuationMap, (draft) => {

src/api/use-set-advanced-keys.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceAdvancedKey } from "@/types/devices"
17+
import { HMKAdvancedKey } from "@/types/libhmk"
1818
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919
import { produce } from "immer"
2020

2121
type SetAdvancedKeysParams = {
2222
start: number
23-
advancedKeys: DeviceAdvancedKey[]
23+
advancedKeys: HMKAdvancedKey[]
2424
}
2525

2626
export function useSetAdvancedKeys(profile: number) {
@@ -35,7 +35,7 @@ export function useSetAdvancedKeys(profile: number) {
3535
onMutate: async ({ start, advancedKeys }) => {
3636
await queryClient.cancelQueries({ queryKey })
3737
const previousAdvancedKeys =
38-
queryClient.getQueryData<DeviceAdvancedKey[]>(queryKey)
38+
queryClient.getQueryData<HMKAdvancedKey[]>(queryKey)
3939
const tmp = produce(previousAdvancedKeys, (draft) => {
4040
if (draft) {
4141
for (let i = 0; i < advancedKeys.length; i++) {

src/api/use-set-calibration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceCalibration } from "@/types/devices"
17+
import { HMKCalibration } from "@/types/libhmk"
1818
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919

2020
export function useSetCalibration() {
@@ -24,11 +24,11 @@ export function useSetCalibration() {
2424
const queryKey = [id, "calibration"]
2525

2626
return useMutation({
27-
mutationFn: (calibration: DeviceCalibration) => setCalibration(calibration),
27+
mutationFn: (calibration: HMKCalibration) => setCalibration(calibration),
2828
onMutate: async (calibration) => {
2929
await queryClient.cancelQueries({ queryKey })
3030
const previousCalibration =
31-
queryClient.getQueryData<DeviceCalibration>(queryKey)
31+
queryClient.getQueryData<HMKCalibration>(queryKey)
3232
queryClient.setQueryData(queryKey, calibration)
3333

3434
return { previousCalibration }

src/api/use-set-gamepad-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceGamepadOptions } from "@/types/devices"
17+
import { HMKGamepadOptions } from "@/types/libhmk"
1818
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919

2020
export function useSetGamepadOptions(profile: number) {
@@ -24,12 +24,12 @@ export function useSetGamepadOptions(profile: number) {
2424
const queryKey = [id, profile, "gamepadOptions"]
2525

2626
return useMutation({
27-
mutationFn: (options: DeviceGamepadOptions) =>
27+
mutationFn: (options: HMKGamepadOptions) =>
2828
setGamepadOptions(profile, options),
2929
onMutate: async (options) => {
3030
await queryClient.cancelQueries({ queryKey })
3131
const previousOptions =
32-
queryClient.getQueryData<DeviceGamepadOptions>(queryKey)
32+
queryClient.getQueryData<HMKGamepadOptions>(queryKey)
3333
queryClient.setQueryData(queryKey, options)
3434

3535
return { previousOptions }

src/api/use-set-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
import { useDevice } from "@/components/providers/device-provider"
17-
import { DeviceOptions } from "@/types/devices"
17+
import { HMKOptions } from "@/types/libhmk"
1818
import { useMutation, useQueryClient } from "@tanstack/react-query"
1919

2020
export function useSetOptions() {
@@ -24,10 +24,10 @@ export function useSetOptions() {
2424
const queryKey = [id, "options"]
2525

2626
return useMutation({
27-
mutationFn: (options: DeviceOptions) => setOptions(options),
27+
mutationFn: (options: HMKOptions) => setOptions(options),
2828
onMutate: async (options) => {
2929
await queryClient.cancelQueries({ queryKey })
30-
const previousOptions = queryClient.getQueryData<DeviceOptions>(queryKey)
30+
const previousOptions = queryClient.getQueryData<HMKOptions>(queryKey)
3131
queryClient.setQueryData(queryKey, options)
3232

3333
return { previousOptions }

src/components/app-configurator.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { useHMKDevice } from "@/hooks/use-hmk-device"
1919
import { createConfigurator } from "@/lib/create-configurator"
2020
import { isWebHIDSupported } from "@/lib/utils"
2121
import { useQueryClient } from "@tanstack/react-query"
22-
import { useEffect, useState } from "react"
22+
import { useCallback, useEffect, useState } from "react"
23+
import { toast } from "sonner"
2324
import { Configurator } from "./configurator/configurator"
2425
import { ConfiguratorLayout } from "./configurator/layout"
2526
import { ConfiguratorProvider } from "./providers/configurator-provider"
@@ -36,6 +37,18 @@ export function AppConfigurator() {
3637

3738
const [webHIDSupported, setWebHIDSupported] = useState(false)
3839

40+
const connect = useCallback(async () => {
41+
try {
42+
await hmkDevice.connect()
43+
} catch (error) {
44+
if (error instanceof Error) {
45+
toast.error(`Failed to connect: ${error.message}`)
46+
} else {
47+
console.error(error)
48+
}
49+
}
50+
}, [hmkDevice])
51+
3952
// Prevent SSR errors by only checking WebHID support on the client side
4053
useEffect(() => setWebHIDSupported(isWebHIDSupported()), [])
4154

@@ -44,9 +57,9 @@ export function AppConfigurator() {
4457
queryClient.clear()
4558
reset()
4659
} else if (webHIDSupported) {
47-
hmkDevice.connect()
60+
connect()
4861
}
49-
}, [hmkDevice, queryClient, reset, webHIDSupported])
62+
}, [connect, hmkDevice.status, queryClient, reset, webHIDSupported])
5063

5164
return (
5265
<ConfiguratorProvider configurator={useAppConfigurator()}>
@@ -58,7 +71,7 @@ export function AppConfigurator() {
5871
) : (
5972
<div className="flex w-full flex-1 flex-col items-center justify-center p-12">
6073
{webHIDSupported ? (
61-
<Button onClick={hmkDevice.connect}>Authorize Device</Button>
74+
<Button onClick={connect}>Authorize Device</Button>
6275
) : (
6376
<p>WebHID is not supported in this browser.</p>
6477
)}

src/components/configurator/advanced-keys/advanced-keys-create.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { useConfigurator } from "@/components/providers/configurator-provider"
2020
import { useDevice } from "@/components/providers/device-provider"
2121
import { Button } from "@/components/ui/button"
2222
import { AK_TYPE_TO_METADATA } from "@/constants/advanced-keys"
23-
import { DeviceAKType } from "@/types/devices"
23+
import { HMKAKType } from "@/types/libhmk"
2424
import { ToggleGroup, ToggleGroupItem } from "@radix-ui/react-toggle-group"
2525
import { KeyboardEditorLayout } from "../common/keyboard-editor"
2626
import { KeycodeButton } from "../common/keycode-button"
@@ -57,7 +57,7 @@ export const AdvancedKeysCreate = () => {
5757
variant="outline"
5858
size="sm"
5959
onClick={() => {
60-
setNewAKType(DeviceAKType.NONE)
60+
setNewAKType(HMKAKType.NONE)
6161
setNewAKKeys([null, null])
6262
setNewAKKeysIndex(null)
6363
}}
@@ -86,7 +86,7 @@ export const AdvancedKeysCreate = () => {
8686
),
8787
],
8888
})
89-
setNewAKType(DeviceAKType.NONE)
89+
setNewAKType(HMKAKType.NONE)
9090
setNewAKKeys([null, null])
9191
setNewAKKeysIndex(null)
9292
setAKIndex(null)

src/components/configurator/advanced-keys/advanced-keys-editor.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { useConfigurator } from "@/components/providers/configurator-provider"
2020
import { Button } from "@/components/ui/button"
2121
import { AK_TYPE_TO_METADATA } from "@/constants/advanced-keys"
2222
import { DeviceAdvancedKeyMetadata } from "@/types/advanced-keys"
23-
import { DeviceAdvancedKey, DeviceAKType } from "@/types/devices"
23+
import { HMKAdvancedKey, HMKAKType } from "@/types/libhmk"
2424
import { createContext, useContext, useLayoutEffect } from "react"
2525
import { KeyboardEditorLayout } from "../common/keyboard-editor"
2626
import { AKDeleteDialog } from "./ak-delete-dialog"
@@ -31,7 +31,7 @@ import { TapHoldEditor } from "./tap-hold-editor"
3131
import { ToggleEditor } from "./toggle-editor"
3232

3333
type AdvancedKeysEditorState = {
34-
advancedKeys: DeviceAdvancedKey[]
34+
advancedKeys: HMKAdvancedKey[]
3535
akMetadata: DeviceAdvancedKeyMetadata
3636
akIndex: number
3737
}
@@ -54,8 +54,8 @@ export function AdvancedKeysEditor() {
5454
!isSuccess ||
5555
akIndex === null ||
5656
akIndex >=
57-
advancedKeys.filter(({ ak }) => ak.type !== DeviceAKType.NONE).length ||
58-
advancedKeys[akIndex].ak.type === DeviceAKType.NONE
57+
advancedKeys.filter(({ ak }) => ak.type !== HMKAKType.NONE).length ||
58+
advancedKeys[akIndex].ak.type === HMKAKType.NONE
5959

6060
useLayoutEffect(() => {
6161
if (disabled) {
@@ -96,13 +96,13 @@ export function AdvancedKeysEditor() {
9696
akIndex,
9797
}}
9898
>
99-
{akMetadata.type === DeviceAKType.NULL_BIND ? (
99+
{akMetadata.type === HMKAKType.NULL_BIND ? (
100100
<NullBindEditor />
101-
) : akMetadata.type === DeviceAKType.DYNAMIC_KEYSTROKE ? (
101+
) : akMetadata.type === HMKAKType.DYNAMIC_KEYSTROKE ? (
102102
<DynamicKeystrokeEditor />
103-
) : akMetadata.type === DeviceAKType.TAP_HOLD ? (
103+
) : akMetadata.type === HMKAKType.TAP_HOLD ? (
104104
<TapHoldEditor />
105-
) : akMetadata.type === DeviceAKType.TOGGLE ? (
105+
) : akMetadata.type === HMKAKType.TOGGLE ? (
106106
<ToggleEditor />
107107
) : (
108108
<></>

src/components/configurator/advanced-keys/advanced-keys-menu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
DropdownMenuTrigger,
2626
} from "@/components/ui/dropdown-menu"
2727
import { AK_METADATA, AK_TYPE_TO_METADATA } from "@/constants/advanced-keys"
28-
import { DeviceAKType } from "@/types/devices"
28+
import { HMKAKType } from "@/types/libhmk"
2929
import { Edit, Plus, Trash } from "lucide-react"
3030
import { KeyboardEditorLayout } from "../common/keyboard-editor"
3131
import { useAdvancedKeys } from "./advanced-keys-tab"
@@ -89,7 +89,7 @@ export function AdvancedKeysMenu() {
8989
) : (
9090
<div className="mt-4 grid w-full gap-4">
9191
{advancedKeys
92-
.filter(({ ak }) => ak.type !== DeviceAKType.NONE)
92+
.filter(({ ak }) => ak.type !== HMKAKType.NONE)
9393
.map((advancedKeys, i) => {
9494
const akMetadata = AK_TYPE_TO_METADATA[advancedKeys.ak.type]
9595
return (
@@ -105,7 +105,7 @@ export function AdvancedKeysMenu() {
105105
variant="outline"
106106
size="icon"
107107
onClick={() => {
108-
setNewAKType(DeviceAKType.NONE)
108+
setNewAKType(HMKAKType.NONE)
109109
setNewAKKeys([null, null])
110110
setNewAKKeysIndex(null)
111111
setLayer(advancedKeys.layer)

0 commit comments

Comments
 (0)