-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import * as proto from "@bufbuild/protobuf";
import { tz } from "@date-fns/tz";
import { CaretLeftIcon, CaretRightIcon } from "@radix-ui/react-icons";
import {
Button,
Box,
Container,
DropdownMenu,
Flex,
Link,
Table,
Text,
Tooltip,
} from "@radix-ui/themes";
import { DateSchema } from "@yamori/proto/yamori/type/v1/date_pb.js";
import { LeaveSchema } from "@yamori/proto/yamori/work_record/v1/leave_pb.js";
import { WorkspaceSchema } from "@yamori/proto/yamori/workspace/v1/workspace_pb.js";
import { GetRequestSchema } from "@yamori/proto/yamori/workspace/v1/get_request_pb.js";
import { GetResponseSchema } from "@yamori/proto/yamori/workspace/v1/get_response_pb.js";
import { WorkerSchema } from "@yamori/proto/yamori/worker/v1/worker_pb.js";
import { ListRequestSchema } from "@yamori/proto/yamori/worker/v1/list_request_pb.js";
import { ListResponseSchema } from "@yamori/proto/yamori/worker/v1/list_response_pb.js";
import { addMonths, startOfMonth, endOfMonth } from "date-fns";
import { type FC, type ReactNode, use, useEffect, useMemo } from "react";
import { ManagedErrorCallout } from "../../../components/ErrorCallout.ts";
import { NavigationContext, URLContext } from "../../../contexts/Router.tsx";
import { useMethodQuery } from "../../../contexts/Service.tsx";
import { IllegalMessageError } from "../../../errors/IllegalMessageError.ts";
import { fromProtoDate, toProtoDate } from "../../../helpers.ts";
import * as workerDashboard from "../workers/:id/Dashboard.tsx";
import { Layout } from "../Layout.tsx";
import { buildSearchParams, deserializeDate, href } from "./meta.ts";
import css from "./page.module.css";
export { href, pattern } from "./meta.ts";
export const Title: FC = () => "日数明細";
interface DateRange {
since: proto.MessageShape<typeof DateSchema>;
until: proto.MessageShape<typeof DateSchema>;
}
interface DayCountProps {
suffix?: ReactNode;
count: number;
}
const DayCount: FC<DayCountProps> = ({ suffix = "日", count }) => {
if (!count) {
return (
<Text size="2" color="gray" style={{ opacity: 0.2 }}>
---
</Text>
);
}
return (
<Text size="3">
{count}
<Text ml="1" size="1">
{suffix}
</Text>
</Text>
);
};
const monthFormatter = new Intl.DateTimeFormat(navigator.language, {
year: "numeric",
month: "numeric",
});
interface BodyProps {
workspace: proto.MessageShape<typeof WorkspaceSchema>;
range: DateRange;
onChangeDateRange(range: DateRange): void;
}
const Body: FC<BodyProps> = ({
workspace,
range: { since, until },
onChangeDateRange,
}) => {
const workspaceConfig = useMethodQuery({
service: "yamori.workspace.v1.WorkspaceService",
method: "Get",
request: {
schema: GetRequestSchema,
data: {
workspaceId: workspace.id,
readMask: {
fields: [
WorkspaceSchema.field.abbreviations.number,
WorkspaceSchema.field.leaveDefinitions.number,
],
leaveDefinitionsMask: {
fields: [
LeaveSchema.field.id.number,
LeaveSchema.field.displayName.number,
LeaveSchema.field.abbreviationName.number,
],
},
},
},
},
response: {
schema: GetResponseSchema,
},
mapResponse(resp) {
switch (resp.result.case) {
case "ok":
return resp.result.value;
case undefined:
throw new IllegalMessageError(resp);
default:
throw resp.result.value;
}
},
});
const workers = useMethodQuery({
service: "yamori.worker.v1.WorkerService",
method: "List",
request: {
schema: ListRequestSchema,
data: {
workspaceId: workspace.id,
workRecordFilter: {
since,
until,
},
readMask: {
fields: [
WorkerSchema.field.id.number,
WorkerSchema.field.displayName.number,
WorkerSchema.field.workRecords.number,
],
workRecordsMask: {
workspaceDefinedLeaveMask: {
fields: [LeaveSchema.field.id.number],
},
},
},
},
},
response: {
schema: ListResponseSchema,
},
mapResponse(resp) {
switch (resp.result.case) {
case "ok":
return resp.result.value.workers;
case undefined:
throw new IllegalMessageError(resp);
default:
throw resp.result.value;
}
},
});
const prevMonth = useMemo(() => {
// TODO: TZ をどこか共通の場所で定義する
return addMonths(fromProtoDate(since), -1, { in: tz("Asia/Tokyo") });
}, [since]);
const nextMonth = useMemo(() => {
// TODO: TZ をどこか共通の場所で定義する
return addMonths(fromProtoDate(since), 1, { in: tz("Asia/Tokyo") });
}, [since]);
return (
<Flex position="absolute" inset="0" direction="column">
{workspaceConfig.isError && (
<ManagedErrorCallout
error={workspaceConfig.error}
title="ワークスペース設定が取得できませんでした"
actions={
<Button
size="1"
loading={workspaceConfig.isFetching}
onClick={() => void workspaceConfig.refetch()}
>
再試行
</Button>
}
/>
)}
{workers.isError && (
<ManagedErrorCallout
error={workers.error}
title="労働者一覧を取得できませんでした"
actions={
<Button
size="1"
loading={workspaceConfig.isFetching}
onClick={() => void workers.refetch()}
>
再試行
</Button>
}
/>
)}
<Container p="3" flexShrink="0" flexGrow="0">
<Flex align="center" justify="end" gap="1">
<Button
variant="soft"
onClick={() => {
onChangeDateRange({
// TODO: TZ をどこか共通の場所で定義する
since: toProtoDate(startOfMonth(prevMonth, { in: tz("Asia/Tokyo") })),
until: toProtoDate(endOfMonth(prevMonth, { in: tz("Asia/Tokyo") })),
});
}}
>
<CaretLeftIcon />
{monthFormatter.format(prevMonth)}
</Button>
<Button
variant="soft"
onClick={() => {
onChangeDateRange({
// TODO: TZ をどこか共通の場所で定義する
since: toProtoDate(startOfMonth(nextMonth, { in: tz("Asia/Tokyo") })),
until: toProtoDate(endOfMonth(nextMonth, { in: tz("Asia/Tokyo") })),
});
}}
>
{monthFormatter.format(nextMonth)}
<CaretRightIcon />
</Button>
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button>
ダウンロード
<DropdownMenu.TriggerIcon />
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content align="end">
<DropdownMenu.Item disabled>TSVファイル</DropdownMenu.Item>
<DropdownMenu.Item disabled>CSVファイル</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
</Flex>
</Container>
<Box mt="2" p="2" flexGrow="1" flexShrink="1" minWidth="0" minHeight="0">
<Table.Root style={{ maxHeight: "100%" }}>
<Table.Header>
<Table.Row>
<Table.ColumnHeaderCell className={css.rowColumnHeader}>
労働者名
</Table.ColumnHeaderCell>
<Table.ColumnHeaderCell className={css.columnHeader} justify="center">
{workspaceConfig.data?.abbreviations?.worked ?? "出勤"}
</Table.ColumnHeaderCell>
<Table.ColumnHeaderCell className={css.columnHeader} justify="center">
{workspaceConfig.data?.abbreviations?.dayoff ?? "休日"}
</Table.ColumnHeaderCell>
<Table.ColumnHeaderCell className={css.columnHeader} justify="center">
{workspaceConfig.data?.abbreviations?.skipWork ?? "欠勤"}
</Table.ColumnHeaderCell>
<Table.ColumnHeaderCell className={css.columnHeader} justify="center">
<Tooltip content="年次有給休暇">
<Text>
{workspaceConfig.data?.abbreviations?.paidLeave ?? "年次有給休暇"}
</Text>
</Tooltip>
</Table.ColumnHeaderCell>
{workspaceConfig.data?.leaveDefinitions.map((def) => {
return (
<Table.ColumnHeaderCell
key={def.id?.value}
className={css.columnHeader}
justify="center"
>
<Tooltip content={def.displayName}>
<Text>{def.abbreviationName || def.displayName}</Text>
</Tooltip>
</Table.ColumnHeaderCell>
);
})}
</Table.Row>
</Table.Header>
<Table.Body>
{workers.data?.map((worker) => {
let worked = 0;
let dayoff = 0;
let skipped = 0;
let paidLeave = 0;
const workspaceDefinedLeaves = new Map<string, number>();
for (const record of worker.workRecords) {
switch (record.record.case) {
case "workingDay":
if (record.record.value.hasWorkerWorked) {
worked++;
} else {
skipped++;
}
break;
case "dayOff":
dayoff++;
break;
case "paidLeave":
paidLeave++;
break;
case "workspaceDefinedLeave":
if (record.record.value.id?.value) {
const current = workspaceDefinedLeaves.get(
record.record.value.id.value,
);
workspaceDefinedLeaves.set(
record.record.value.id.value,
(current ?? 0) + 1,
);
}
break;
}
}
return (
<Table.Row key={worker.id?.value}>
<Table.RowHeaderCell className={css.rowHeader}>
<Link href={workerDashboard.href({ workspace, worker })}>
{worker.displayName}
</Link>
</Table.RowHeaderCell>
<Table.Cell className={css.cell} justify="center">
<DayCount count={worked} />
</Table.Cell>
<Table.Cell className={css.cell} justify="center">
<DayCount count={dayoff} />
</Table.Cell>
<Table.Cell className={css.cell} justify="center">
<DayCount count={skipped} />
</Table.Cell>
<Table.Cell className={css.cell} justify="center">
<DayCount count={paidLeave} />
</Table.Cell>
{workspaceConfig.data?.leaveDefinitions.map((def) => {
return (
<Table.Cell
key={def.id?.value}
className={css.cell}
justify="center"
>
<DayCount
count={
(def.id && workspaceDefinedLeaves.get(def.id.value)) || 0
}
/>
</Table.Cell>
);
})}
</Table.Row>
);
})}
</Table.Body>
</Table.Root>
</Box>
</Flex>
);
};
export interface PageProps {
workspace: proto.MessageShape<typeof WorkspaceSchema>;
}
export const Page: FC<PageProps> = ({ workspace }) => {
const navigation = use(NavigationContext);
const url = use(URLContext);
const searchParams = useMemo(() => new URLSearchParams(url.search), [url.search]);
const range = useMemo<DateRange>(() => {
const parseOr = (
paramName: string,
fallback: Date,
): proto.MessageShape<typeof DateSchema> => {
const param = searchParams.get(paramName);
if (!param) {
return toProtoDate(fallback);
}
return deserializeDate(param) || toProtoDate(fallback);
};
// TODO: TZ をどこか共通の場所で定義する
const since = parseOr("since", startOfMonth(Date.now(), { in: tz("Asia/Tokyo") }));
const until = parseOr("until", endOfMonth(Date.now(), { in: tz("Asia/Tokyo") }));
return { since, until };
}, [searchParams]);
// ブラウザのクエリパラメータと since/until が違う場合はブラウザのクエリパラメータを
// 書き換える。
useEffect(() => {
const next = buildSearchParams({ since: range.since, until: range.until });
if (next.toString() === searchParams.toString()) {
return;
}
navigation.replace(href({ workspace, since: range.since, until: range.until }));
}, [searchParams, range]);
return (
<Layout workspace={workspace} title={<Title />}>
<Body
workspace={workspace}
range={range}
onChangeDateRange={({ since, until }) => {
navigation.replace(
href({
workspace,
since,
until,
}),
);
}}
/>
</Layout>
);
};