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

Fixed memory leak in CreateInspection component #884

Merged
merged 1 commit into from
Nov 19, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useTranslation } from 'react-i18next';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { useMonkApi } from '@monkvision/network';
import { i18nWrap, useI18nSync, useLoadingState, useMonkAppState } from '@monkvision/common';
import {
i18nWrap,
useI18nSync,
useIsMounted,
useLoadingState,
useMonkAppState,
} from '@monkvision/common';
import { useMonitoring } from '@monkvision/monitoring';
import { useAnalytics } from '@monkvision/analytics';
import { styles } from './CreateInspection.styles';
Expand Down Expand Up @@ -41,6 +47,7 @@ export const CreateInspection = i18nWrap(function CreateInspection({
onInspectionCreated,
}: CreateInspectionProps) {
useI18nSync(lang);
const [retry, setRetry] = useState(0);
const loading = useLoadingState();
const { t } = useTranslation();
const { handleError, setTags } = useMonitoring();
Expand All @@ -51,27 +58,27 @@ export const CreateInspection = i18nWrap(function CreateInspection({
thumbnailDomain: config.thumbnailDomain,
});
const analytics = useAnalytics();

const handleCreateInspection = () => {
loading.start();
if (config?.allowCreateInspection) {
createInspection(config.createInspectionOptions)
.then((res) => {
loading.onSuccess();
setInspectionId(res.id);
})
.catch((err) => {
loading.onError(CreateInspectionError.CREATE_INSPECTION);
handleError(err);
});
}
};
const isMounted = useIsMounted();

useEffect(() => {
if (!inspectionId) {
if (config?.allowCreateInspection) {
loading.start();
handleCreateInspection();
if (config?.allowCreateInspection) {
createInspection(config.createInspectionOptions)
.then((res) => {
if (isMounted()) {
loading.onSuccess();
setInspectionId(res.id);
}
})
.catch((err) => {
if (isMounted()) {
loading.onError(CreateInspectionError.CREATE_INSPECTION);
handleError(err);
}
});
}
} else {
loading.onError(CreateInspectionError.MISSING_INSPECTION_ID);
}
Expand All @@ -80,7 +87,7 @@ export const CreateInspection = i18nWrap(function CreateInspection({
analytics.setUserId(inspectionId);
onInspectionCreated?.();
}
}, [inspectionId, setTags, analytics]);
}, [inspectionId, setTags, analytics, retry, isMounted]);

return (
<div style={styles['container']}>
Expand All @@ -90,7 +97,11 @@ export const CreateInspection = i18nWrap(function CreateInspection({
<div style={styles['errorMessage']}>{t(loading.error)}</div>
{loading.error === CreateInspectionError.CREATE_INSPECTION && (
<div style={styles['retryBtnContainer']}>
<Button variant='outline' icon='refresh' onClick={handleCreateInspection}>
<Button
variant='outline'
icon='refresh'
onClick={() => setRetry((value) => value + 1)}
>
{t('errors.retry')}
</Button>
</div>
Expand Down