@clicktap/state 0.1.1 → 0.1.3

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.
@@ -1,222 +0,0 @@
1
- import { createMachine, sendTo } from 'xstate';
2
- import { assign } from '@xstate/immer';
3
- import { cloneElement, ReactNode } from 'react';
4
- import crypto from 'crypto';
5
- import type { ReactElement } from 'react';
6
- import { timerMachine } from './timer';
7
-
8
- // circular reference, don't want library depending on UI component specific to one app
9
- // import { Notification } from "@waytrade/components/app/Toast/Notification";
10
-
11
- /** @todo figure out TS here */
12
- export type ToastItem = {
13
- id: string | null;
14
- duration: number;
15
- element: ReactNode;
16
- };
17
-
18
- export interface ToastMachineContext {
19
- /** @todo create proper type for Array items */
20
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
- items: Array<ToastItem>;
22
- order: 'asc' | 'desc';
23
- duration: number;
24
- activeItem: string | null;
25
- }
26
-
27
- export interface AddItemEvent {
28
- type: 'ADD_ITEM';
29
- item: ReactElement;
30
- duration?: number;
31
- }
32
-
33
- export interface RemoveItemEvent {
34
- type: 'REMOVE_ITEM';
35
- }
36
-
37
- export interface SetIdleEvent {
38
- type: 'SET_IDLE';
39
- }
40
-
41
- export interface SetActiveEvent {
42
- type: 'SET_ACTIVE';
43
- }
44
-
45
- export interface ResumeTimerEvent {
46
- type: 'RESUME_TIMER';
47
- }
48
-
49
- export interface PauseTimerEvent {
50
- type: 'PAUSE_TIMER';
51
- }
52
-
53
- export interface TimeoutEvent {
54
- type: 'xstate.after(ITEM_TIMEOUT)#toast.active';
55
- }
56
-
57
- export interface TimerDoneEvent {
58
- type: 'done.invoke.timer';
59
- }
60
-
61
- export type ToastMachineEvents =
62
- | AddItemEvent
63
- | RemoveItemEvent
64
- | SetIdleEvent
65
- | SetActiveEvent
66
- | TimeoutEvent
67
- | ResumeTimerEvent
68
- | PauseTimerEvent
69
- | TimerDoneEvent;
70
-
71
- export const toastMachine =
72
- /** @xstate-layout N4IgpgJg5mDOIC5QBcD2BDWyB0BLCANmAMQCCAIuQPoCSAKgKICyA2gAwC6ioADqrLmS5UAO24gAHogCMAZgCs2AGzS2SgBxKA7AE4lO2ToAsAGhABPRAFoj6gL52zaTDnQBjIQDcSEUWDwinqgA1v5CALZgAE7sXEggfAJCouJSCEpsOspaOQay0kqyAExKZpYIReps2LY6mmxG+kUKWkYOThhY2O5eJBTU9Myx4omCwmLxabKF2NKN8kpFbPls8gpl1vLqykXyasaNhdJFWu0gzl09uN7EAErMAPIAagy0jKycI-xjKZOIi9sDPI9EZgYV1LItBsEIYlNg9NIdGxKki1jkzhdXB5riQAMoMOi0cgAGQYw3io2SE1AaTkWS0RQOSKWEK0c2hzWkNUWJy0Skah1sGM6WN6dwYuIAqkxXnQaDLbuTeN8qakZPIuXylhktAppgUitDilpsJpQeo9KsVOoCsKXN1sTcAAqkSX4qhyhVKhIq8ZqirSbaZeTyLRbVnqVqyaHSaQm45mtirKo6Eo6O2XCAQXAiKDEfGEmgksmfCm+340xC2bCh3XGrTLfR8+TQqxFJbYTIoow6HStBshjOuLM5vMFqikADCcpe3spfr+MKMmsZbDmFvU6jBRtW8I3IcMBT7Q+wUTA4VQnlH+YJRNJc-L1MkiHURiKps0DIWKhyIZjehqfc1h0I8GyUE8zwvK9cxvQkpxnEs4mVJIF0rBBU1kWZVw1WtgSMUwLHVIxsEqIxjVDIF5CFM4RFQCA4HETEvhQitnwQKxYxNOoDFjbQ2RtfRW1kfJO1THjA1sVRZBPfAiGYn4nzSKx9D3cS+MDI9-zhWot2A0C1BPK5vHk1VF1kIwuOZPICmKYEtMAuoDxAptlkMkdcxM1C2MMYjKhKXUlH5DUimkVsijImsxMMcLpjDUMIPPS9R081i0i3OE10afI42XcKW0ImFgRrYE5FBWx1F2cyHAcIA */
73
- createMachine<ToastMachineContext, ToastMachineEvents>(
74
- {
75
- context: { items: [], order: 'desc', duration: 0, activeItem: null },
76
- predictableActionArguments: true,
77
- id: 'toast',
78
- initial: 'idle',
79
- states: {
80
- idle: {
81
- on: {
82
- ADD_ITEM: {
83
- target: 'adding',
84
- },
85
- },
86
- },
87
- active: {
88
- invoke: {
89
- id: 'timer',
90
- src: timerMachine,
91
- onDone: {
92
- target: 'removing',
93
- cond: 'itemHasTimeout',
94
- },
95
- data: Object.assign(timerMachine.context, {
96
- duration: (context: ToastMachineContext) =>
97
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
98
- context.items[context.items.length - 1].duration ??
99
- context.duration,
100
- }),
101
- },
102
- on: {
103
- ADD_ITEM: {
104
- // actions: sendTo('timer', 'PAUSE_TIMER'),
105
- target: 'adding',
106
- },
107
- REMOVE_ITEM: {
108
- target: 'removing',
109
- },
110
- SET_IDLE: {
111
- target: 'idle',
112
- },
113
- RESUME_TIMER: {
114
- actions: sendTo('timer', 'RESUME_TIMER'),
115
- // cond: (context) => context.duration > 0,
116
- },
117
- PAUSE_TIMER: {
118
- actions: sendTo('timer', 'PAUSE_TIMER'),
119
- // cond: (context) => context.duration > 0,
120
- },
121
- },
122
- // after: {
123
- // ITEM_TIMEOUT: {
124
- // target: 'removing',
125
- // cond: 'itemHasTimeout',
126
- // },
127
- // },
128
- },
129
- adding: {
130
- entry: ['addItem'],
131
- on: {
132
- SET_IDLE: {
133
- target: 'idle',
134
- },
135
- SET_ACTIVE: {
136
- target: 'active',
137
- },
138
- },
139
- },
140
- removing: {
141
- entry: 'removeItem',
142
- on: {
143
- SET_IDLE: {
144
- target: 'idle',
145
- },
146
- SET_ACTIVE: {
147
- target: 'active',
148
- },
149
- },
150
- },
151
- },
152
- },
153
- {
154
- actions: {
155
- addItem: assign((context, event) => {
156
- if (event.type !== 'ADD_ITEM') return;
157
-
158
- const id = `notification-${crypto.randomBytes(16).toString('hex')}`;
159
- const duration = event.duration ?? context.duration;
160
-
161
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
162
- const notification = cloneElement(event.item, {
163
- duration,
164
- id,
165
- key: event.item.key ?? id,
166
- ...event.item.props,
167
- });
168
- const item = {
169
- duration,
170
- id,
171
- element: notification,
172
- };
173
-
174
- if (context.order === 'desc') {
175
- context.items.push(item);
176
- } else {
177
- context.items.unshift(item);
178
- }
179
- context.activeItem = id;
180
- }),
181
- removeItem: assign((context, event) => {
182
- if (
183
- event.type !== 'REMOVE_ITEM' &&
184
- event.type !== 'xstate.after(ITEM_TIMEOUT)#toast.active' &&
185
- event.type !== 'done.invoke.timer'
186
- )
187
- return;
188
-
189
- if (context.order === 'desc') {
190
- context.items.pop();
191
- context.activeItem =
192
- context.items.length > 0
193
- ? context.items[context.items.length - 1].id
194
- : null;
195
- } else {
196
- context.items.shift();
197
- context.activeItem =
198
- context.items.length > 0 ? context.items[0].id : null;
199
- }
200
- }),
201
- // startTimer: sendTo('timer', { type: 'START' }),
202
- },
203
- delays: {
204
- ITEM_TIMEOUT: (context) => {
205
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
206
- return (
207
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
208
- context.items[context.items.length - 1].duration ?? context.duration
209
- );
210
- },
211
- },
212
- guards: {
213
- itemHasTimeout: (context) => {
214
- return (
215
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
216
- context.items[context.items.length - 1].duration > 0 ||
217
- context.duration > 0
218
- );
219
- },
220
- },
221
- }
222
- );
package/tsconfig.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "module": "commonjs",
5
- "esModuleInterop": true,
6
- "jsx": "preserve",
7
- "forceConsistentCasingInFileNames": true,
8
- "strict": true,
9
- "noImplicitOverride": true,
10
- "noPropertyAccessFromIndexSignature": true,
11
- "noImplicitReturns": true,
12
- "noFallthroughCasesInSwitch": true
13
- },
14
- "files": [],
15
- "include": [],
16
- "references": [
17
- {
18
- "path": "./tsconfig.lib.json"
19
- },
20
- {
21
- "path": "./tsconfig.spec.json"
22
- }
23
- ]
24
- }
package/tsconfig.lib.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../../dist/out-tsc",
5
- "declaration": true,
6
- "types": ["node"]
7
- },
8
- "include": ["src/**/*.ts"],
9
- "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
10
- }
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../../dist/out-tsc",
5
- "module": "commonjs",
6
- "types": ["jest", "node"]
7
- },
8
- "include": [
9
- "jest.config.ts",
10
- "src/**/*.test.ts",
11
- "src/**/*.spec.ts",
12
- "src/**/*.d.ts"
13
- ]
14
- }
File without changes