R: Fix Rules of React violations
This commit is contained in:
parent
150e6498af
commit
32b41620d8
|
@ -7,6 +7,7 @@ import { TabList, TabPanel, Tabs } from 'react-tabs';
|
||||||
import Modal from '@/components/ui/Modal';
|
import Modal from '@/components/ui/Modal';
|
||||||
import TabLabel from '@/components/ui/TabLabel';
|
import TabLabel from '@/components/ui/TabLabel';
|
||||||
import useRSFormCache from '@/hooks/useRSFormCache';
|
import useRSFormCache from '@/hooks/useRSFormCache';
|
||||||
|
import { LibraryItemID } from '@/models/library';
|
||||||
import { HelpTopic } from '@/models/miscellaneous';
|
import { HelpTopic } from '@/models/miscellaneous';
|
||||||
import {
|
import {
|
||||||
ICstSubstitute,
|
ICstSubstitute,
|
||||||
|
@ -17,6 +18,7 @@ import {
|
||||||
OperationType
|
OperationType
|
||||||
} from '@/models/oss';
|
} from '@/models/oss';
|
||||||
import { SubstitutionValidator } from '@/models/ossAPI';
|
import { SubstitutionValidator } from '@/models/ossAPI';
|
||||||
|
import { ConstituentaID } from '@/models/rsform';
|
||||||
|
|
||||||
import TabArguments from './TabArguments';
|
import TabArguments from './TabArguments';
|
||||||
import TabOperation from './TabOperation';
|
import TabOperation from './TabOperation';
|
||||||
|
@ -48,20 +50,17 @@ function DlgEditOperation({ hideWindow, oss, target, onSubmit }: DlgEditOperatio
|
||||||
const initialInputs = useMemo(() => oss.graph.expandInputs([target.id]), [oss.graph, target.id]);
|
const initialInputs = useMemo(() => oss.graph.expandInputs([target.id]), [oss.graph, target.id]);
|
||||||
const [inputs, setInputs] = useState<OperationID[]>(initialInputs);
|
const [inputs, setInputs] = useState<OperationID[]>(initialInputs);
|
||||||
const inputOperations = useMemo(() => inputs.map(id => oss.operationByID.get(id)!), [inputs, oss.operationByID]);
|
const inputOperations = useMemo(() => inputs.map(id => oss.operationByID.get(id)!), [inputs, oss.operationByID]);
|
||||||
const schemasIDs = useMemo(
|
|
||||||
() => inputOperations.map(operation => operation.result).filter(id => id !== null),
|
const [needPreload, setNeedPreload] = useState(false);
|
||||||
[inputOperations]
|
const [schemasIDs, setSchemaIDs] = useState<LibraryItemID[]>([]);
|
||||||
);
|
|
||||||
|
|
||||||
const [substitutions, setSubstitutions] = useState<ICstSubstitute[]>(target.substitutions);
|
const [substitutions, setSubstitutions] = useState<ICstSubstitute[]>(target.substitutions);
|
||||||
const [suggestions, setSuggestions] = useState<ICstSubstitute[]>([]);
|
const [suggestions, setSuggestions] = useState<ICstSubstitute[]>([]);
|
||||||
|
|
||||||
const cache = useRSFormCache();
|
const cache = useRSFormCache();
|
||||||
const schemas = useMemo(
|
const schemas = useMemo(
|
||||||
() => schemasIDs.map(id => cache.getSchema(id)).filter(item => item !== undefined),
|
() => schemasIDs.map(id => cache.data.find(item => item.id === id)).filter(item => item !== undefined),
|
||||||
// eslint-disable-next-line react-compiler/react-compiler
|
[schemasIDs, cache.data]
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
[schemasIDs, cache.getSchema]
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const isModified = useMemo(
|
const isModified = useMemo(
|
||||||
|
@ -87,11 +86,30 @@ function DlgEditOperation({ hideWindow, oss, target, onSubmit }: DlgEditOperatio
|
||||||
|
|
||||||
const canSubmit = useMemo(() => isModified && alias !== '', [isModified, alias]);
|
const canSubmit = useMemo(() => isModified && alias !== '', [isModified, alias]);
|
||||||
|
|
||||||
|
const getSchemaByCst = useCallback(
|
||||||
|
(id: ConstituentaID) => {
|
||||||
|
for (const schema of cache.data) {
|
||||||
|
const cst = schema.items.find(cst => cst.id === id);
|
||||||
|
if (cst) {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
[cache.data]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
cache.preload(schemasIDs);
|
setNeedPreload(true);
|
||||||
// eslint-disable-next-line react-compiler/react-compiler
|
setSchemaIDs(inputOperations.map(operation => operation.result).filter(id => id !== null));
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [inputOperations]);
|
||||||
}, [schemasIDs]);
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (needPreload) {
|
||||||
|
setNeedPreload(false);
|
||||||
|
cache.preload(schemasIDs);
|
||||||
|
}
|
||||||
|
}, [schemasIDs, needPreload, cache]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cache.loading || schemas.length !== schemasIDs.length) {
|
if (cache.loading || schemas.length !== schemasIDs.length) {
|
||||||
|
@ -99,20 +117,18 @@ function DlgEditOperation({ hideWindow, oss, target, onSubmit }: DlgEditOperatio
|
||||||
}
|
}
|
||||||
setSubstitutions(prev =>
|
setSubstitutions(prev =>
|
||||||
prev.filter(sub => {
|
prev.filter(sub => {
|
||||||
const original = cache.getSchemaByCst(sub.original);
|
const original = getSchemaByCst(sub.original);
|
||||||
if (!original || !schemasIDs.includes(original.id)) {
|
if (!original || !schemasIDs.includes(original.id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const substitution = cache.getSchemaByCst(sub.substitution);
|
const substitution = getSchemaByCst(sub.substitution);
|
||||||
if (!substitution || !schemasIDs.includes(substitution.id)) {
|
if (!substitution || !schemasIDs.includes(substitution.id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// eslint-disable-next-line react-compiler/react-compiler
|
}, [schemasIDs, schemas, cache.loading, getSchemaByCst]);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [schemasIDs, schemas, cache.loading]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cache.loading || schemas.length !== schemasIDs.length) {
|
if (cache.loading || schemas.length !== schemasIDs.length) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { getRSFormDetails } from '@/backend/rsforms';
|
import { getRSFormDetails } from '@/backend/rsforms';
|
||||||
import { type ErrorData } from '@/components/info/InfoError';
|
import { type ErrorData } from '@/components/info/InfoError';
|
||||||
import { LibraryItemID } from '@/models/library';
|
import { LibraryItemID } from '@/models/library';
|
||||||
import { ConstituentaID, IRSForm, IRSFormData } from '@/models/rsform';
|
import { IRSForm, IRSFormData } from '@/models/rsform';
|
||||||
import { RSFormLoader } from '@/models/RSFormLoader';
|
import { RSFormLoader } from '@/models/RSFormLoader';
|
||||||
|
|
||||||
function useRSFormCache() {
|
function useRSFormCache() {
|
||||||
|
@ -20,34 +20,6 @@ function useRSFormCache() {
|
||||||
setCache(prev => [...prev, schema]);
|
setCache(prev => [...prev, schema]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const getSchema = useCallback((id: LibraryItemID) => cache.find(item => item.id === id), [cache]);
|
|
||||||
|
|
||||||
const getSchemaByCst = useCallback(
|
|
||||||
(id: ConstituentaID) => {
|
|
||||||
for (const schema of cache) {
|
|
||||||
const cst = schema.items.find(cst => cst.id === id);
|
|
||||||
if (cst) {
|
|
||||||
return schema;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
[cache]
|
|
||||||
);
|
|
||||||
|
|
||||||
const getConstituenta = useCallback(
|
|
||||||
(id: ConstituentaID) => {
|
|
||||||
for (const schema of cache) {
|
|
||||||
const cst = schema.items.find(cst => cst.id === id);
|
|
||||||
if (cst) {
|
|
||||||
return cst;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
},
|
|
||||||
[cache]
|
|
||||||
);
|
|
||||||
|
|
||||||
const preload = useCallback(
|
const preload = useCallback(
|
||||||
(target: LibraryItemID[]) => setPending(prev => [...prev, ...target.filter(id => !prev.includes(id))]),
|
(target: LibraryItemID[]) => setPending(prev => [...prev, ...target.filter(id => !prev.includes(id))]),
|
||||||
[]
|
[]
|
||||||
|
@ -76,11 +48,9 @@ function useRSFormCache() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
// eslint-disable-next-line react-compiler/react-compiler
|
}, [pending, cache, processing]);
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [pending]);
|
|
||||||
|
|
||||||
return { preload, getSchema, getConstituenta, getSchemaByCst, loading, error, setError };
|
return { preload, data: cache, loading, error, setError };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useRSFormCache;
|
export default useRSFormCache;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user