react-achievements 3.0.2 → 3.2.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/README.md +338 -225
- package/dist/index.d.ts +205 -75
- package/dist/index.js +458 -115
- package/dist/index.js.map +1 -1
- package/dist/types/core/icons/defaultIcons.d.ts +0 -73
- package/dist/types/core/types.d.ts +14 -0
- package/dist/types/core/utils/configNormalizer.d.ts +3 -0
- package/dist/types/hooks/useSimpleAchievements.d.ts +40 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/providers/AchievementProvider.d.ts +2 -2
- package/dist/types/utils/achievementHelpers.d.ts +135 -0
- package/package.json +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,20 @@ interface AchievementCondition {
|
|
|
23
23
|
interface AchievementConfiguration {
|
|
24
24
|
[key: string]: AchievementCondition[];
|
|
25
25
|
}
|
|
26
|
+
interface SimpleAchievementDetails {
|
|
27
|
+
title: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
icon?: string;
|
|
30
|
+
}
|
|
31
|
+
interface CustomAchievementDetails extends SimpleAchievementDetails {
|
|
32
|
+
condition: (metrics: Record<string, any>) => boolean;
|
|
33
|
+
}
|
|
34
|
+
interface SimpleAchievementConfig {
|
|
35
|
+
[metric: string]: {
|
|
36
|
+
[threshold: string]: SimpleAchievementDetails | CustomAchievementDetails;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
type AchievementConfigurationType = AchievementConfiguration | SimpleAchievementConfig;
|
|
26
40
|
interface InitialAchievementMetrics {
|
|
27
41
|
[key: string]: AchievementMetricValue;
|
|
28
42
|
}
|
|
@@ -90,6 +104,17 @@ declare class LocalStorage implements AchievementStorage {
|
|
|
90
104
|
clear(): void;
|
|
91
105
|
}
|
|
92
106
|
|
|
107
|
+
declare class MemoryStorage implements AchievementStorage {
|
|
108
|
+
private metrics;
|
|
109
|
+
private unlockedAchievements;
|
|
110
|
+
constructor();
|
|
111
|
+
getMetrics(): AchievementMetrics;
|
|
112
|
+
setMetrics(metrics: AchievementMetrics): void;
|
|
113
|
+
getUnlockedAchievements(): string[];
|
|
114
|
+
setUnlockedAchievements(achievements: string[]): void;
|
|
115
|
+
clear(): void;
|
|
116
|
+
}
|
|
117
|
+
|
|
93
118
|
interface BadgesButtonProps {
|
|
94
119
|
onClick: () => void;
|
|
95
120
|
position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
|
@@ -126,7 +151,7 @@ interface AchievementContextType {
|
|
|
126
151
|
}
|
|
127
152
|
declare const AchievementContext: React$1.Context<AchievementContextType | undefined>;
|
|
128
153
|
interface AchievementProviderProps {
|
|
129
|
-
achievements:
|
|
154
|
+
achievements: AchievementConfigurationType;
|
|
130
155
|
storage?: AchievementStorage | StorageType;
|
|
131
156
|
children: React$1.ReactNode;
|
|
132
157
|
icons?: Record<string, string>;
|
|
@@ -135,88 +160,193 @@ declare const AchievementProvider: React$1.FC<AchievementProviderProps>;
|
|
|
135
160
|
|
|
136
161
|
declare const useAchievements: () => AchievementContextType;
|
|
137
162
|
|
|
163
|
+
/**
|
|
164
|
+
* A simplified hook for achievement tracking.
|
|
165
|
+
* Provides an easier API for common use cases while maintaining access to advanced features.
|
|
166
|
+
*/
|
|
167
|
+
declare const useSimpleAchievements: () => {
|
|
168
|
+
/**
|
|
169
|
+
* Track a metric value for achievements
|
|
170
|
+
* @param metric - The metric name (e.g., 'score', 'level')
|
|
171
|
+
* @param value - The metric value
|
|
172
|
+
*/
|
|
173
|
+
track: (metric: string, value: any) => void;
|
|
174
|
+
/**
|
|
175
|
+
* Track multiple metrics at once
|
|
176
|
+
* @param metrics - Object with metric names as keys and values
|
|
177
|
+
*/
|
|
178
|
+
trackMultiple: (metrics: Record<string, any>) => void;
|
|
179
|
+
/**
|
|
180
|
+
* Array of unlocked achievement IDs
|
|
181
|
+
*/
|
|
182
|
+
unlocked: string[];
|
|
183
|
+
/**
|
|
184
|
+
* All available achievements
|
|
185
|
+
*/
|
|
186
|
+
all: Record<string, any>;
|
|
187
|
+
/**
|
|
188
|
+
* Number of unlocked achievements
|
|
189
|
+
*/
|
|
190
|
+
unlockedCount: number;
|
|
191
|
+
/**
|
|
192
|
+
* Reset all achievement progress
|
|
193
|
+
*/
|
|
194
|
+
reset: () => void;
|
|
195
|
+
/**
|
|
196
|
+
* Get current state (advanced usage)
|
|
197
|
+
*/
|
|
198
|
+
getState: () => {
|
|
199
|
+
metrics: AchievementMetrics;
|
|
200
|
+
unlocked: string[];
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
|
|
138
204
|
declare const defaultStyles: Required<StylesProps>;
|
|
139
205
|
|
|
140
206
|
declare const defaultAchievementIcons: {
|
|
141
|
-
levelUp: string;
|
|
142
|
-
questComplete: string;
|
|
143
|
-
monsterDefeated: string;
|
|
144
|
-
itemCollected: string;
|
|
145
|
-
challengeCompleted: string;
|
|
146
|
-
milestoneReached: string;
|
|
147
|
-
firstStep: string;
|
|
148
|
-
newBeginnings: string;
|
|
149
|
-
breakthrough: string;
|
|
150
|
-
growth: string;
|
|
151
|
-
shared: string;
|
|
152
|
-
liked: string;
|
|
153
|
-
commented: string;
|
|
154
|
-
followed: string;
|
|
155
|
-
invited: string;
|
|
156
|
-
communityMember: string;
|
|
157
|
-
supporter: string;
|
|
158
|
-
connected: string;
|
|
159
|
-
participant: string;
|
|
160
|
-
influencer: string;
|
|
161
|
-
activeDay: string;
|
|
162
|
-
activeWeek: string;
|
|
163
|
-
activeMonth: string;
|
|
164
|
-
earlyBird: string;
|
|
165
|
-
nightOwl: string;
|
|
166
|
-
streak: string;
|
|
167
|
-
dedicated: string;
|
|
168
|
-
punctual: string;
|
|
169
|
-
consistent: string;
|
|
170
|
-
marathon: string;
|
|
171
|
-
artist: string;
|
|
172
|
-
writer: string;
|
|
173
|
-
innovator: string;
|
|
174
|
-
creator: string;
|
|
175
|
-
expert: string;
|
|
176
|
-
master: string;
|
|
177
|
-
pioneer: string;
|
|
178
|
-
performer: string;
|
|
179
|
-
thinker: string;
|
|
180
|
-
explorer: string;
|
|
181
|
-
bronze: string;
|
|
182
|
-
silver: string;
|
|
183
|
-
gold: string;
|
|
184
|
-
diamond: string;
|
|
185
|
-
legendary: string;
|
|
186
|
-
epic: string;
|
|
187
|
-
rare: string;
|
|
188
|
-
common: string;
|
|
189
|
-
special: string;
|
|
190
|
-
hidden: string;
|
|
191
|
-
one: string;
|
|
192
|
-
ten: string;
|
|
193
|
-
hundred: string;
|
|
194
|
-
thousand: string;
|
|
195
|
-
clicked: string;
|
|
196
|
-
used: string;
|
|
197
|
-
found: string;
|
|
198
|
-
built: string;
|
|
199
|
-
solved: string;
|
|
200
|
-
discovered: string;
|
|
201
|
-
unlocked: string;
|
|
202
|
-
upgraded: string;
|
|
203
|
-
repaired: string;
|
|
204
|
-
defended: string;
|
|
205
207
|
default: string;
|
|
206
208
|
loading: string;
|
|
207
209
|
error: string;
|
|
208
210
|
success: string;
|
|
209
|
-
failure: string;
|
|
210
211
|
trophy: string;
|
|
211
212
|
star: string;
|
|
212
|
-
flag: string;
|
|
213
|
-
puzzle: string;
|
|
214
|
-
gem: string;
|
|
215
|
-
crown: string;
|
|
216
|
-
medal: string;
|
|
217
|
-
ribbon: string;
|
|
218
|
-
badge: string;
|
|
219
|
-
shield: string;
|
|
220
213
|
};
|
|
221
214
|
|
|
222
|
-
|
|
215
|
+
declare function isSimpleConfig(config: AchievementConfigurationType): config is SimpleAchievementConfig;
|
|
216
|
+
declare function normalizeAchievements(config: AchievementConfigurationType): AchievementConfiguration;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Helper interface for cleaner achievement award definitions
|
|
220
|
+
*/
|
|
221
|
+
interface AwardDetails {
|
|
222
|
+
title?: string;
|
|
223
|
+
description?: string;
|
|
224
|
+
icon?: string;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Base class for chainable achievement configuration (Tier 2)
|
|
228
|
+
*/
|
|
229
|
+
declare abstract class Achievement {
|
|
230
|
+
protected metric: string;
|
|
231
|
+
protected award: AwardDetails;
|
|
232
|
+
constructor(metric: string, defaultAward: AwardDetails);
|
|
233
|
+
/**
|
|
234
|
+
* Customize the award details for this achievement
|
|
235
|
+
* @param award - Custom award details
|
|
236
|
+
* @returns This achievement for chaining
|
|
237
|
+
*/
|
|
238
|
+
withAward(award: AwardDetails): Achievement;
|
|
239
|
+
/**
|
|
240
|
+
* Convert this achievement to a SimpleAchievementConfig
|
|
241
|
+
*/
|
|
242
|
+
abstract toConfig(): SimpleAchievementConfig;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Threshold-based achievement (score, level, etc.)
|
|
246
|
+
*/
|
|
247
|
+
declare class ThresholdAchievement extends Achievement {
|
|
248
|
+
private threshold;
|
|
249
|
+
constructor(metric: string, threshold: number, defaultAward: AwardDetails);
|
|
250
|
+
toConfig(): SimpleAchievementConfig;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Boolean achievement (tutorial completion, first login, etc.)
|
|
254
|
+
*/
|
|
255
|
+
declare class BooleanAchievement extends Achievement {
|
|
256
|
+
toConfig(): SimpleAchievementConfig;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Value-based achievement (character class, difficulty, etc.)
|
|
260
|
+
*/
|
|
261
|
+
declare class ValueAchievement extends Achievement {
|
|
262
|
+
private value;
|
|
263
|
+
constructor(metric: string, value: string, defaultAward: AwardDetails);
|
|
264
|
+
toConfig(): SimpleAchievementConfig;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Complex achievement builder for power users (Tier 3)
|
|
268
|
+
*/
|
|
269
|
+
declare class ComplexAchievementBuilder {
|
|
270
|
+
private id;
|
|
271
|
+
private metric;
|
|
272
|
+
private condition;
|
|
273
|
+
private award;
|
|
274
|
+
/**
|
|
275
|
+
* Set the unique identifier for this achievement
|
|
276
|
+
*/
|
|
277
|
+
withId(id: string): ComplexAchievementBuilder;
|
|
278
|
+
/**
|
|
279
|
+
* Set the metric this achievement tracks
|
|
280
|
+
*/
|
|
281
|
+
withMetric(metric: string): ComplexAchievementBuilder;
|
|
282
|
+
/**
|
|
283
|
+
* Set the condition function that determines if achievement is unlocked
|
|
284
|
+
*/
|
|
285
|
+
withCondition(fn: (value: AchievementMetricValue, state: AchievementState) => boolean): ComplexAchievementBuilder;
|
|
286
|
+
/**
|
|
287
|
+
* Set the award details for this achievement
|
|
288
|
+
*/
|
|
289
|
+
withAward(award: AwardDetails): ComplexAchievementBuilder;
|
|
290
|
+
/**
|
|
291
|
+
* Build the final achievement configuration
|
|
292
|
+
*/
|
|
293
|
+
build(): SimpleAchievementConfig;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Main AchievementBuilder with three-tier API
|
|
297
|
+
* Tier 1: Simple static methods with smart defaults
|
|
298
|
+
* Tier 2: Chainable customization
|
|
299
|
+
* Tier 3: Full builder for complex logic
|
|
300
|
+
*/
|
|
301
|
+
declare class AchievementBuilder {
|
|
302
|
+
/**
|
|
303
|
+
* Create a single score achievement with smart defaults
|
|
304
|
+
* @param threshold - Score threshold to achieve
|
|
305
|
+
* @returns Chainable ThresholdAchievement
|
|
306
|
+
*/
|
|
307
|
+
static createScoreAchievement(threshold: number): ThresholdAchievement;
|
|
308
|
+
/**
|
|
309
|
+
* Create multiple score achievements
|
|
310
|
+
* @param thresholds - Array of thresholds or [threshold, award] tuples
|
|
311
|
+
* @returns Complete SimpleAchievementConfig
|
|
312
|
+
*/
|
|
313
|
+
static createScoreAchievements(thresholds: (number | [number, AwardDetails])[]): SimpleAchievementConfig;
|
|
314
|
+
/**
|
|
315
|
+
* Create a single level achievement with smart defaults
|
|
316
|
+
* @param level - Level threshold to achieve
|
|
317
|
+
* @returns Chainable ThresholdAchievement
|
|
318
|
+
*/
|
|
319
|
+
static createLevelAchievement(level: number): ThresholdAchievement;
|
|
320
|
+
/**
|
|
321
|
+
* Create multiple level achievements
|
|
322
|
+
* @param levels - Array of levels or [level, award] tuples
|
|
323
|
+
* @returns Complete SimpleAchievementConfig
|
|
324
|
+
*/
|
|
325
|
+
static createLevelAchievements(levels: (number | [number, AwardDetails])[]): SimpleAchievementConfig;
|
|
326
|
+
/**
|
|
327
|
+
* Create a boolean achievement with smart defaults
|
|
328
|
+
* @param metric - The metric name (e.g., 'completedTutorial')
|
|
329
|
+
* @returns Chainable BooleanAchievement
|
|
330
|
+
*/
|
|
331
|
+
static createBooleanAchievement(metric: string): BooleanAchievement;
|
|
332
|
+
/**
|
|
333
|
+
* Create a value-based achievement with smart defaults
|
|
334
|
+
* @param metric - The metric name (e.g., 'characterClass')
|
|
335
|
+
* @param value - The value to match (e.g., 'wizard')
|
|
336
|
+
* @returns Chainable ValueAchievement
|
|
337
|
+
*/
|
|
338
|
+
static createValueAchievement(metric: string, value: string): ValueAchievement;
|
|
339
|
+
/**
|
|
340
|
+
* Create a complex achievement builder for power users
|
|
341
|
+
* @returns ComplexAchievementBuilder for full control
|
|
342
|
+
*/
|
|
343
|
+
static create(): ComplexAchievementBuilder;
|
|
344
|
+
/**
|
|
345
|
+
* Combine multiple achievement configurations
|
|
346
|
+
* @param achievements - Array of SimpleAchievementConfig objects or Achievement instances
|
|
347
|
+
* @returns Combined SimpleAchievementConfig
|
|
348
|
+
*/
|
|
349
|
+
static combine(achievements: (SimpleAchievementConfig | Achievement)[]): SimpleAchievementConfig;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export { AchievementBuilder, AchievementCondition, AchievementConfiguration, AchievementConfigurationType, AchievementContext, AchievementContextValue, AchievementDetails, AchievementMetricArrayValue, AchievementMetricValue, AchievementMetrics, AchievementProvider, AchievementProviderProps$1 as AchievementProviderProps, AchievementState, AchievementStorage, AwardDetails, BadgesButton, BadgesModal, ConfettiWrapper, CustomAchievementDetails, InitialAchievementMetrics, LocalStorage, MemoryStorage, SimpleAchievementConfig, SimpleAchievementDetails, StorageType, StylesProps, defaultAchievementIcons, defaultStyles, isSimpleConfig, normalizeAchievements, useAchievements, useSimpleAchievements };
|