| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { Play, Plus |
| 5 | import { useEffect, useState |
| 6 | import { toast } |
| 7 | import SendToClientDialog from '@/components/send-to-client-dialog |
| 8 | import { Button } |
| 9 | import { Input } |
| 10 | import { Label } |
| 11 | import { Tabs, TabsContent |
| 12 | import { useTimerStore } |
| 13 | import { BiweeklyTimesheet } |
| 14 | import { BudgetIndicator } |
| 15 | import { ClientReportsView } |
| 16 | import { MemberRatesDialog } |
| 17 | import { MonthlyTimesheet } |
| 18 | import { SentReportsList } |
| 19 | import { TeamEntriesTable } |
| 20 | import { TimeEntryForm } |
| 21 | import { TimesheetApproval } |
| 22 | import { WeeklyTimesheet } |
| 23 | import { sendTimesheetToClientAction } |
| 24 | import { formatMinutes } |
| 25 | import type { TimeTrackingPageProps |
| 26 | |
| 27 | export function TimeTrackingClient(props |
| 28 | const { |
| 29 | entries, |
| 30 | projectId, |
| 31 | projectName, |
| 32 | orgSlug, |
| 33 | projectSlug, |
| 34 | isAdmin, |
| 35 | isClient, |
| 36 | currentMemberId, |
| 37 | requirements, |
| 38 | projectMembers, |
| 39 | clients, |
| 40 | budgetStatus, |
| 41 | memberRates, |
| 42 | timesheetDuration, |
| 43 | timesheetReports, |
| 44 | reportEntriesMap, |
| 45 | reportRecipientsMap, |
| 46 | clientReports, |
| 47 | defaultCurrency, |
| 48 | isClientInvolved, |
| 49 | initialLogMinutes, |
| 50 | customFields, |
| 51 | } = props |
| 52 | const hasCustomFields = |
| 53 | const timesheetsBase = |
| 54 | const router = |
| 55 | const [formOpen, |
| 56 | !hasCustomFields && !!initialLogMinutes |
| 57 | ) |
| 58 | const [formDefaultDate, |
| 59 | const [formDefaultDuration, |
| 60 | number | undefined |
| 61 | >(hasCustomFields ? undefined |
| 62 | const timerStartedAt = |
| 63 | const timerAccumulatedMs = |
| 64 | const startTimer = |
| 65 | const timerActive = |
| 66 | |
| 67 | useEffect(() => |
| 68 | if (!initialLogMinutes) |
| 69 | return |
| 70 | } |
| 71 | if (hasCustomFields) { |
| 72 | const qs = |
| 73 | router.replace(`${ |
| 74 | return |
| 75 | } |
| 76 | const url = |
| 77 | url.searchParams.delete |
| 78 | window.history.replaceState |
| 79 | }, [hasCustomFields, |
| 80 | |
| 81 | useEffect(() => |
| 82 | const handler = |
| 83 | const detail = |
| 84 | if (hasCustomFields) { |
| 85 | const qs = |
| 86 | router.push(`${ |
| 87 | return |
| 88 | } |
| 89 | setFormDefaultDuration(detail.minutes |
| 90 | setFormOpen(true) |
| 91 | } |
| 92 | window.addEventListener('timer:log |
| 93 | return () => |
| 94 | }, [hasCustomFields, |
| 95 | const [ratesOpen, |
| 96 | const [sendOpen, |
| 97 | const [selectedEntryIds, |
| 98 | new Set() |
| 99 | ) |
| 100 | const [reportTitle, |
| 101 | |
| 102 | const goToNew = |
| 103 | const params = |
| 104 | if (opts?.date |
| 105 | const y = |
| 106 | const m = |
| 107 | const d = |
| 108 | params.set('date |
| 109 | } |
| 110 | if (opts?.durationMinutes |
| 111 | params.set('duration |
| 112 | } |
| 113 | const qs = |
| 114 | router.push(`${ |
| 115 | } |
| 116 | const openCreate = |
| 117 | if (hasCustomFields) { |
| 118 | goToNew(opts) |
| 119 | return |
| 120 | } |
| 121 | if (opts?.date |
| 122 | setFormDefaultDate(opts.date |
| 123 | } |
| 124 | if (opts?.durationMinutes |
| 125 | setFormDefaultDuration(opts.durationMinutes |
| 126 | } |
| 127 | setFormOpen(true) |
| 128 | } |
| 129 | |
| 130 | const selectedMinutes = |
| 131 | .filter((e) |
| 132 | .reduce((sum, |
| 133 | |
| 134 | const myEntries = |
| 135 | const submittedEntries = |
| 136 | (e) => |
| 137 | ) |
| 138 | const disputedReports = |
| 139 | (r) => |
| 140 | ) |
| 141 | |
| 142 | if (isClient) { |
| 143 | return ( |
| 144 | <div className='w-full |
| 145 | <div className='mb-6 flex items-center justify-between |
| 146 | <h1 className='font-semibold text-2xl |
| 147 | </div> |
| 148 | <ClientReportsView |
| 149 | customFields={customFields} |
| 150 | reports={clientReports} |
| 151 | /> |
| 152 | </div> |
| 153 | ) |
| 154 | } |
| 155 | |
| 156 | return ( |
| 157 | <div className='w-full |
| 158 | <div className='mb-6 flex items-center justify-between |
| 159 | <div className='flex items-center gap-4 |
| 160 | <h1 className='font-semibold text-2xl |
| 161 | {budgetStatus && isAdmin |
| 162 | <BudgetIndicator budgetStatus={ |
| 163 | )} |
| 164 | </div> |
| 165 | <div className='flex items-center gap-2 |
| 166 | {isAdmin && isClientInvolved |
| 167 | <Button |
| 168 | disabled={selectedEntryIds.size |
| 169 | onClick={() => |
| 170 | variant='outline' |
| 171 | > |
| 172 | <Send className=' |
| 173 | Send to Client |
| 174 | {selectedEntryIds.size > |
| 175 | </Button> |
| 176 | )} |
| 177 | {isAdmin && ( |
| 178 | <Button |
| 179 | kbd='r' |
| 180 | onClick={() => |
| 181 | variant='outline' |
| 182 | > |
| 183 | <Settings2 className=' |
| 184 | Rates |
| 185 | </Button> |
| 186 | )} |
| 187 | <Button |
| 188 | disabled={timerActive} |
| 189 | kbd='t' |
| 190 | onClick={() => |
| 191 | startTimer({ orgSlug, |
| 192 | } |
| 193 | variant='outline' |
| 194 | > |
| 195 | <Play className=' |
| 196 | {timerStartedAt ? 'Recording… |
| 197 | </Button> |
| 198 | <Button kbd=' |
| 199 | <Plus className=' |
| 200 | Log Time |
| 201 | </Button> |
| 202 | </div> |
| 203 | </div> |
| 204 | |
| 205 | <Tabs defaultValue={ |
| 206 | <TabsList className=' |
| 207 | <TabsTrigger value=' |
| 208 | {isAdmin && ( |
| 209 | <> |
| 210 | <TabsTrigger value=' |
| 211 | <TabsTrigger value=' |
| 212 | Approval |
| 213 | {submittedEntries.length > |
| 214 | <span className='ml-1.5 inline-flex size-5 items-center justify-center rounded-full bg-primary text-primary-foreground text-xs |
| 215 | {submittedEntries.length} |
| 216 | </span> |
| 217 | )} |
| 218 | </TabsTrigger> |
| 219 | {isClientInvolved && ( |
| 220 | <TabsTrigger value=' |
| 221 | Client Reports |
| 222 | {disputedReports.length > |
| 223 | <span className='ml-1.5 inline-flex size-5 items-center justify-center rounded-full bg-destructive text-destructive-foreground text-xs |
| 224 | {disputedReports.length} |
| 225 | </span> |
| 226 | )} |
| 227 | </TabsTrigger> |
| 228 | )} |
| 229 | </> |
| 230 | )} |
| 231 | </TabsList> |
| 232 | |
| 233 | <TabsContent value=' |
| 234 | {timesheetDuration === 'monthly |
| 235 | <MonthlyTimesheet |
| 236 | currentMemberId={currentMemberId} |
| 237 | customFields={customFields} |
| 238 | entries={myEntries} |
| 239 | isAdmin={false} |
| 240 | isClientInvolved={isClientInvolved} |
| 241 | onAddEntry={(date) |
| 242 | orgSlug={orgSlug} |
| 243 | projectId={projectId} |
| 244 | projectSlug={projectSlug} |
| 245 | requirements={requirements} |
| 246 | /> |
| 247 | ) : timesheetDuration === |
| 248 | <BiweeklyTimesheet |
| 249 | currentMemberId={currentMemberId} |
| 250 | customFields={customFields} |
| 251 | entries={myEntries} |
| 252 | isAdmin={false} |
| 253 | isClientInvolved={isClientInvolved} |
| 254 | onAddEntry={() => |
| 255 | orgSlug={orgSlug} |
| 256 | projectId={projectId} |
| 257 | projectSlug={projectSlug} |
| 258 | requirements={requirements} |
| 259 | /> |
| 260 | ) : ( |
| 261 | <WeeklyTimesheet |
| 262 | currentMemberId={currentMemberId} |
| 263 | customFields={customFields} |
| 264 | entries={myEntries} |
| 265 | isAdmin={false} |
| 266 | isClientInvolved={isClientInvolved} |
| 267 | onAddEntry={() => |
| 268 | orgSlug={orgSlug} |
| 269 | projectId={projectId} |
| 270 | projectSlug={projectSlug} |
| 271 | requirements={requirements} |
| 272 | /> |
| 273 | )} |
| 274 | </TabsContent> |
| 275 | |
| 276 | {isAdmin && ( |
| 277 | <> |
| 278 | <TabsContent value=' |
| 279 | <TeamEntriesTable |
| 280 | currentMemberId={currentMemberId} |
| 281 | customFields={customFields} |
| 282 | entries={entries.filter |
| 283 | isClientInvolved={isClientInvolved} |
| 284 | onSelectionChange={setSelectedEntryIds} |
| 285 | orgSlug={orgSlug} |
| 286 | projectId={projectId} |
| 287 | projectMembers={projectMembers} |
| 288 | projectSlug={projectSlug} |
| 289 | requirements={requirements} |
| 290 | selectedIds={selectedEntryIds} |
| 291 | /> |
| 292 | </TabsContent> |
| 293 | |
| 294 | <TabsContent value=' |
| 295 | <TimesheetApproval |
| 296 | customFields={customFields} |
| 297 | entries={submittedEntries} |
| 298 | orgSlug={orgSlug} |
| 299 | projectId={projectId} |
| 300 | projectMembers={projectMembers} |
| 301 | projectSlug={projectSlug} |
| 302 | requirements={requirements} |
| 303 | /> |
| 304 | </TabsContent> |
| 305 | |
| 306 | {isClientInvolved && ( |
| 307 | <TabsContent value=' |
| 308 | <SentReportsList |
| 309 | isAdmin={isAdmin} |
| 310 | orgSlug={orgSlug} |
| 311 | projectName={projectName} |
| 312 | projectSlug={projectSlug} |
| 313 | reportEntriesMap={reportEntriesMap} |
| 314 | reportRecipientsMap={reportRecipientsMap} |
| 315 | reports={timesheetReports} |
| 316 | /> |
| 317 | </TabsContent> |
| 318 | )} |
| 319 | </> |
| 320 | )} |
| 321 | </Tabs> |
| 322 | |
| 323 | <TimeEntryForm |
| 324 | customFields={customFields} |
| 325 | defaultDate={formDefaultDate} |
| 326 | defaultDurationMinutes={formDefaultDuration} |
| 327 | onOpenChange={(open) |
| 328 | setFormOpen(open) |
| 329 | if (!open) { |
| 330 | setFormDefaultDate(undefined) |
| 331 | setFormDefaultDuration(undefined) |
| 332 | } |
| 333 | }} |
| 334 | open={formOpen} |
| 335 | projectId={projectId} |
| 336 | requirements={requirements} |
| 337 | /> |
| 338 | |
| 339 | {isAdmin && isClientInvolved |
| 340 | <SendToClientDialog |
| 341 | clients={clients} |
| 342 | description={`Send ${selectedEntryIds |
| 343 | onOpenChange={(open) |
| 344 | setSendOpen(open) |
| 345 | if (!open) { |
| 346 | setSelectedEntryIds(new Set |
| 347 | setReportTitle('') |
| 348 | } |
| 349 | }} |
| 350 | onSend={async ( |
| 351 | await sendTimesheetToClientAction({ |
| 352 | projectId, |
| 353 | clientMemberIds, |
| 354 | title: reportTitle.trim |
| 355 | timeEntryIds: [...selectedEntryIds] |
| 356 | }) |
| 357 | toast.success('Timesheet sent to client |
| 358 | setSendOpen(false) |
| 359 | setSelectedEntryIds(new Set |
| 360 | setReportTitle('') |
| 361 | router.refresh() |
| 362 | }} |
| 363 | open={sendOpen} |
| 364 | recipientLabel='client' |
| 365 | sendDisabled={!reportTitle. |
| 366 | title='Send Timesheet to Client' |
| 367 | > |
| 368 | <div className='space-y-1.5 |
| 369 | <Label>Report Title</Label |
| 370 | <Input |
| 371 | onChange={(e) |
| 372 | placeholder='e.g. Week of Mar 3 – Mar 9' |
| 373 | value={reportTitle} |
| 374 | /> |
| 375 | </div> |
| 376 | </SendToClientDialog> |
| 377 | )} |
| 378 | |
| 379 | {isAdmin && ( |
| 380 | <MemberRatesDialog |
| 381 | defaultCurrency={defaultCurrency} |
| 382 | existingRates={memberRates} |
| 383 | onOpenChange={setRatesOpen} |
| 384 | open={ratesOpen} |
| 385 | projectId={projectId} |
| 386 | projectMembers={projectMembers} |
| 387 | /> |
| 388 | )} |
| 389 | </div> |
| 390 | ) |
| 391 | } |