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

dev: enforce React hooks rules in ESLint #2149

Merged
merged 1 commit into from
Aug 20, 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
11 changes: 11 additions & 0 deletions assets/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";
import pluginReactHooks from "eslint-plugin-react-hooks";
import eslintConfigPrettier from "eslint-config-prettier";
import jestPlugin from "eslint-plugin-jest";

Expand All @@ -15,6 +16,16 @@ export default [
files: ["test/**"],
...jestPlugin.configs["flat/style"],
},
{
files: ["**/*.{ts, tsx}"],
plugins: {
"react-hooks": pluginReactHooks,
},
rules: {
...pluginReactHooks.configs.recommended.rules,
"react-hooks/exhaustive-deps": "error",
},
},
{
settings: { react: { version: "detect" } },
rules: {
Expand Down
13 changes: 13 additions & 0 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.5.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.2",
"exit_on_eof": "^1.0.4",
"file-loader": "^6.2.0",
"fishery": "^2.2.2",
Expand Down
31 changes: 16 additions & 15 deletions assets/src/hooks/v2/use_refresh_rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@ interface RefreshRateConfig {

const useRefreshRate = (): RefreshRateConfig => {
const screenId = useScreenID();
// Live OFM screens ignore any configured refreshRate.
// Hardcoding to 0 prevents an interval from being started unnecessarily.
const refreshRate = isOFM() ? "0" : fetchDatasetValue("refreshRate");
const refreshRateOffset = getDatasetValue("refreshRateOffset") || "0";
const screenIdsWithOffsetMap = getDatasetValue("screenIdsWithOffsetMap");

const refreshRateMs = parseFloat(refreshRate) * 1000;
return useMemo(() => {
// Live OFM screens ignore any configured refreshRate.
// Hardcoding to 0 prevents an interval from being started unnecessarily.
const refreshRate = isOFM() ? "0" : fetchDatasetValue("refreshRate");
const refreshRateOffset = getDatasetValue("refreshRateOffset") || "0";
const screenIdsWithOffsetMap = getDatasetValue("screenIdsWithOffsetMap");

const refreshRateMs = parseFloat(refreshRate) * 1000;

let refreshRateOffsetMs;

const refreshRateOffsetMs = useMemo(() => {
if (screenIdsWithOffsetMap) {
const screens = JSON.parse(screenIdsWithOffsetMap);
return _.find(screens, { id: screenId }).refresh_rate_offset * 1000;
refreshRateOffsetMs =
_.find(screens, { id: screenId }).refresh_rate_offset * 1000;
} else {
refreshRateOffsetMs = parseFloat(refreshRateOffset) * 1000;
}

return parseFloat(refreshRateOffset) * 1000;
}, []);

return {
refreshRateMs,
refreshRateOffsetMs,
};
return { refreshRateMs, refreshRateOffsetMs };
}, [screenId]);
};

export default useRefreshRate;
Loading