prevent infinite update

This commit is contained in:
Rohid
2024-08-29 14:37:20 +06:00
parent 391b75e0c3
commit fc3fd04834

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import config, { SuccessOrFailType } from '../config/config'; import config, { SuccessOrFailType } from '../config/config';
import apiKeyInvalid from '../utils/apiKey'; import apiKeyInvalid from '../utils/apiKey';
import { IS_CHROME } from '../utils/operatingSystem'; import { IS_CHROME } from '../utils/operatingSystem';
@@ -31,15 +31,17 @@ export default function Options(): JSX.Element {
trackSocialMedia: config.trackSocialMedia, trackSocialMedia: config.trackSocialMedia,
}); });
const isApiKeyValid = useMemo(() => apiKeyInvalid(state.apiKey) === '', [state.apiKey]);
const loggingStyleRef = useRef(null); const loggingStyleRef = useRef(null);
const restoreSettings = useCallback(async (): Promise<void> => { const restoreSettings = useCallback(async () => {
const settings = await getSettings(); const settings = await getSettings();
setState({ setState((oldState) => ({
...state, ...oldState,
...settings, ...settings,
}); }));
}, [state]); }, []);
useEffect(() => { useEffect(() => {
void restoreSettings(); void restoreSettings();
@@ -47,7 +49,7 @@ export default function Options(): JSX.Element {
const handleSubmit = async () => { const handleSubmit = async () => {
if (state.loading) return; if (state.loading) return;
setState({ ...state, loading: true }); setState((oldState) => ({ ...oldState, loading: true }));
if (state.apiUrl.endsWith('/')) { if (state.apiUrl.endsWith('/')) {
state.apiUrl = state.apiUrl.slice(0, -1); state.apiUrl = state.apiUrl.slice(0, -1);
} }
@@ -72,50 +74,47 @@ export default function Options(): JSX.Element {
} }
}; };
const updateDenyListState = useCallback( const updateDenyListState = useCallback((sites: string) => {
(sites: string) => { setState((oldState) => ({
setState({ ...oldState,
...state,
denyList: sites.trim().split('\n'), denyList: sites.trim().split('\n'),
}); }));
}, }, []);
[state],
);
const updateAllowListState = useCallback( const updateAllowListState = useCallback((sites: string) => {
(sites: string) => { setState((oldState) => ({
setState({ ...oldState,
...state,
allowList: sites.trim().split('\n'), allowList: sites.trim().split('\n'),
}); }));
}, }, []);
[state],
);
const updateLoggingStyle = (style: string) => { const updateLoggingStyle = useCallback((style: string) => {
setState({ setState((oldState) => ({
...state, ...oldState,
loggingStyle: style === 'allow' ? 'allow' : 'deny', loggingStyle: style === 'allow' ? 'allow' : 'deny',
}); }));
}; }, []);
const updateLoggingType = (type: string) => { const updateLoggingType = useCallback((type: string) => {
setState({ setState((oldState) => ({
...state, ...oldState,
loggingType: type === 'url' ? 'url' : 'domain', loggingType: type === 'url' ? 'url' : 'domain',
}); }));
}; }, []);
const updateTheme = (theme: string) => { const updateTheme = useCallback((theme: string) => {
setState({ setState((oldState) => ({
...state, ...oldState,
theme: theme === 'light' ? 'light' : 'dark', theme: theme === 'light' ? 'light' : 'dark',
}); }));
}; }, []);
const toggleSocialMedia = () => { const toggleSocialMedia = useCallback(() => {
setState({ ...state, trackSocialMedia: !state.trackSocialMedia }); setState((oldState) => ({
}; ...oldState,
trackSocialMedia: !oldState.trackSocialMedia,
}));
}, []);
const loggingStyle = useCallback(() => { const loggingStyle = useCallback(() => {
// TODO: rewrite SitesList to be structured inputs instead of textarea // TODO: rewrite SitesList to be structured inputs instead of textarea
@@ -147,8 +146,6 @@ export default function Options(): JSX.Element {
updateDenyListState, updateDenyListState,
]); ]);
const isApiKeyValid = apiKeyInvalid(state.apiKey) === '';
return ( return (
<div className="container"> <div className="container">
<div className="row"> <div className="row">