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

fix(j-s): Memoize CaseFiles so they don't move when renaming #16149

Merged
merged 2 commits into from
Sep 25, 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,4 +1,4 @@
import { useCallback, useContext, useState } from 'react'
import { useCallback, useContext, useMemo, useState } from 'react'
import { useIntl } from 'react-intl'
import { LayoutGroup } from 'framer-motion'
import router from 'next/router'
Expand All @@ -23,6 +23,15 @@ import { caseFile as m } from './CaseFile.strings'
const CaseFile = () => {
const { workingCase, isLoadingWorkingCase, caseNotFound } =
useContext(FormContext)

const caseFiles = useMemo(() => {
return (
workingCase.caseFiles?.filter(
(caseFile) => caseFile.category === CaseFileCategory.CASE_FILE_RECORD,
) ?? []
)
}, [workingCase.caseFiles])

const { formatMessage } = useIntl()
const [editCount, setEditCount] = useState<number>(0)

Expand Down Expand Up @@ -57,24 +66,20 @@ const CaseFile = () => {
<Box marginBottom={5}>
<LayoutGroup>
<Accordion singleExpand>
{workingCase.policeCaseNumbers?.map((policeCaseNumber, index) => (
<IndictmentsCaseFilesAccordionItem
key={index}
caseId={workingCase.id}
policeCaseNumber={policeCaseNumber}
shouldStartExpanded={index === 0}
caseFiles={
workingCase.caseFiles?.filter(
(caseFile) =>
caseFile.policeCaseNumber === policeCaseNumber &&
caseFile.category === CaseFileCategory.CASE_FILE_RECORD,
) ?? []
}
subtypes={workingCase.indictmentSubtypes}
crimeScenes={workingCase.crimeScenes}
setEditCount={setEditCount}
/>
))}
{workingCase.policeCaseNumbers?.map((policeCaseNumber, index) => {
return (
<IndictmentsCaseFilesAccordionItem
key={index}
caseId={workingCase.id}
policeCaseNumber={policeCaseNumber}
shouldStartExpanded={index === 0}
caseFiles={caseFiles}
subtypes={workingCase.indictmentSubtypes}
crimeScenes={workingCase.crimeScenes}
setEditCount={setEditCount}
/>
)
})}
</Accordion>
</LayoutGroup>
</Box>
Expand Down