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

81 lines
2.5 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { motion } from 'framer-motion';
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 { animateSideView } from '@/styling/animations';
2023-12-08 19:24:08 +03:00
import ConstituentsSearch from './ConstituentsSearch';
import TableSideConstituents from './TableSideConstituents';
2023-12-08 19:24:08 +03:00
// Window width cutoff for expression show
const COLUMN_EXPRESSION_HIDE_THRESHOLD = 1500;
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;
2023-12-08 19:24:08 +03:00
}
2024-05-23 13:36:16 +03:00
function ViewConstituents({ expression, schema, activeCst, isBottom, onOpenEdit }: 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}
denseThreshold={COLUMN_EXPRESSION_HIDE_THRESHOLD}
/>
),
2024-06-09 15:19:39 +03:00
[isBottom, filteredData, activeCst, onOpenEdit, calculateHeight, accessLevel]
);
2023-12-17 20:19:28 +03:00
return (
2023-12-28 14:04:44 +03:00
<motion.div
className={clsx(
2024-06-05 14:43:52 +03:00
'border overflow-visible', // prettier: split-lines
{
2024-08-10 11:41:52 +03:00
'mt-[2.2rem] rounded-l-md rounded-r-none h-fit': !isBottom,
'mt-3 mx-6 rounded-md md:w-[45.8rem]': isBottom
}
)}
2023-12-28 14:04:44 +03:00
initial={{ ...animateSideView.initial }}
animate={{ ...animateSideView.animate }}
exit={{ ...animateSideView.exit }}
>
<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}
2023-12-28 14:04:44 +03:00
</motion.div>
);
2023-12-08 19:24:08 +03:00
}
2023-12-28 14:04:44 +03:00
export default ViewConstituents;