Skip to content

ステータスバーを追加#48

Merged
0suu merged 2 commits into
mainfrom
32-温湿度表示を分かりやすくする
Apr 13, 2026

Hidden character warning

The head ref may contain hidden characters: "32-\u6e29\u6e7f\u5ea6\u8868\u793a\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u3059\u308b"
Merged

ステータスバーを追加#48
0suu merged 2 commits into
mainfrom
32-温湿度表示を分かりやすくする

Conversation

@0suu

@0suu 0suu commented Apr 13, 2026

Copy link
Copy Markdown
Owner

No description provided.

@0suu 0suu linked an issue Apr 13, 2026 that may be closed by this pull request
@0suu

0suu commented Apr 13, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 13, 2026

Copy link
Copy Markdown

Deploying switchbotcontroller with  Cloudflare Pages  Cloudflare Pages

Latest commit: a4358f0
Status:⚡️  Build in progress...

View logs

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an EnvironmentStatusBar component to display real-time environmental data in the application header. Key changes include moving device fetching to the App level, adding a persistent pinnedEnvironmentDeviceId setting to the Redux store, and updating the UI to allow sensor selection. The review feedback focuses on performance optimizations within the new component, specifically recommending the use of useMemo and useCallback to memoize device filtering, device lookup, and value formatting logic.

@@ -0,0 +1,176 @@
import React, { useState } from "react";

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

パフォーマンス最適化のために useMemouseCallback を使用するため、これらをインポートに追加してください。

Suggested change
import React, { useState } from "react";
import React, { useState, useMemo, useCallback } from "react";

Comment on lines +38 to +41
const envDevices = devices.filter((device) => {
const def = findDeviceDefinition(device.deviceType);
return def && ENVIRONMENT_DEVICE_KEYS.has(def.key);
});

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

devices 配列のフィルタリング処理は計算コストがかかる可能性があり、また devices は Redux の状態更新(ポーリング等)により頻繁に更新されるため、useMemo を使用して最適化することをお勧めします。

Suggested change
const envDevices = devices.filter((device) => {
const def = findDeviceDefinition(device.deviceType);
return def && ENVIRONMENT_DEVICE_KEYS.has(def.key);
});
const envDevices = useMemo(() => {
return devices.filter((device) => {
const def = findDeviceDefinition(device.deviceType);
return def && ENVIRONMENT_DEVICE_KEYS.has(def.key);
});
}, [devices]);

// Nothing to show if no environment-capable devices exist
if (envDevices.length === 0) return null;

const pinnedDevice = pinnedId ? envDevices.find((d) => d.deviceId === pinnedId) : undefined;

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

pinnedDevice の検索結果を useMemo でメモ化することで、レンダリングごとの不要な再計算を抑制できます。

  const pinnedDevice = useMemo(() => 
    pinnedId ? envDevices.find((d) => d.deviceId === pinnedId) : undefined
  , [envDevices, pinnedId]);

Comment on lines +48 to +58
const getFormattedValue = (deviceId: string, fieldKey: string): string | undefined => {
const device = devices.find((d) => d.deviceId === deviceId);
if (!device) return undefined;
const statusFields = getStatusFieldsForDevice(device.deviceType);
const field = statusFields.find((f: any) => f.key === fieldKey);
if (!field) return undefined;
const status = statusMap[deviceId];
const raw = status ? (status as any)[field.key] : undefined;
if (raw === undefined || raw === null) return undefined;
return field.formatter ? String(field.formatter(raw, status)) : String(raw);
};

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

getFormattedValue 関数はレンダリングのたびに再生成されています。また、この関数は Menu 内のループでも呼び出されるため、useCallback でラップしてメモ化することをお勧めします。併せて、statusFields.find の引数における不要な : any 型指定も削除しています。

Suggested change
const getFormattedValue = (deviceId: string, fieldKey: string): string | undefined => {
const device = devices.find((d) => d.deviceId === deviceId);
if (!device) return undefined;
const statusFields = getStatusFieldsForDevice(device.deviceType);
const field = statusFields.find((f: any) => f.key === fieldKey);
if (!field) return undefined;
const status = statusMap[deviceId];
const raw = status ? (status as any)[field.key] : undefined;
if (raw === undefined || raw === null) return undefined;
return field.formatter ? String(field.formatter(raw, status)) : String(raw);
};
const getFormattedValue = useCallback((deviceId: string, fieldKey: string): string | undefined => {
const device = devices.find((d) => d.deviceId === deviceId);
if (!device) return undefined;
const statusFields = getStatusFieldsForDevice(device.deviceType);
const field = statusFields.find((f) => f.key === fieldKey);
if (!field) return undefined;
const status = statusMap[deviceId];
const raw = status ? (status as any)[field.key] : undefined;
if (raw === undefined || raw === null) return undefined;
return field.formatter ? String(field.formatter(raw, status)) : String(raw);
}, [devices, statusMap]);

Comment on lines +60 to +62
const temp = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "temperature") : undefined;
const hum = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "humidity") : undefined;
const co2 = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "CO2") : undefined;

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

ステータスバーに表示する主要な環境数値(温度・湿度・CO2)は、依存関係が変更されたときのみ再計算されるように useMemo でメモ化することをお勧めします。

Suggested change
const temp = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "temperature") : undefined;
const hum = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "humidity") : undefined;
const co2 = pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "CO2") : undefined;
const { temp, hum, co2 } = useMemo(() => ({
temp: pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "temperature") : undefined,
hum: pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "humidity") : undefined,
co2: pinnedDevice ? getFormattedValue(pinnedDevice.deviceId, "CO2") : undefined,
}), [pinnedDevice, getFormattedValue]);

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 242f71628b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/renderer/src/App.tsx
Comment on lines +113 to +115
if (isTokenValid && storedToken) {
dispatch(fetchDevices());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Clear cached devices when auth is no longer valid

This effect fetches devices after validation succeeds but has no corresponding branch to clear previously loaded devices when credentials are removed or become invalid. In that scenario, devices/deviceStatuses stay populated and the new global EnvironmentStatusBar can continue showing stale sensor data from the prior authenticated session until another successful fetch occurs. Add a cleanup path (for example dispatching clearDevices() when !isTokenValid or token is missing) so UI state matches auth state.

Useful? React with 👍 / 👎.

@0suu 0suu merged commit 7f09217 into main Apr 13, 2026
1 of 2 checks passed
@0suu 0suu deleted the 32-温湿度表示を分かりやすくする branch April 13, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

温湿度表示を分かりやすくする

1 participant