@giraffesyo/timeclock 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +72 -2
- package/dist/index.d.ts +85 -0
- package/dist/index.js +162 -0
- package/dist/react.d.ts +96 -0
- package/dist/react.js +223 -0
- package/dist/styles.css +236 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @giraffesyo/timeclock
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [Timeclock](https://github.com/giraffesyo/timeclock) clock for a host application: start, stop and move the clock from your own header, without sending people to Timeclock's pages.
|
|
4
|
+
|
|
5
|
+
It talks to a Timeclock mounted on the same origin (for example at `/timeclock`), as the person your application has signed in. There are three layers; use the lowest one that does what you need.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @giraffesyo/timeclock
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## The bar
|
|
12
|
+
|
|
13
|
+
A ready-made bar: what you are working on, the project, the running time, and a start/stop button. It draws nothing until Timeclock has answered, and nothing for someone Timeclock doesn't know.
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { ClockBar } from '@giraffesyo/timeclock/react';
|
|
17
|
+
import '@giraffesyo/timeclock/styles.css';
|
|
18
|
+
|
|
19
|
+
<ClockBar
|
|
20
|
+
basePath="/timeclock"
|
|
21
|
+
onError={(err, action) => notify(`Couldn't ${action}: ${err.message}`)}
|
|
22
|
+
onSwitched={(entry) => notify('Moved the clock')}
|
|
23
|
+
/>;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- While the clock runs, choosing another project moves the clock to it without stopping: the time so far stays on the project it was on.
|
|
27
|
+
- The note saves when the field is left; a note typed just before choosing a project goes with the new stretch.
|
|
28
|
+
- `labels` replaces any of its words, for another language or voice.
|
|
29
|
+
- Colors come from `--tc-*` custom properties. They default to [@parallelworks/ui](https://www.npmjs.com/package/@parallelworks/ui)'s theme tokens (`--theme-*`) when the page has them, and to plain light values when it doesn't. Set `--tc-bg`, `--tc-fg`, `--tc-muted`, `--tc-border`, `--tc-hover`, `--tc-accent`, `--tc-accent-fg` and `--tc-stop` on an ancestor to fit another look.
|
|
30
|
+
|
|
31
|
+
`ProjectPicker` and `ProjectDot` are exported too.
|
|
32
|
+
|
|
33
|
+
## The hook
|
|
34
|
+
|
|
35
|
+
The clock with no look, to draw your own:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useClock } from '@giraffesyo/timeclock/react';
|
|
39
|
+
|
|
40
|
+
function HeaderClock() {
|
|
41
|
+
const clock = useClock({ basePath: '/timeclock' });
|
|
42
|
+
if (clock.status !== 'ready') return null;
|
|
43
|
+
return clock.running ? (
|
|
44
|
+
<button onClick={() => clock.stop()}>Stop</button>
|
|
45
|
+
) : (
|
|
46
|
+
<button onClick={() => clock.start()}>Start</button>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`useClock()` gives the state (`status`, `running`, `projects`, `requireProject`, `locked`, `timeZone`, `busy`), what is being typed (`note`, `setNote`, `projectId`), and what can be done (`start`, `stop`, `chooseProject`, `saveNote`, `resume`). Everything that changes the clock returns a promise that rejects with a `ClockError` when Timeclock refuses.
|
|
52
|
+
|
|
53
|
+
## The store
|
|
54
|
+
|
|
55
|
+
No React at all:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { clockFor } from '@giraffesyo/timeclock';
|
|
59
|
+
|
|
60
|
+
const clock = clockFor('/timeclock');
|
|
61
|
+
const stop = clock.subscribe(() => render(clock.getState()));
|
|
62
|
+
await clock.start({ projectId, note: 'Standup' });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`clockFor(basePath)` is one store for the page, so everything showing the clock agrees. While something is subscribed it asks Timeclock again every minute and whenever the tab comes back, since a clock can be stopped from another tab. `createClock(options)` makes a separate one, with your own `fetch` or polling interval.
|
|
66
|
+
|
|
67
|
+
## Errors
|
|
68
|
+
|
|
69
|
+
A `ClockError` carries the HTTP `status`, the problem's stable `code` (such as `project_required` or `period_locked`), a `message` in English, and the RFC 9457 problem itself as `body`.
|
|
70
|
+
|
|
71
|
+
## Versions
|
|
72
|
+
|
|
73
|
+
The package follows the server's API under `/api/v1`. A host should run a Timeclock at least as new as the package it uses.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** A stretch of work. `endedAt` is absent while the clock is running. */
|
|
2
|
+
export interface Entry {
|
|
3
|
+
id: string;
|
|
4
|
+
personId: string;
|
|
5
|
+
projectId?: string;
|
|
6
|
+
startedAt: string;
|
|
7
|
+
endedAt?: string;
|
|
8
|
+
note: string;
|
|
9
|
+
}
|
|
10
|
+
/** A project time is recorded on. `customerName` is empty for internal work. */
|
|
11
|
+
export interface Project {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
customerName: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ClockState {
|
|
17
|
+
/**
|
|
18
|
+
* `loading` until the first answer; `signed-out` when Timeclock doesn't
|
|
19
|
+
* know the caller; `unavailable` when it can't be reached.
|
|
20
|
+
*/
|
|
21
|
+
status: 'loading' | 'ready' | 'signed-out' | 'unavailable';
|
|
22
|
+
/** The running stretch, if the clock is on. */
|
|
23
|
+
running: Entry | null;
|
|
24
|
+
/** The projects that take new time, internal ones first, then by customer. */
|
|
25
|
+
projects: Project[];
|
|
26
|
+
/** Whether the organization requires a project on every entry. */
|
|
27
|
+
requireProject: boolean;
|
|
28
|
+
/** The pay period's timesheet is in, so the clock is off. */
|
|
29
|
+
locked: boolean;
|
|
30
|
+
/** The IANA time zone the person's times are shown in. */
|
|
31
|
+
timeZone: string;
|
|
32
|
+
/** A change is on its way. */
|
|
33
|
+
busy: boolean;
|
|
34
|
+
}
|
|
35
|
+
export interface ClockOptions {
|
|
36
|
+
/** Where Timeclock is mounted, such as "/timeclock". Empty at the site root. */
|
|
37
|
+
basePath?: string;
|
|
38
|
+
/** How to make requests; the page's own fetch by default. */
|
|
39
|
+
fetch?: typeof fetch;
|
|
40
|
+
/** How often to ask again while something is listening, in milliseconds. A clock can be stopped from another tab. */
|
|
41
|
+
pollMs?: number;
|
|
42
|
+
}
|
|
43
|
+
/** What starts or moves the clock: a project and a note, both optional. */
|
|
44
|
+
export interface ClockInput {
|
|
45
|
+
projectId?: string;
|
|
46
|
+
note?: string;
|
|
47
|
+
}
|
|
48
|
+
/** A request Timeclock refused. `body` is the RFC 9457 problem it answered with. */
|
|
49
|
+
export declare class ClockError extends Error {
|
|
50
|
+
readonly status: number;
|
|
51
|
+
/** The problem's stable code, such as "project_required". */
|
|
52
|
+
readonly code: string;
|
|
53
|
+
readonly body: unknown;
|
|
54
|
+
constructor(status: number,
|
|
55
|
+
/** The problem's stable code, such as "project_required". */
|
|
56
|
+
code: string, message: string, body: unknown);
|
|
57
|
+
}
|
|
58
|
+
export interface ClockStore {
|
|
59
|
+
getState(): ClockState;
|
|
60
|
+
/** Calls `listener` after every change. Listening keeps the store fresh; returns how to stop. */
|
|
61
|
+
subscribe(listener: () => void): () => void;
|
|
62
|
+
/** Asks Timeclock again. */
|
|
63
|
+
refresh(): Promise<void>;
|
|
64
|
+
start(input?: ClockInput): Promise<Entry>;
|
|
65
|
+
stop(): Promise<Entry>;
|
|
66
|
+
/**
|
|
67
|
+
* Moves the running clock to other work without stopping it: the time so
|
|
68
|
+
* far stays where it was, and the clock goes on from this instant.
|
|
69
|
+
*/
|
|
70
|
+
switchTo(input: ClockInput): Promise<Entry>;
|
|
71
|
+
/** Sets the running stretch's note. */
|
|
72
|
+
saveNote(note: string): Promise<Entry>;
|
|
73
|
+
/** Starts the clock on what an entry was about; a running clock moves to it. */
|
|
74
|
+
resume(entry: Pick<Entry, 'projectId' | 'note'>): Promise<Entry>;
|
|
75
|
+
}
|
|
76
|
+
/** Makes a clock store. Most hosts want {@link clockFor}, which shares one per base path. */
|
|
77
|
+
export declare function createClock(options?: ClockOptions): ClockStore;
|
|
78
|
+
/** The clock store for a Timeclock at a base path: one for the page, so everything showing the clock agrees. */
|
|
79
|
+
export declare function clockFor(basePath?: string): ClockStore;
|
|
80
|
+
/** "Customer / Project", or just the project for internal work. */
|
|
81
|
+
export declare function projectLabel(project: Project): string;
|
|
82
|
+
/** A project's hue (0–359), the same wherever its time is shown. */
|
|
83
|
+
export declare function projectHue(projectId: string): number;
|
|
84
|
+
/** Milliseconds as H:MM:SS, for a running clock. */
|
|
85
|
+
export declare function stopwatch(ms: number): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// The Timeclock clock for a host application, with no look and no framework:
|
|
2
|
+
// a store that knows what is running and can start, stop and move the clock.
|
|
3
|
+
// It talks to a Timeclock mounted on the same origin, as the signed-in person.
|
|
4
|
+
/** A request Timeclock refused. `body` is the RFC 9457 problem it answered with. */
|
|
5
|
+
export class ClockError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
code;
|
|
8
|
+
body;
|
|
9
|
+
constructor(status,
|
|
10
|
+
/** The problem's stable code, such as "project_required". */
|
|
11
|
+
code, message, body) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.body = body;
|
|
16
|
+
this.name = 'ClockError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const initial = {
|
|
20
|
+
status: 'loading',
|
|
21
|
+
running: null,
|
|
22
|
+
projects: [],
|
|
23
|
+
requireProject: false,
|
|
24
|
+
locked: false,
|
|
25
|
+
timeZone: 'UTC',
|
|
26
|
+
busy: false,
|
|
27
|
+
};
|
|
28
|
+
/** Makes a clock store. Most hosts want {@link clockFor}, which shares one per base path. */
|
|
29
|
+
export function createClock(options = {}) {
|
|
30
|
+
const base = `${(options.basePath ?? '').replace(/\/$/, '')}/api/v1`;
|
|
31
|
+
const request = options.fetch ?? ((input, init) => fetch(input, init));
|
|
32
|
+
const pollMs = options.pollMs ?? 60_000;
|
|
33
|
+
const listeners = new Set();
|
|
34
|
+
let state = initial;
|
|
35
|
+
let timer;
|
|
36
|
+
const set = (patch) => {
|
|
37
|
+
state = { ...state, ...patch };
|
|
38
|
+
for (const listener of listeners)
|
|
39
|
+
listener();
|
|
40
|
+
};
|
|
41
|
+
async function call(method, path, body) {
|
|
42
|
+
const res = await request(base + path, {
|
|
43
|
+
method,
|
|
44
|
+
credentials: 'same-origin',
|
|
45
|
+
headers: body === undefined
|
|
46
|
+
? { Accept: 'application/json' }
|
|
47
|
+
: { Accept: 'application/json', 'Content-Type': 'application/json' },
|
|
48
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
49
|
+
});
|
|
50
|
+
const text = await res.text();
|
|
51
|
+
let json;
|
|
52
|
+
try {
|
|
53
|
+
json = text ? JSON.parse(text) : undefined;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
json = undefined;
|
|
57
|
+
}
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
const problem = (json ?? {});
|
|
60
|
+
throw new ClockError(res.status, problem.code ?? '', problem.detail || problem.title || res.statusText, json);
|
|
61
|
+
}
|
|
62
|
+
return json;
|
|
63
|
+
}
|
|
64
|
+
const refresh = async () => {
|
|
65
|
+
try {
|
|
66
|
+
const [me, projects, sheet] = await Promise.all([
|
|
67
|
+
call('GET', '/me'),
|
|
68
|
+
call('GET', '/projects'),
|
|
69
|
+
call('GET', '/timesheet'),
|
|
70
|
+
]);
|
|
71
|
+
const status = sheet.timesheet?.status;
|
|
72
|
+
set({
|
|
73
|
+
status: 'ready',
|
|
74
|
+
running: me.running ?? null,
|
|
75
|
+
projects: projects.projects ?? [],
|
|
76
|
+
requireProject: me.settings.requireProject,
|
|
77
|
+
locked: status === 'submitted' || status === 'approved',
|
|
78
|
+
timeZone: me.person.timezone || me.settings.timezone,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
set({ status: err instanceof ClockError && err.status === 401 ? 'signed-out' : 'unavailable' });
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
// One change at a time: the bar shows it is busy, then what is now true.
|
|
86
|
+
const change = async (run) => {
|
|
87
|
+
set({ busy: true });
|
|
88
|
+
try {
|
|
89
|
+
const entry = await run();
|
|
90
|
+
set({ running: entry.endedAt ? null : entry });
|
|
91
|
+
return entry;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
set({ busy: false });
|
|
95
|
+
void refresh();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const onVisible = () => {
|
|
99
|
+
if (document.visibilityState === 'visible')
|
|
100
|
+
void refresh();
|
|
101
|
+
};
|
|
102
|
+
const store = {
|
|
103
|
+
getState: () => state,
|
|
104
|
+
subscribe(listener) {
|
|
105
|
+
listeners.add(listener);
|
|
106
|
+
if (listeners.size === 1) {
|
|
107
|
+
void refresh();
|
|
108
|
+
timer = setInterval(() => void refresh(), pollMs);
|
|
109
|
+
if (typeof document !== 'undefined')
|
|
110
|
+
document.addEventListener('visibilitychange', onVisible);
|
|
111
|
+
}
|
|
112
|
+
return () => {
|
|
113
|
+
listeners.delete(listener);
|
|
114
|
+
if (listeners.size === 0) {
|
|
115
|
+
clearInterval(timer);
|
|
116
|
+
if (typeof document !== 'undefined')
|
|
117
|
+
document.removeEventListener('visibilitychange', onVisible);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
refresh,
|
|
122
|
+
start: (input = {}) => change(() => call('POST', '/clock/in', { projectId: input.projectId || undefined, note: input.note ?? '' })),
|
|
123
|
+
stop: () => change(() => call('POST', '/clock/out')),
|
|
124
|
+
switchTo: (input) => change(() => call('POST', '/clock/switch', { projectId: input.projectId || undefined, note: input.note ?? '' })),
|
|
125
|
+
saveNote: (note) => {
|
|
126
|
+
const running = state.running;
|
|
127
|
+
if (!running)
|
|
128
|
+
return Promise.reject(new ClockError(409, 'clock_not_running', 'the clock is not running', undefined));
|
|
129
|
+
return change(() => call('PUT', `/entries/${running.id}`, { projectId: running.projectId, startedAt: running.startedAt, note }));
|
|
130
|
+
},
|
|
131
|
+
resume: (entry) => (state.running ? store.switchTo(entry) : store.start(entry)),
|
|
132
|
+
};
|
|
133
|
+
return store;
|
|
134
|
+
}
|
|
135
|
+
const shared = new Map();
|
|
136
|
+
/** The clock store for a Timeclock at a base path: one for the page, so everything showing the clock agrees. */
|
|
137
|
+
export function clockFor(basePath = '') {
|
|
138
|
+
let store = shared.get(basePath);
|
|
139
|
+
if (!store) {
|
|
140
|
+
store = createClock({ basePath });
|
|
141
|
+
shared.set(basePath, store);
|
|
142
|
+
}
|
|
143
|
+
return store;
|
|
144
|
+
}
|
|
145
|
+
/** "Customer / Project", or just the project for internal work. */
|
|
146
|
+
export function projectLabel(project) {
|
|
147
|
+
return project.customerName ? `${project.customerName} / ${project.name}` : project.name;
|
|
148
|
+
}
|
|
149
|
+
/** A project's hue (0–359), the same wherever its time is shown. */
|
|
150
|
+
export function projectHue(projectId) {
|
|
151
|
+
let h = 0;
|
|
152
|
+
for (let i = 0; i < projectId.length; i++)
|
|
153
|
+
h = (h * 31 + projectId.charCodeAt(i)) >>> 0;
|
|
154
|
+
// Steps of the golden angle keep any two projects' hues apart.
|
|
155
|
+
return Math.round((h % 997) * 137.508) % 360;
|
|
156
|
+
}
|
|
157
|
+
/** Milliseconds as H:MM:SS, for a running clock. */
|
|
158
|
+
export function stopwatch(ms) {
|
|
159
|
+
const s = Math.max(0, Math.floor(ms / 1000));
|
|
160
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
161
|
+
return `${Math.floor(s / 3600)}:${pad(Math.floor((s % 3600) / 60))}:${pad(s % 60)}`;
|
|
162
|
+
}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type ClockError, type ClockState, type ClockStore, type Entry, type Project } from './index.js';
|
|
2
|
+
export interface UseClockOptions {
|
|
3
|
+
/** Where Timeclock is mounted, such as "/timeclock". Ignored when `store` is given. */
|
|
4
|
+
basePath?: string;
|
|
5
|
+
/** A store made with createClock(), instead of the page's shared one. */
|
|
6
|
+
store?: ClockStore;
|
|
7
|
+
}
|
|
8
|
+
/** The clock without a look: what is running, what is being typed, and what can be done about it. */
|
|
9
|
+
export interface Clock extends ClockState {
|
|
10
|
+
/** The note in the field: the running stretch's, or the one to start with. */
|
|
11
|
+
note: string;
|
|
12
|
+
setNote: (note: string) => void;
|
|
13
|
+
/** Whether the note differs from what is saved on the running stretch. */
|
|
14
|
+
noteEdited: boolean;
|
|
15
|
+
/** The project in the picker: the running stretch's, or the one to start on. */
|
|
16
|
+
projectId: string;
|
|
17
|
+
/** Picks a project. While the clock runs this moves the clock to it, and answers with the new stretch. */
|
|
18
|
+
chooseProject: (projectId: string) => Promise<Entry | null>;
|
|
19
|
+
/** Saves an edited note onto the running stretch. */
|
|
20
|
+
saveNote: () => Promise<void>;
|
|
21
|
+
start: () => Promise<Entry>;
|
|
22
|
+
/** Stops the clock, saving an edited note first. */
|
|
23
|
+
stop: () => Promise<Entry>;
|
|
24
|
+
/** Starts the clock on what an entry was about; a running clock moves to it. */
|
|
25
|
+
resume: (entry: Pick<Entry, 'projectId' | 'note'>) => Promise<Entry>;
|
|
26
|
+
/** The store underneath, for anything else. */
|
|
27
|
+
store: ClockStore;
|
|
28
|
+
}
|
|
29
|
+
/** The clock of the Timeclock at a base path, kept current while the component is mounted. */
|
|
30
|
+
export declare function useClock(options?: UseClockOptions): Clock;
|
|
31
|
+
export interface ProjectPickerLabels {
|
|
32
|
+
/** What the control is: "Project". */
|
|
33
|
+
project: string;
|
|
34
|
+
/** Shown when nothing is chosen and nothing has to be. */
|
|
35
|
+
none: string;
|
|
36
|
+
/** Shown when a project must be chosen and none is. */
|
|
37
|
+
choose: string;
|
|
38
|
+
search: string;
|
|
39
|
+
noMatch: string;
|
|
40
|
+
}
|
|
41
|
+
/** A project's color, as a dot: the same hue its time has everywhere. */
|
|
42
|
+
export declare function ProjectDot({ projectId, className }: {
|
|
43
|
+
projectId?: string | null;
|
|
44
|
+
className?: string;
|
|
45
|
+
}): import("react").JSX.Element;
|
|
46
|
+
/**
|
|
47
|
+
* Picks the project time is recorded on: a button that opens a list to
|
|
48
|
+
* search, grouped by customer.
|
|
49
|
+
*/
|
|
50
|
+
export declare function ProjectPicker({ projects, value, onChange, required, disabled, className, id, label, labels: given, }: {
|
|
51
|
+
/** The projects to choose from. One that is the value but not among them is still shown as chosen. */
|
|
52
|
+
projects: Project[];
|
|
53
|
+
value: string;
|
|
54
|
+
onChange: (projectId: string) => void;
|
|
55
|
+
/** A project must be chosen: there is no "no project" choice. */
|
|
56
|
+
required?: boolean;
|
|
57
|
+
disabled?: boolean;
|
|
58
|
+
className?: string;
|
|
59
|
+
id?: string;
|
|
60
|
+
/** What the control is for, when it isn't simply the project. */
|
|
61
|
+
label?: string;
|
|
62
|
+
labels?: Partial<ProjectPickerLabels>;
|
|
63
|
+
}): import("react").JSX.Element;
|
|
64
|
+
export interface ClockBarLabels extends ProjectPickerLabels {
|
|
65
|
+
/** The bar's name, for assistive tech. */
|
|
66
|
+
clock: string;
|
|
67
|
+
notePlaceholder: string;
|
|
68
|
+
noteLabel: string;
|
|
69
|
+
start: string;
|
|
70
|
+
stop: string;
|
|
71
|
+
/** What the picker is for while the clock runs. */
|
|
72
|
+
switchProject: string;
|
|
73
|
+
/** Replaces the note's placeholder when the pay period's timesheet is in. */
|
|
74
|
+
locked: string;
|
|
75
|
+
/** When the running stretch started, given the time as text. */
|
|
76
|
+
since: (time: string) => string;
|
|
77
|
+
}
|
|
78
|
+
/** What the person was doing when something was refused. */
|
|
79
|
+
export type ClockAction = 'start' | 'stop' | 'switch' | 'note';
|
|
80
|
+
/**
|
|
81
|
+
* The clock as a bar: say what you are working on, pick its project, and
|
|
82
|
+
* start. While it runs the same bar changes it in place: the note is the
|
|
83
|
+
* running stretch's own, and another project moves the clock to it without
|
|
84
|
+
* stopping. It draws nothing until Timeclock has answered, and nothing for
|
|
85
|
+
* someone Timeclock doesn't know.
|
|
86
|
+
*/
|
|
87
|
+
export declare function ClockBar({ basePath, store, labels: given, className, onError, onSwitched, onChanged, }: UseClockOptions & {
|
|
88
|
+
labels?: Partial<ClockBarLabels>;
|
|
89
|
+
className?: string;
|
|
90
|
+
/** Called when Timeclock refuses something, to tell the person. */
|
|
91
|
+
onError?: (error: ClockError | unknown, action: ClockAction) => void;
|
|
92
|
+
/** Called when the running clock moved to another project, with the new stretch. */
|
|
93
|
+
onSwitched?: (entry: Entry) => void;
|
|
94
|
+
/** Called after anything the bar did took effect, for a host that shows the same time elsewhere. */
|
|
95
|
+
onChanged?: () => void;
|
|
96
|
+
}): import("react").JSX.Element | null;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// The clock for React: useClock() is the logic with no look, and ClockBar
|
|
3
|
+
// and ProjectPicker are a look for it, styled by "@giraffesyo/timeclock/styles.css".
|
|
4
|
+
import { useEffect, useId, useRef, useState, useSyncExternalStore, } from 'react';
|
|
5
|
+
import { clockFor, projectHue, projectLabel, stopwatch, } from './index.js';
|
|
6
|
+
/** The clock of the Timeclock at a base path, kept current while the component is mounted. */
|
|
7
|
+
export function useClock(options = {}) {
|
|
8
|
+
const store = options.store ?? clockFor(options.basePath);
|
|
9
|
+
const state = useSyncExternalStore(store.subscribe, store.getState, store.getState);
|
|
10
|
+
const { running } = state;
|
|
11
|
+
// What is typed belongs to one stretch (or to none yet): when the running
|
|
12
|
+
// stretch changes, the field starts over from the new one's note.
|
|
13
|
+
const stretch = running?.id ?? '';
|
|
14
|
+
const [draft, setDraft] = useState({ stretch, note: running?.note ?? '', projectId: '' });
|
|
15
|
+
const current = draft.stretch === stretch ? draft : { stretch, note: running?.note ?? '', projectId: '' };
|
|
16
|
+
const note = current.note;
|
|
17
|
+
const noteEdited = !!running && note.trim() !== running.note;
|
|
18
|
+
const saveNote = async () => {
|
|
19
|
+
if (running && noteEdited)
|
|
20
|
+
await store.saveNote(note);
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
...state,
|
|
24
|
+
store,
|
|
25
|
+
note,
|
|
26
|
+
setNote: (next) => setDraft({ ...current, note: next }),
|
|
27
|
+
noteEdited,
|
|
28
|
+
projectId: running ? (running.projectId ?? '') : current.projectId,
|
|
29
|
+
chooseProject: async (projectId) => {
|
|
30
|
+
if (!running) {
|
|
31
|
+
setDraft({ ...current, projectId });
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
// A note typed just before choosing a project describes the work being
|
|
35
|
+
// moved to, so it goes with the new stretch.
|
|
36
|
+
return store.switchTo({ projectId, note: noteEdited ? note : '' });
|
|
37
|
+
},
|
|
38
|
+
saveNote,
|
|
39
|
+
start: () => store.start({ projectId: current.projectId, note }),
|
|
40
|
+
stop: async () => {
|
|
41
|
+
await saveNote();
|
|
42
|
+
return store.stop();
|
|
43
|
+
},
|
|
44
|
+
resume: (entry) => store.resume(entry),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The current time, refreshed every second while enabled. */
|
|
48
|
+
function useSecond(enabled) {
|
|
49
|
+
const [now, setNow] = useState(() => Date.now());
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!enabled)
|
|
52
|
+
return;
|
|
53
|
+
setNow(Date.now());
|
|
54
|
+
const id = setInterval(() => setNow(Date.now()), 1000);
|
|
55
|
+
return () => clearInterval(id);
|
|
56
|
+
}, [enabled]);
|
|
57
|
+
return now;
|
|
58
|
+
}
|
|
59
|
+
const cx = (...parts) => parts.filter(Boolean).join(' ');
|
|
60
|
+
const pickerLabels = {
|
|
61
|
+
project: 'Project',
|
|
62
|
+
none: 'No project',
|
|
63
|
+
choose: 'Choose a project',
|
|
64
|
+
search: 'Search projects',
|
|
65
|
+
noMatch: 'No project matches.',
|
|
66
|
+
};
|
|
67
|
+
/** A project's color, as a dot: the same hue its time has everywhere. */
|
|
68
|
+
export function ProjectDot({ projectId, className }) {
|
|
69
|
+
return (_jsx("span", { "aria-hidden": true, className: cx('tc-dot', !projectId && 'tc-dot-none', className), style: projectId ? { '--tc-hue': projectHue(projectId) } : undefined }));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Picks the project time is recorded on: a button that opens a list to
|
|
73
|
+
* search, grouped by customer.
|
|
74
|
+
*/
|
|
75
|
+
export function ProjectPicker({ projects, value, onChange, required = false, disabled, className, id, label, labels: given, }) {
|
|
76
|
+
const labels = { ...pickerLabels, ...given };
|
|
77
|
+
const listId = useId();
|
|
78
|
+
const root = useRef(null);
|
|
79
|
+
const trigger = useRef(null);
|
|
80
|
+
const [open, setOpen] = useState(false);
|
|
81
|
+
const [query, setQuery] = useState('');
|
|
82
|
+
const [cursor, setCursor] = useState(0);
|
|
83
|
+
const current = value ? projects.find((p) => p.id === value) : undefined;
|
|
84
|
+
const words = query.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
85
|
+
const options = [
|
|
86
|
+
...(required ? [] : [{ id: '', name: labels.none, group: '' }]),
|
|
87
|
+
...projects.map((p) => ({ id: p.id, name: p.name, group: p.customerName })),
|
|
88
|
+
].filter((o) => words.every((w) => `${o.group} ${o.name}`.toLowerCase().includes(w)));
|
|
89
|
+
const at = Math.min(cursor, Math.max(options.length - 1, 0));
|
|
90
|
+
// A press outside closes the list.
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!open)
|
|
93
|
+
return;
|
|
94
|
+
const close = (e) => {
|
|
95
|
+
if (!root.current?.contains(e.target))
|
|
96
|
+
setOpen(false);
|
|
97
|
+
};
|
|
98
|
+
document.addEventListener('pointerdown', close);
|
|
99
|
+
return () => document.removeEventListener('pointerdown', close);
|
|
100
|
+
}, [open]);
|
|
101
|
+
const show = () => {
|
|
102
|
+
setQuery('');
|
|
103
|
+
setCursor(Math.max(0, options.findIndex((o) => o.id === value)));
|
|
104
|
+
setOpen(true);
|
|
105
|
+
};
|
|
106
|
+
const pick = (projectId) => {
|
|
107
|
+
setOpen(false);
|
|
108
|
+
trigger.current?.focus();
|
|
109
|
+
if (projectId !== value)
|
|
110
|
+
onChange(projectId);
|
|
111
|
+
};
|
|
112
|
+
const onKeyDown = (e) => {
|
|
113
|
+
switch (e.key) {
|
|
114
|
+
case 'ArrowDown':
|
|
115
|
+
e.preventDefault();
|
|
116
|
+
setCursor(Math.min(at + 1, options.length - 1));
|
|
117
|
+
break;
|
|
118
|
+
case 'ArrowUp':
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
setCursor(Math.max(at - 1, 0));
|
|
121
|
+
break;
|
|
122
|
+
case 'Enter': {
|
|
123
|
+
e.preventDefault();
|
|
124
|
+
const o = options[at];
|
|
125
|
+
if (o)
|
|
126
|
+
pick(o.id);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case 'Escape':
|
|
130
|
+
// The list closes; a dialog around it stays.
|
|
131
|
+
e.stopPropagation();
|
|
132
|
+
e.nativeEvent.stopImmediatePropagation();
|
|
133
|
+
setOpen(false);
|
|
134
|
+
trigger.current?.focus();
|
|
135
|
+
break;
|
|
136
|
+
case 'Tab':
|
|
137
|
+
setOpen(false);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const shown = current ? projectLabel(current) : required ? labels.choose : labels.none;
|
|
142
|
+
return (_jsxs("div", { ref: root, className: cx('tc-picker', className), children: [_jsxs("button", { ref: trigger, id: id, type: "button", className: "tc-picker-button", "aria-haspopup": "listbox", "aria-expanded": open, "aria-label": `${label ?? labels.project}: ${shown}`, disabled: disabled, onClick: () => (open ? setOpen(false) : show()), onKeyDown: (e) => {
|
|
143
|
+
if (!open && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
144
|
+
e.preventDefault();
|
|
145
|
+
show();
|
|
146
|
+
}
|
|
147
|
+
}, children: [_jsx(ProjectDot, { projectId: current?.id }), _jsx("span", { className: cx('tc-picker-value', !current && 'tc-muted'), children: shown }), _jsx("svg", { "aria-hidden": true, className: "tc-chevron", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.75", children: _jsx("path", { d: "M2.5 4.5 6 8l3.5-3.5", strokeLinecap: "round", strokeLinejoin: "round" }) })] }), open && (_jsxs("div", { className: "tc-popover", children: [_jsx("input", { className: "tc-search", role: "combobox", "aria-expanded": true, "aria-controls": listId, "aria-activedescendant": options[at] ? `${listId}-${at}` : undefined, "aria-label": labels.search, placeholder: labels.search, value: query,
|
|
148
|
+
// biome-ignore lint/a11y/noAutofocus: opening the list is asking to search it
|
|
149
|
+
autoFocus: true, onChange: (e) => {
|
|
150
|
+
setQuery(e.target.value);
|
|
151
|
+
setCursor(0);
|
|
152
|
+
}, onKeyDown: onKeyDown }), _jsxs("div", { id: listId, role: "listbox", "aria-label": label ?? labels.project, className: "tc-options", children: [options.length === 0 && _jsx("p", { className: "tc-empty", children: labels.noMatch }), options.map((o, i) => (_jsxs("div", { children: [o.group !== (options[i - 1]?.group ?? '') && _jsx("div", { className: "tc-group", children: o.group }), _jsxs("div", { id: `${listId}-${i}`, role: "option", tabIndex: -1, "aria-selected": o.id === value, className: cx('tc-option', i === at && 'tc-option-active'), onPointerMove: () => setCursor(i), onClick: () => pick(o.id), children: [_jsx(ProjectDot, { projectId: o.id }), _jsx("span", { className: "tc-option-name", children: o.name }), o.id === value && (_jsx("svg", { "aria-hidden": true, className: "tc-check", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "2", children: _jsx("path", { d: "m2.5 6.5 2.5 2.5 4.5-5.5", strokeLinecap: "round", strokeLinejoin: "round" }) }))] })] }, o.id)))] })] }))] }));
|
|
153
|
+
}
|
|
154
|
+
const barLabels = {
|
|
155
|
+
...pickerLabels,
|
|
156
|
+
clock: 'Clock',
|
|
157
|
+
notePlaceholder: 'What are you working on?',
|
|
158
|
+
noteLabel: 'What you are working on',
|
|
159
|
+
start: 'Start the clock',
|
|
160
|
+
stop: 'Stop the clock',
|
|
161
|
+
switchProject: 'Project the clock is running on',
|
|
162
|
+
locked: 'This pay period’s timesheet is submitted, so the clock is off.',
|
|
163
|
+
since: (time) => `Running since ${time}`,
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* The clock as a bar: say what you are working on, pick its project, and
|
|
167
|
+
* start. While it runs the same bar changes it in place: the note is the
|
|
168
|
+
* running stretch's own, and another project moves the clock to it without
|
|
169
|
+
* stopping. It draws nothing until Timeclock has answered, and nothing for
|
|
170
|
+
* someone Timeclock doesn't know.
|
|
171
|
+
*/
|
|
172
|
+
export function ClockBar({ basePath, store, labels: given, className, onError, onSwitched, onChanged, }) {
|
|
173
|
+
const labels = { ...barLabels, ...given };
|
|
174
|
+
const clock = useClock({ basePath, store });
|
|
175
|
+
const { running } = clock;
|
|
176
|
+
const now = useSecond(!!running);
|
|
177
|
+
const bar = useRef(null);
|
|
178
|
+
// Whether a press that began inside the bar is under way.
|
|
179
|
+
const pressed = useRef(false);
|
|
180
|
+
if (clock.status !== 'ready')
|
|
181
|
+
return null;
|
|
182
|
+
const failed = (action) => (err) => onError?.(err, action);
|
|
183
|
+
const choose = (projectId) => {
|
|
184
|
+
clock
|
|
185
|
+
.chooseProject(projectId)
|
|
186
|
+
.then((next) => {
|
|
187
|
+
if (next) {
|
|
188
|
+
onSwitched?.(next);
|
|
189
|
+
onChanged?.();
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
.catch(failed('switch'));
|
|
193
|
+
};
|
|
194
|
+
const leaveNote = (e) => {
|
|
195
|
+
// Moving on to the project picker isn't done with the note yet. Safari
|
|
196
|
+
// doesn't focus a button that is clicked, so the press is noted as well.
|
|
197
|
+
if (pressed.current || bar.current?.contains(e.relatedTarget))
|
|
198
|
+
return;
|
|
199
|
+
clock.saveNote().then(onChanged, failed('note'));
|
|
200
|
+
};
|
|
201
|
+
const since = running
|
|
202
|
+
? labels.since(new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', timeZone: clock.timeZone }).format(new Date(running.startedAt)))
|
|
203
|
+
: undefined;
|
|
204
|
+
return (_jsxs("form", { ref: bar, "aria-label": labels.clock, className: cx('tc-bar', className), onSubmit: (e) => {
|
|
205
|
+
e.preventDefault();
|
|
206
|
+
if (running)
|
|
207
|
+
clock.stop().then(onChanged, failed('stop'));
|
|
208
|
+
else
|
|
209
|
+
clock.start().then(onChanged, failed('start'));
|
|
210
|
+
}, onPointerDownCapture: () => {
|
|
211
|
+
pressed.current = true;
|
|
212
|
+
}, onPointerUpCapture: () => {
|
|
213
|
+
pressed.current = false;
|
|
214
|
+
}, onPointerCancelCapture: () => {
|
|
215
|
+
pressed.current = false;
|
|
216
|
+
}, children: [_jsx("input", { className: "tc-note", value: clock.note, placeholder: clock.locked ? labels.locked : labels.notePlaceholder, "aria-label": labels.noteLabel, disabled: clock.locked, maxLength: 2000, onChange: (e) => clock.setNote(e.target.value), onBlur: running ? leaveNote : undefined, onKeyDown: (e) => {
|
|
217
|
+
// While the clock runs, Enter is done with the note; it doesn't stop the clock.
|
|
218
|
+
if (running && e.key === 'Enter') {
|
|
219
|
+
e.preventDefault();
|
|
220
|
+
e.currentTarget.blur();
|
|
221
|
+
}
|
|
222
|
+
} }), _jsx(ProjectPicker, { className: "tc-bar-picker", projects: clock.projects, value: clock.projectId, onChange: choose, required: clock.requireProject, disabled: clock.locked || (!!running && clock.busy), label: running ? labels.switchProject : undefined, labels: labels }), running ? (_jsx("span", { className: "tc-time", role: "timer", title: since, children: stopwatch(now - Date.parse(running.startedAt)) })) : (_jsx("span", { "aria-hidden": true, className: "tc-time tc-time-idle", children: stopwatch(0) })), _jsx("button", { type: "submit", "aria-label": running ? labels.stop : labels.start, disabled: clock.locked || clock.busy, className: cx('tc-go', !!running && 'tc-go-stop'), children: _jsx("svg", { "aria-hidden": true, viewBox: "0 0 16 16", fill: "currentColor", children: running ? _jsx("rect", { x: "4", y: "4", width: "8", height: "8", rx: "1" }) : _jsx("path", { d: "M5.5 3.5v9l7-4.5z" }) }) })] }));
|
|
223
|
+
}
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/* The look of @giraffesyo/timeclock's ClockBar and ProjectPicker. Colors come
|
|
2
|
+
* from the --tc-* properties, which default to @parallelworks/ui's theme
|
|
3
|
+
* contract (--theme-*) and, without it, to plain light values. Set --tc-*
|
|
4
|
+
* on any ancestor to fit another look. */
|
|
5
|
+
.tc-bar,
|
|
6
|
+
.tc-picker {
|
|
7
|
+
--tc-bg: var(--theme-panel-bg, #fff);
|
|
8
|
+
--tc-fg: var(--theme-panel, #16181d);
|
|
9
|
+
--tc-muted: var(--theme-muted-text-color, #5d6370);
|
|
10
|
+
--tc-border: var(--theme-border, #d9dce3);
|
|
11
|
+
--tc-hover: var(--theme-hover, #eef0f4);
|
|
12
|
+
--tc-accent: var(--theme-accent, #474fd4);
|
|
13
|
+
--tc-accent-fg: var(--theme-accent-text, #fff);
|
|
14
|
+
--tc-stop: var(--danger, #d92d20);
|
|
15
|
+
--tc-radius: 0.5rem;
|
|
16
|
+
box-sizing: border-box;
|
|
17
|
+
color: var(--tc-fg);
|
|
18
|
+
font-variant-numeric: tabular-nums;
|
|
19
|
+
}
|
|
20
|
+
.tc-bar *,
|
|
21
|
+
.tc-picker * {
|
|
22
|
+
box-sizing: border-box;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.tc-bar {
|
|
26
|
+
display: flex;
|
|
27
|
+
flex-wrap: wrap;
|
|
28
|
+
align-items: center;
|
|
29
|
+
gap: 0.375rem 0.75rem;
|
|
30
|
+
}
|
|
31
|
+
.tc-note {
|
|
32
|
+
flex: 1 1 100%;
|
|
33
|
+
min-width: 0;
|
|
34
|
+
height: 2.5rem;
|
|
35
|
+
padding: 0 0.75rem;
|
|
36
|
+
border: 1px solid transparent;
|
|
37
|
+
border-radius: var(--tc-radius);
|
|
38
|
+
background: transparent;
|
|
39
|
+
color: inherit;
|
|
40
|
+
font: inherit;
|
|
41
|
+
font-size: 1rem;
|
|
42
|
+
}
|
|
43
|
+
.tc-note::placeholder {
|
|
44
|
+
color: var(--tc-muted);
|
|
45
|
+
}
|
|
46
|
+
.tc-note:hover,
|
|
47
|
+
.tc-note:focus-visible {
|
|
48
|
+
border-color: var(--tc-border);
|
|
49
|
+
}
|
|
50
|
+
.tc-bar-picker {
|
|
51
|
+
flex: 1 1 0;
|
|
52
|
+
min-width: 0;
|
|
53
|
+
height: 2.5rem;
|
|
54
|
+
}
|
|
55
|
+
.tc-time {
|
|
56
|
+
width: 6rem;
|
|
57
|
+
text-align: right;
|
|
58
|
+
font-size: 1.25rem;
|
|
59
|
+
font-weight: 600;
|
|
60
|
+
letter-spacing: -0.02em;
|
|
61
|
+
}
|
|
62
|
+
.tc-time-idle {
|
|
63
|
+
display: none;
|
|
64
|
+
color: color-mix(in oklab, var(--tc-muted) 60%, transparent);
|
|
65
|
+
}
|
|
66
|
+
.tc-go {
|
|
67
|
+
display: flex;
|
|
68
|
+
flex-shrink: 0;
|
|
69
|
+
align-items: center;
|
|
70
|
+
justify-content: center;
|
|
71
|
+
width: 2.5rem;
|
|
72
|
+
height: 2.5rem;
|
|
73
|
+
border: 0;
|
|
74
|
+
border-radius: 9999px;
|
|
75
|
+
background: var(--tc-accent);
|
|
76
|
+
color: var(--tc-accent-fg);
|
|
77
|
+
cursor: pointer;
|
|
78
|
+
transition:
|
|
79
|
+
scale 120ms ease-out,
|
|
80
|
+
opacity 120ms ease-out;
|
|
81
|
+
}
|
|
82
|
+
.tc-go svg {
|
|
83
|
+
width: 1.25rem;
|
|
84
|
+
height: 1.25rem;
|
|
85
|
+
}
|
|
86
|
+
.tc-go-stop {
|
|
87
|
+
background: var(--tc-stop);
|
|
88
|
+
color: var(--tc-bg);
|
|
89
|
+
}
|
|
90
|
+
.tc-go:hover:enabled {
|
|
91
|
+
opacity: 0.9;
|
|
92
|
+
}
|
|
93
|
+
.tc-go:active:enabled {
|
|
94
|
+
scale: 0.95;
|
|
95
|
+
}
|
|
96
|
+
.tc-go:disabled {
|
|
97
|
+
cursor: not-allowed;
|
|
98
|
+
opacity: 0.5;
|
|
99
|
+
}
|
|
100
|
+
@media (min-width: 40rem) {
|
|
101
|
+
.tc-note {
|
|
102
|
+
flex-basis: 12rem;
|
|
103
|
+
}
|
|
104
|
+
.tc-bar-picker {
|
|
105
|
+
flex: 0 0 15rem;
|
|
106
|
+
}
|
|
107
|
+
.tc-time-idle {
|
|
108
|
+
display: block;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.tc-picker {
|
|
113
|
+
position: relative;
|
|
114
|
+
min-height: 2.25rem;
|
|
115
|
+
}
|
|
116
|
+
.tc-picker-button {
|
|
117
|
+
display: flex;
|
|
118
|
+
align-items: center;
|
|
119
|
+
gap: 0.5rem;
|
|
120
|
+
width: 100%;
|
|
121
|
+
height: 100%;
|
|
122
|
+
min-height: 2.25rem;
|
|
123
|
+
padding: 0 0.75rem;
|
|
124
|
+
border: 1px solid var(--tc-border);
|
|
125
|
+
border-radius: var(--tc-radius);
|
|
126
|
+
background: var(--tc-bg);
|
|
127
|
+
color: inherit;
|
|
128
|
+
font: inherit;
|
|
129
|
+
font-size: 0.875rem;
|
|
130
|
+
text-align: left;
|
|
131
|
+
cursor: pointer;
|
|
132
|
+
}
|
|
133
|
+
.tc-picker-button:disabled {
|
|
134
|
+
cursor: not-allowed;
|
|
135
|
+
opacity: 0.6;
|
|
136
|
+
}
|
|
137
|
+
.tc-picker-value {
|
|
138
|
+
flex: 1;
|
|
139
|
+
min-width: 0;
|
|
140
|
+
overflow: hidden;
|
|
141
|
+
text-overflow: ellipsis;
|
|
142
|
+
white-space: nowrap;
|
|
143
|
+
}
|
|
144
|
+
.tc-muted {
|
|
145
|
+
color: var(--tc-muted);
|
|
146
|
+
}
|
|
147
|
+
.tc-chevron,
|
|
148
|
+
.tc-check {
|
|
149
|
+
flex-shrink: 0;
|
|
150
|
+
width: 0.875rem;
|
|
151
|
+
height: 0.875rem;
|
|
152
|
+
color: var(--tc-muted);
|
|
153
|
+
}
|
|
154
|
+
.tc-check {
|
|
155
|
+
color: var(--tc-accent);
|
|
156
|
+
}
|
|
157
|
+
.tc-popover {
|
|
158
|
+
position: absolute;
|
|
159
|
+
top: 100%;
|
|
160
|
+
left: 0;
|
|
161
|
+
z-index: 40;
|
|
162
|
+
width: 18rem;
|
|
163
|
+
max-width: calc(100vw - 2rem);
|
|
164
|
+
margin-top: 0.25rem;
|
|
165
|
+
border: 1px solid var(--tc-border);
|
|
166
|
+
border-radius: 0.625rem;
|
|
167
|
+
background: var(--tc-bg);
|
|
168
|
+
box-shadow:
|
|
169
|
+
0 10px 28px -10px rgb(0 0 0 / 0.3),
|
|
170
|
+
0 2px 6px -2px rgb(0 0 0 / 0.12);
|
|
171
|
+
}
|
|
172
|
+
.tc-search {
|
|
173
|
+
width: 100%;
|
|
174
|
+
padding: 0.5rem 0.75rem;
|
|
175
|
+
border: 0;
|
|
176
|
+
border-bottom: 1px solid var(--tc-border);
|
|
177
|
+
border-radius: 0.625rem 0.625rem 0 0;
|
|
178
|
+
background: transparent;
|
|
179
|
+
color: inherit;
|
|
180
|
+
font: inherit;
|
|
181
|
+
font-size: 0.875rem;
|
|
182
|
+
outline: none;
|
|
183
|
+
}
|
|
184
|
+
.tc-search::placeholder {
|
|
185
|
+
color: var(--tc-muted);
|
|
186
|
+
}
|
|
187
|
+
.tc-options {
|
|
188
|
+
max-height: 16rem;
|
|
189
|
+
overflow-y: auto;
|
|
190
|
+
padding: 0.25rem;
|
|
191
|
+
}
|
|
192
|
+
.tc-group {
|
|
193
|
+
padding: 0.5rem 0.5rem 0.25rem;
|
|
194
|
+
font-size: 0.75rem;
|
|
195
|
+
font-weight: 500;
|
|
196
|
+
color: var(--tc-muted);
|
|
197
|
+
}
|
|
198
|
+
.tc-option {
|
|
199
|
+
display: flex;
|
|
200
|
+
align-items: center;
|
|
201
|
+
gap: 0.5rem;
|
|
202
|
+
padding: 0.375rem 0.5rem;
|
|
203
|
+
border-radius: 0.375rem;
|
|
204
|
+
font-size: 0.875rem;
|
|
205
|
+
cursor: pointer;
|
|
206
|
+
}
|
|
207
|
+
.tc-option-active {
|
|
208
|
+
background: var(--tc-hover);
|
|
209
|
+
}
|
|
210
|
+
.tc-option-name {
|
|
211
|
+
flex: 1;
|
|
212
|
+
min-width: 0;
|
|
213
|
+
overflow: hidden;
|
|
214
|
+
text-overflow: ellipsis;
|
|
215
|
+
white-space: nowrap;
|
|
216
|
+
}
|
|
217
|
+
.tc-empty {
|
|
218
|
+
margin: 0;
|
|
219
|
+
padding: 0.75rem 0.5rem;
|
|
220
|
+
text-align: center;
|
|
221
|
+
font-size: 0.875rem;
|
|
222
|
+
color: var(--tc-muted);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.tc-dot {
|
|
226
|
+
display: inline-block;
|
|
227
|
+
flex-shrink: 0;
|
|
228
|
+
width: 0.5rem;
|
|
229
|
+
height: 0.5rem;
|
|
230
|
+
border-radius: 9999px;
|
|
231
|
+
background: oklch(0.64 0.15 var(--tc-hue));
|
|
232
|
+
}
|
|
233
|
+
.tc-dot-none {
|
|
234
|
+
background: transparent;
|
|
235
|
+
box-shadow: inset 0 0 0 1.5px var(--tc-muted);
|
|
236
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@giraffesyo/timeclock",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "The Timeclock clock for a host application: a headless store and React hook, and a ready-made bar.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/giraffesyo/timeclock.git",
|
|
9
|
+
"directory": "web/packages/timeclock"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": [
|
|
13
|
+
"*.css"
|
|
14
|
+
],
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./react": {
|
|
24
|
+
"types": "./dist/react.d.ts",
|
|
25
|
+
"default": "./dist/react.js"
|
|
26
|
+
},
|
|
27
|
+
"./styles.css": "./dist/styles.css"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "rm -rf dist && tsc6 -p tsconfig.build.json && cp src/styles.css dist/styles.css",
|
|
31
|
+
"typecheck": "tsc6 -p tsconfig.json"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": ">=18"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"react": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/react": "^19.3.0",
|
|
43
|
+
"react": "^19.3.0",
|
|
44
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
}
|
|
50
|
+
}
|