|
| 1 | +import { Dispatch } from "react" |
| 2 | + |
| 3 | +import { Trash } from "lucide-react" |
| 4 | + |
| 5 | +import { Checkbox } from "components/ui/checkbox" |
| 6 | +import { showDialog } from "components/ui/dialog" |
| 7 | +import { IconButton } from "components/ui/icon-button" |
| 8 | +import { createColumnHelper, Table } from "components/ui/table" |
| 9 | +import { getDateAtom, useDateEntries, type TimeEntry } from "data/time-entries" |
| 10 | + |
| 11 | +import { Duration } from "./duration" |
| 12 | +import { inputs } from "./inputs" |
| 13 | + |
| 14 | +export interface CheckedProps { |
| 15 | + checked: Record<string, boolean> |
| 16 | + onCheckedChange: Dispatch<TimeEntry> |
| 17 | +} |
| 18 | + |
| 19 | +interface TimeTableRowsProps |
| 20 | + extends CheckedProps, |
| 21 | + ReturnType<typeof useDateEntries> { |
| 22 | + date: string |
| 23 | +} |
| 24 | + |
| 25 | +interface TableConfig { |
| 26 | + rowData: TimeEntry |
| 27 | + rowMeta: CheckedProps & { |
| 28 | + onChange: Dispatch<TimeEntry> |
| 29 | + onRemove: Dispatch<number> |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const helper = createColumnHelper<TableConfig>() |
| 34 | +const checkedColumn = helper.column({ |
| 35 | + name: "Selected", |
| 36 | + render: ({ rowData, checked, onCheckedChange }) => ( |
| 37 | + <Checkbox |
| 38 | + checked={checked[rowData.id] ?? false} |
| 39 | + onCheckedChange={() => onCheckedChange(rowData)} |
| 40 | + /> |
| 41 | + ), |
| 42 | +}) |
| 43 | +const descriptionColumn = helper.column({ |
| 44 | + name: "Description", |
| 45 | + colSize: "col-[2_/_-1] @xl:col-[2_/_-1] @4xl:col-[span_1]", |
| 46 | + className: "flex", |
| 47 | + render: ({ rowData, onChange }) => ( |
| 48 | + <inputs.Description |
| 49 | + entry={rowData} |
| 50 | + onChange={data => onChange({ ...rowData, ...data })} |
| 51 | + /> |
| 52 | + ), |
| 53 | +}) |
| 54 | +const projectColumn = helper.column({ |
| 55 | + name: "Project", |
| 56 | + colSize: "col-[2_/_6] @xl:col-[2] @4xl:col-[span_1]", |
| 57 | + className: "@4xl:*:w-full", |
| 58 | + render: ({ rowData, onChange }) => ( |
| 59 | + <inputs.Project |
| 60 | + entry={rowData} |
| 61 | + onChange={data => onChange({ ...rowData, ...data })} |
| 62 | + /> |
| 63 | + ), |
| 64 | +}) |
| 65 | +const dateColumn = helper.column({ |
| 66 | + name: "Date", |
| 67 | + colSize: |
| 68 | + "col-[6_/_-1] justify-self-end @xl:justify-self-start @xl:col-[auto]", |
| 69 | + render: ({ rowData, onChange }) => ( |
| 70 | + <inputs.Date |
| 71 | + entry={rowData} |
| 72 | + onChange={data => onChange({ ...rowData, ...data })} |
| 73 | + /> |
| 74 | + ), |
| 75 | +}) |
| 76 | +const timeStartColumn = helper.column({ |
| 77 | + name: "Time Start", |
| 78 | + colSize: "col-[2] @xl:col-[auto]", |
| 79 | + render: ({ rowData, onChange }) => ( |
| 80 | + <inputs.TimeStart |
| 81 | + entry={rowData} |
| 82 | + onChange={data => onChange({ ...rowData, ...data })} |
| 83 | + /> |
| 84 | + ), |
| 85 | +}) |
| 86 | +const timeSeparatorColumn = helper.decorator({ |
| 87 | + name: "", |
| 88 | + render: inputs.TimeSeparator, |
| 89 | +}) |
| 90 | +const timeEndColumn = helper.column({ |
| 91 | + name: "Time End", |
| 92 | + render: ({ rowData, onChange }) => ( |
| 93 | + <inputs.TimeEnd |
| 94 | + entry={rowData} |
| 95 | + onChange={data => onChange({ ...rowData, ...data })} |
| 96 | + /> |
| 97 | + ), |
| 98 | +}) |
| 99 | +const durationColumn = helper.column({ |
| 100 | + name: "Duration", |
| 101 | + render: ({ rowData }) => ( |
| 102 | + <Duration entries={[rowData]} className="inline-block w-15 text-center" /> |
| 103 | + ), |
| 104 | +}) |
| 105 | +const actionColumn = helper.column({ |
| 106 | + name: "Actions", |
| 107 | + colSize: "col-[7] @xl:col-[auto]", |
| 108 | + render: ({ rowData, onRemove }) => ( |
| 109 | + <IconButton |
| 110 | + title="Delete" |
| 111 | + hideTitle |
| 112 | + icon={Trash} |
| 113 | + onClick={() => onRemove(rowData.id)} |
| 114 | + className="@4xl:[[role='row']:not(:hover,:focus-within)_&]:opacity-0" |
| 115 | + /> |
| 116 | + ), |
| 117 | +}) |
| 118 | + |
| 119 | +export const TimeTableEditable = ({ |
| 120 | + date, |
| 121 | + atom, |
| 122 | + entries, |
| 123 | + checked, |
| 124 | + onCheckedChange, |
| 125 | +}: TimeTableRowsProps) => { |
| 126 | + const handleChange = (data: TimeEntry) => { |
| 127 | + if (data.date === date) { |
| 128 | + atom.actions.edit(data.id, data) |
| 129 | + return |
| 130 | + } |
| 131 | + // If date changed, move entry to new table |
| 132 | + atom.actions.delete(data.id) |
| 133 | + const newAtom = getDateAtom(data.date) |
| 134 | + const add = () => newAtom.actions.add(data) |
| 135 | + if (newAtom.didInit instanceof Promise) { |
| 136 | + void newAtom.didInit.then(add) |
| 137 | + } else { |
| 138 | + add() |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const handleRemove = (id: number) => |
| 143 | + showDialog({ |
| 144 | + title: "Delete time entry?", |
| 145 | + description: |
| 146 | + "Do you really want to delete this time entry? This action cannot be reverted.", |
| 147 | + confirm: { |
| 148 | + caption: "Delete", |
| 149 | + look: "destructive", |
| 150 | + onClick: () => atom.actions.delete(id), |
| 151 | + }, |
| 152 | + }) |
| 153 | + |
| 154 | + return ( |
| 155 | + <Table<TableConfig> |
| 156 | + hideHeaders |
| 157 | + name="time-table" |
| 158 | + gridCols="grid-cols-[2.5rem_auto_auto_auto_auto_1fr_2.5rem] @xl:grid-cols-[2.5rem_1fr_auto_auto_auto_auto_auto_2.5rem] @4xl:grid-cols-[2.5rem_1fr_auto_auto_auto_auto_auto_auto_2.5rem]" |
| 159 | + rowData={entries} |
| 160 | + columns={[ |
| 161 | + checkedColumn, |
| 162 | + descriptionColumn, |
| 163 | + projectColumn, |
| 164 | + dateColumn, |
| 165 | + timeStartColumn, |
| 166 | + timeSeparatorColumn, |
| 167 | + timeEndColumn, |
| 168 | + durationColumn, |
| 169 | + actionColumn, |
| 170 | + ]} |
| 171 | + rowMeta={{ |
| 172 | + checked, |
| 173 | + onCheckedChange, |
| 174 | + onChange: handleChange, |
| 175 | + onRemove: handleRemove, |
| 176 | + }} |
| 177 | + /> |
| 178 | + ) |
| 179 | +} |
0 commit comments