2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import { motion } from 'framer-motion';
|
|
|
|
import { useMemo, useState } from 'react';
|
|
|
|
|
2024-06-09 15:18:52 +03:00
|
|
|
import { useAccessMode } from '@/context/AccessModeContext';
|
2024-06-26 19:47:05 +03:00
|
|
|
import { useConceptOptions } from '@/context/ConceptOptionsContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
import useWindowSize from '@/hooks/useWindowSize';
|
|
|
|
import { ConstituentaID, IConstituenta, IRSForm } from '@/models/rsform';
|
2024-06-09 15:18:52 +03:00
|
|
|
import { UserLevel } from '@/models/user';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { animateSideView } from '@/styling/animations';
|
|
|
|
|
|
|
|
import ConstituentsSearch from './ConstituentsSearch';
|
2024-06-26 19:47:05 +03:00
|
|
|
import TableSideConstituents from './TableSideConstituents';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
// Window width cutoff for expression show
|
|
|
|
const COLUMN_EXPRESSION_HIDE_THRESHOLD = 1500;
|
|
|
|
|
|
|
|
// Window width cutoff for dense search bar
|
|
|
|
const COLUMN_DENSE_SEARCH_THRESHOLD = 1100;
|
|
|
|
|
|
|
|
interface ViewConstituentsProps {
|
|
|
|
expression: string;
|
|
|
|
isBottom?: boolean;
|
|
|
|
activeCst?: IConstituenta;
|
|
|
|
schema?: IRSForm;
|
|
|
|
onOpenEdit: (cstID: ConstituentaID) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ViewConstituents({ expression, schema, activeCst, isBottom, onOpenEdit }: ViewConstituentsProps) {
|
|
|
|
const { calculateHeight } = useConceptOptions();
|
|
|
|
const windowSize = useWindowSize();
|
2024-06-09 15:18:52 +03:00
|
|
|
const { accessLevel } = useAccessMode();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
const [filteredData, setFilteredData] = useState<IConstituenta[]>(schema?.items ?? []);
|
|
|
|
|
|
|
|
const table = useMemo(
|
|
|
|
() => (
|
2024-06-26 19:47:05 +03:00
|
|
|
<TableSideConstituents
|
2024-06-09 15:18:52 +03:00
|
|
|
maxHeight={
|
|
|
|
isBottom
|
|
|
|
? calculateHeight(accessLevel !== UserLevel.READER ? '42rem' : '35rem', '10rem')
|
|
|
|
: calculateHeight('8.2rem')
|
|
|
|
}
|
2024-06-07 20:17:03 +03:00
|
|
|
items={filteredData}
|
|
|
|
activeCst={activeCst}
|
|
|
|
onOpenEdit={onOpenEdit}
|
2024-08-24 08:24:05 +03:00
|
|
|
autoScroll={!isBottom}
|
2024-06-07 20:17:03 +03:00
|
|
|
denseThreshold={COLUMN_EXPRESSION_HIDE_THRESHOLD}
|
|
|
|
/>
|
|
|
|
),
|
2024-06-09 15:18:52 +03:00
|
|
|
[isBottom, filteredData, activeCst, onOpenEdit, calculateHeight, accessLevel]
|
2024-06-07 20:17:03 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<motion.div
|
|
|
|
className={clsx(
|
2024-08-24 08:24:05 +03:00
|
|
|
'border', // prettier: split-lines
|
2024-06-07 20:17:03 +03:00
|
|
|
{
|
2024-08-24 08:24:05 +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-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
)}
|
|
|
|
initial={{ ...animateSideView.initial }}
|
|
|
|
animate={{ ...animateSideView.animate }}
|
|
|
|
exit={{ ...animateSideView.exit }}
|
|
|
|
>
|
|
|
|
<ConstituentsSearch
|
|
|
|
dense={windowSize.width && windowSize.width < COLUMN_DENSE_SEARCH_THRESHOLD ? true : undefined}
|
|
|
|
schema={schema}
|
|
|
|
activeID={activeCst?.id}
|
|
|
|
activeExpression={expression}
|
|
|
|
setFiltered={setFilteredData}
|
|
|
|
/>
|
|
|
|
{table}
|
|
|
|
</motion.div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ViewConstituents;
|