diff --git a/web/src/components/DataGrid/DataGrid.tsx b/web/src/components/DataGrid/DataGrid.tsx index 086f34f..a848c36 100644 --- a/web/src/components/DataGrid/DataGrid.tsx +++ b/web/src/components/DataGrid/DataGrid.tsx @@ -1,10 +1,12 @@ -import { useEffect, useMemo, useState, type ReactNode } from 'react'; +import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { keepPreviousData, useQuery } from '@tanstack/react-query'; import { useLocation, useSearchParams } from 'react-router-dom'; import { alpha, Alert, Badge, Box, Button, Chip, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, Paper, Stack, Table, TableBody, TableCell, TableContainer, TableHead, TablePagination, TableRow, Tooltip, Typography, useTheme, } from '@mui/material'; +import CheckIcon from '@mui/icons-material/Check'; +import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import FilterListIcon from '@mui/icons-material/FilterList'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import EditOutlined from '@mui/icons-material/EditOutlined'; @@ -255,7 +257,7 @@ export function DataGrid({ meta }: DataGridProps) { {hasActions && actionsBefore && actionsCell(row)} {columns.map((c) => ( - + {c.format === 'status' ? : } ))} {hasActions && !actionsBefore && actionsCell(row)} @@ -326,6 +328,70 @@ function cellSx(c: ColumnMeta) { ...(c.hideBelow ? { display: { xs: 'none', [BP[c.hideBelow]]: 'table-cell' } } : {}), }; } +/** Writes text to the clipboard (async API, with the old execCommand path for contexts where it is unavailable). */ +async function copyText(text: string) { + try { + await navigator.clipboard.writeText(text); + } catch { + const ta = document.createElement('textarea'); + ta.value = text; + ta.style.position = 'fixed'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.select(); + try { document.execCommand('copy'); } finally { document.body.removeChild(ta); } + } +} + +/** + * A grid cell value with a small copy icon (shown on hover / keyboard focus, always on touch screens). It copies the text the cell + * shows; cells without text (icon-only columns) get no icon. No toast: the icon turns into a check mark for a moment. + */ +export function CopyCell({ children }: { children: ReactNode }) { + const { t } = useUi(); + const ref = useRef(null); + const [hasText, setHasText] = useState(false); + const [done, setDone] = useState(false); + useLayoutEffect(() => { + const has = (ref.current?.textContent ?? '').trim().length > 0; + setHasText((cur) => (cur === has ? cur : has)); + }); + useEffect(() => { + if (!done) return undefined; + const id = window.setTimeout(() => setDone(false), 1200); + return () => window.clearTimeout(id); + }, [done]); + return ( + + {children} + {hasText && ( + { + e.stopPropagation(); // the row click (open) must not fire + const text = (ref.current?.textContent ?? '').trim(); + void copyText(text).then(() => setDone(true)); + }} + sx={{ p: 0.25 }} + > + {done ? : } + + )} + + ); +} + function Cell({ col, row }: { col: ColumnMeta; row: T }): ReactNode { const { lang } = useUi(); if (col.render) return <>{col.render(row, { lang })}; diff --git a/web/src/components/DataGrid/index.ts b/web/src/components/DataGrid/index.ts index 545b40c..97d0d1c 100644 --- a/web/src/components/DataGrid/index.ts +++ b/web/src/components/DataGrid/index.ts @@ -1,3 +1,3 @@ -export { DataGrid, gridKey } from './DataGrid'; +export { CopyCell, DataGrid, gridKey } from './DataGrid'; export type { DataGridProps } from './DataGrid'; export { gridReturnUrl, rememberGridUrl } from './gridReturn'; diff --git a/web/src/features/refinery/commission-claims/pages.tsx b/web/src/features/refinery/commission-claims/pages.tsx index 8d76f00..db4428b 100644 --- a/web/src/features/refinery/commission-claims/pages.tsx +++ b/web/src/features/refinery/commission-claims/pages.tsx @@ -9,7 +9,7 @@ import PaymentsIcon from '@mui/icons-material/Payments'; import SearchIcon from '@mui/icons-material/Search'; import AssignmentIcon from '@mui/icons-material/Assignment'; import ReceiptLongIcon from '@mui/icons-material/ReceiptLong'; -import { DataGrid } from '@/components/DataGrid'; +import { CopyCell, DataGrid } from '@/components/DataGrid'; import { LoadingBanner } from '@/components/LoadingBanner'; import { PageHeader } from '@/components/PageHeader'; import { StatusChip } from '@/components/StatusChip'; @@ -63,9 +63,9 @@ function WageClaimsTable(p: { {selectable && ( p.onToggle?.(r.id)} /> )} - {r.wageClaimNo} - {formatDate(r.wageClaimDate)} - {r.tripCnt} + {r.wageClaimNo} + {formatDate(r.wageClaimDate)} + {r.tripCnt} ))} diff --git a/web/src/features/refinery/wage-claims/components.tsx b/web/src/features/refinery/wage-claims/components.tsx index 50bce2b..ca6ecb5 100644 --- a/web/src/features/refinery/wage-claims/components.tsx +++ b/web/src/features/refinery/wage-claims/components.tsx @@ -6,6 +6,7 @@ import { import HistoryIcon from '@mui/icons-material/History'; import VisibilityOutlined from '@mui/icons-material/VisibilityOutlined'; import type { PagedResult } from '@/api/client'; +import { CopyCell } from '@/components/DataGrid'; import { LoadingBanner } from '@/components/LoadingBanner'; import { SectionPaper } from '@/components/SectionPaper'; import { SectionTitle } from '@/components/SectionTitle'; @@ -60,14 +61,14 @@ export function TripsTable(p: { )} {rows.map((r) => ( p.onOpen!(r) : undefined} sx={{ cursor: p.onOpen ? 'pointer' : undefined }}> - {r.pn} - {formatDate(r.paymentedDate)} - {r.loadingNo} - {r.dischargeAuthoriztionNo} - {formatDate(r.dischargeEntryDate)} - {r.tn} - {r.trn} - {r.nnpn} + {r.pn} + {formatDate(r.paymentedDate)} + {r.loadingNo} + {r.dischargeAuthoriztionNo} + {formatDate(r.dischargeEntryDate)} + {r.tn} + {r.trn} + {r.nnpn} ))} diff --git a/web/src/i18n/ui.ts b/web/src/i18n/ui.ts index 07ca01e..d13c0e4 100644 --- a/web/src/i18n/ui.ts +++ b/web/src/i18n/ui.ts @@ -70,6 +70,7 @@ export const ui = { 'common.save': { en: 'Save', ar: 'حفظ' }, 'common.cancel': { en: 'Cancel', ar: 'إلغاء' }, 'common.close': { en: 'Close', ar: 'إغلاق' }, + 'common.copy': { en: 'Copy', ar: 'نسخ' }, 'common.back': { en: 'Back', ar: 'رجوع' }, 'common.next': { en: 'Next', ar: 'التالي' }, 'common.edit': { en: 'Edit', ar: 'تعديل' },