Skip to content
Merged
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
22 changes: 19 additions & 3 deletions src/renderer/src/components/DeviceControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ export const DeviceControls: React.FC<DeviceControlsProps> = ({
const isInfraredRemote = !!(device as any).isInfraredRemote;

const normalizedType = useMemo(() => rawType, [rawType]);
const normalizedTypeCompact = useMemo(
() => normalizedType.replace(/[^a-z0-9]/g, ""),
[normalizedType]
);

const isBot = !isInfraredRemote && normalizedType === "bot";
const isPlug = !isInfraredRemote && normalizedType.includes("plug");
const isCurtain = !isInfraredRemote && (normalizedType.includes("curtain") || normalizedType.includes("blind tilt"));
const isLock = !isInfraredRemote && normalizedType.includes("lock");
const isCeilingLight = !isInfraredRemote && definition?.key === "ceilingLight";
const isCeilingLight = !isInfraredRemote && (definition?.key === "ceilingLight" || normalizedTypeCompact.includes("ceilinglight"));
const isFloorLamp = !isInfraredRemote && normalizedType.includes("floor lamp");
const isStripLight3 = !isInfraredRemote && normalizedType.includes("strip light 3");
const isStripLight = !isInfraredRemote && normalizedType.includes("strip light");
Expand Down Expand Up @@ -515,7 +519,13 @@ export const DeviceControls: React.FC<DeviceControlsProps> = ({
<Button
size="small"
variant="contained"
onClick={() => sendCommand("turnOn")}
onClick={() => {
if (isCeilingLight) {
sendCommand("setBrightness", Math.round(clamp(brightness, 1, 100)));
return;
}
sendCommand("turnOn");
}}
disabled={controlsDisabled}
fullWidth
>
Expand Down Expand Up @@ -843,7 +853,13 @@ export const DeviceControls: React.FC<DeviceControlsProps> = ({
<Button
size="small"
variant="contained"
onClick={() => sendCommand("turnOn")}
onClick={() => {
if (isCeilingLight) {
sendCommand("setBrightness", Math.round(clamp(brightness, 1, 100)));
return;
}
sendCommand("turnOn");
}}
Comment on lines +856 to +862

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

この onClick ハンドラのロジックは、522-528行目でも使用されており、重複しています。コードの重複をなくし、メンテナンス性を向上させるために、このロジックを関数として抽出することをお勧めします。

例えば、DeviceControls コンポーネント内に handleTurnOn 関数を定義し、両方の Button で再利用できます。

const handleTurnOn = () => {
  if (isCeilingLight) {
    sendCommand("setBrightness", Math.round(clamp(brightness, 1, 100)));
  } else {
    sendCommand("turnOn");
  }
};

そして、この Button と522行目の ButtononClick を提案のように変更してください。

                onClick={handleTurnOn}

disabled={controlsDisabled}
fullWidth
>
Expand Down