Files
hats-app/app/admin/report/history/component/index.tsx
T
Ichsan Kurniawan 279b642a7a Apply RBAC permissions across admin and tasks
Enforce page access and action-level permissions across admin and task views, including exports and record actions. Move user creation to a client component behind server-side permission checks, standardize data fetching, and remove the deprecated user-role route.
2026-11-09 17:32:31 +07:00

109 lines
4.4 KiB
TypeScript

'use client'
import { ChangeEvent, FC, Suspense, useCallback, useEffect, useMemo, useState } from 'react';
import { Table } from 'antd';
import { TableProps } from 'antd/es/table';
import { Input } from '@/components/ui/input';
import { IHistory } from '@/app/(dashboard)/history/components/type';
import ColumnsHistory from './Columns';
import { CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import LazyLoading from '@/components/loader/LazyLoading';
import ExportComponent from './Export';
import useDebounce from '@/hooks/useDebounce';
type Props = {
data: IHistory[]
actions?: string[]
};
const ComponentPage: FC<Props> = ({ data, actions }) => {
const [currentDataTable, setCurrentDataTable] = useState<IHistory[]>([])
const [searchText, setSearchText] = useState<string>('')
const debouncedSearchText = useDebounce(searchText, 300)
const searchFilterLogic = useCallback((item: IHistory, text: string) => {
const lowerText = text.toLowerCase();
// Use optional chaining carefully, and provide fallback empty strings to avoid crashes
return (
(item.user?.fullname || '').toLowerCase().includes(lowerText) ||
(item.working_status || '').toLowerCase().includes(lowerText) ||
(item.device?.name || '').toLowerCase().includes(lowerText) ||
(item.location_name || '').toLowerCase().includes(lowerText) ||
(item.note || '').toLowerCase().includes(lowerText) ||
(item.presence_checkout?.location_name || '').toLowerCase().includes(lowerText) ||
(item.presence_checkout?.note || '').toLowerCase().includes(lowerText)
);
}, []);
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
setSearchText(e.target.value);
}
const filterData = useMemo(() => {
if (!data) return [];
if (!debouncedSearchText) return data;
return data.filter(item => searchFilterLogic(item, debouncedSearchText));
}, [data, debouncedSearchText, searchFilterLogic]);
const exportData = useMemo(() => {
const baseExportData = currentDataTable.length > 0 ? currentDataTable : (data || []);
// We use the raw searchText here or debounced? Usually debounced is better so they don't export mid-type
if (debouncedSearchText) {
return baseExportData.filter(item => searchFilterLogic(item, debouncedSearchText));
}
return baseExportData;
}, [currentDataTable, data, debouncedSearchText, searchFilterLogic]);
const tableProps: TableProps<IHistory> = {
size: 'small',
bordered: false,
showHeader: true,
pagination: {
pageSizeOptions: [5, 10, 20, 50],
defaultPageSize: 15,
showSizeChanger: true,
showQuickJumper: true,
size: 'middle' as any
},
scroll: { x: 'max-content' },
tableLayout: 'auto',
};
return (
<>
<CardHeader className='flex flex-col md:flex-row items-center justify-between space-y-5 sm:space-y-0'>
<div>
<CardTitle>Attendance History</CardTitle>
<CardDescription className='mt-1 sm:block hidden'>Here&apos;s a list of all employee attendance history!</CardDescription>
</div>
{actions?.includes('EXPORT') &&
<Suspense fallback={<LazyLoading />}>
<ExportComponent data={exportData} />
</Suspense>
}
</CardHeader>
<CardContent>
<div className='flex flex-col space-y-4'>
<div className="flex items-center justify-between">
<div className="flex flex-1 items-center space-x-2">
<Input placeholder="Search report..." className="h-8 w-full sm:w-62.5 lg:w-72" onChange={handleSearch} />
</div>
</div>
<Table
{...tableProps}
rowKey='presence_checkin_code'
columns={ColumnsHistory}
onChange={(pagination, filters, sorter, extra) => setCurrentDataTable(extra.currentDataSource as IHistory[])}
dataSource={filterData || []}
/>
</div>
</CardContent>
</>
);
}
export default ComponentPage;