ConceptPortal-public/rsconcept/frontend/src/pages/RSFormPage/ViewConstituents/ViewConstituents.tsx

82 lines
2.5 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
2024-12-12 17:16:36 +03:00
import { useMemo, useState } from 'react';
2023-12-08 19:24:08 +03:00
2024-06-09 15:19:39 +03:00
import { useAccessMode } from '@/context/AccessModeContext';
import { useConceptOptions } from '@/context/ConceptOptionsContext';
2024-06-04 14:20:43 +03:00
import useWindowSize from '@/hooks/useWindowSize';
2024-03-17 19:24:12 +03:00
import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform';
2024-06-09 15:19:39 +03:00
import { UserLevel } from '@/models/user';
import { PARAMETER } from '@/utils/constants';
2023-12-08 19:24:08 +03:00
import ConstituentsSearch from './ConstituentsSearch';
import TableSideConstituents from './TableSideConstituents';
2023-12-08 19:24:08 +03:00
2024-06-04 14:20:43 +03:00
// Window width cutoff for dense search bar
const COLUMN_DENSE_SEARCH_THRESHOLD = 1100;
2023-12-08 19:24:08 +03:00
interface ViewConstituentsProps {
2023-12-28 14:04:44 +03:00
expression: string;
isBottom?: boolean;
2024-05-23 13:36:16 +03:00
activeCst?: IConstituenta;
2023-12-28 14:04:44 +03:00
schema?: IRSForm;
2024-03-17 19:24:12 +03:00
onOpenEdit: (cstID: ConstituentaID) => void;
isMounted: boolean;
2023-12-08 19:24:08 +03:00
}
function ViewConstituents({ expression, schema, activeCst, isBottom, onOpenEdit, isMounted }: ViewConstituentsProps) {
2024-04-06 14:39:49 +03:00
const { calculateHeight } = useConceptOptions();
2024-06-04 14:20:43 +03:00
const windowSize = useWindowSize();
2024-06-09 15:19:39 +03:00
const { accessLevel } = useAccessMode();
2023-12-28 14:04:44 +03:00
2023-12-08 19:24:08 +03:00
const [filteredData, setFilteredData] = useState<IConstituenta[]>(schema?.items ?? []);
const table = useMemo(
() => (
<TableSideConstituents
2024-06-09 15:19:39 +03:00
maxHeight={
isBottom
? calculateHeight(accessLevel !== UserLevel.READER ? '42rem' : '35rem', '10rem')
: calculateHeight('8.2rem')
}
items={filteredData}
2024-05-23 13:36:16 +03:00
activeCst={activeCst}
onOpenEdit={onOpenEdit}
2024-08-24 08:24:32 +03:00
autoScroll={!isBottom}
/>
),
2024-06-09 15:19:39 +03:00
[isBottom, filteredData, activeCst, onOpenEdit, calculateHeight, accessLevel]
);
2023-12-17 20:19:28 +03:00
return (
2024-12-12 17:16:36 +03:00
<div
className={clsx(
2024-08-24 08:24:32 +03:00
'border', // prettier: split-lines
{
2024-08-24 08:24:32 +03:00
'mt-[2.2rem] rounded-l-md rounded-r-none h-fit overflow-visible': !isBottom,
'mt-3 mx-6 rounded-md md:max-w-[45.8rem] overflow-hidden': isBottom
}
)}
2024-12-12 17:16:36 +03:00
style={{
transitionProperty: 'opacity, width',
transitionDuration: `${2 * PARAMETER.moveDuration}ms`,
transitionTimingFunction: 'ease-in-out',
opacity: isMounted ? 1 : 0,
width: isMounted ? '100%' : '0'
}}
2023-12-28 14:04:44 +03:00
>
<ConstituentsSearch
2024-06-04 14:20:43 +03:00
dense={windowSize.width && windowSize.width < COLUMN_DENSE_SEARCH_THRESHOLD ? true : undefined}
2023-12-28 14:04:44 +03:00
schema={schema}
2024-05-23 13:36:16 +03:00
activeID={activeCst?.id}
2023-12-28 14:04:44 +03:00
activeExpression={expression}
setFiltered={setFilteredData}
/>
{table}
2024-12-12 17:16:36 +03:00
</div>
2023-12-28 14:04:44 +03:00
);
2023-12-08 19:24:08 +03:00
}
2023-12-28 14:04:44 +03:00
export default ViewConstituents;