react-achievements 1.0.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/dist/badges.d.ts +8 -0
- package/dist/components/Achievement.d.ts +9 -0
- package/dist/components/Badge.d.ts +9 -0
- package/dist/components/ConfettiWrapper.d.ts +6 -0
- package/dist/context/AchievementContext.d.ts +14 -0
- package/dist/index.cjs.js +170 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.esm.js +166 -0
- package/dist/levels.d.ts +7 -0
- package/package.json +35 -0
- package/rollup.config.mjs +23 -0
- package/src/badges.ts +24 -0
- package/src/components/Achievement.tsx +20 -0
- package/src/components/Badge.tsx +34 -0
- package/src/components/ConfettiWrapper.tsx +15 -0
- package/src/context/AchievementContext.tsx +55 -0
- package/src/custom.d.ts +21 -0
- package/src/index.ts +4 -0
- package/src/levels.ts +13 -0
- package/tsconfig.json +109 -0
package/dist/badges.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { BadgeConfig } from '../badges';
|
|
3
|
+
import { LevelConfig } from '../levels';
|
|
4
|
+
interface AchievementContextProps {
|
|
5
|
+
metric: number;
|
|
6
|
+
setMetric: (value: number) => void;
|
|
7
|
+
badges: BadgeConfig[];
|
|
8
|
+
levels: LevelConfig[];
|
|
9
|
+
}
|
|
10
|
+
declare const AchievementProvider: React.FC<{
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}>;
|
|
13
|
+
declare const useAchievement: () => AchievementContextProps;
|
|
14
|
+
export { AchievementProvider, useAchievement };
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
const defaultBadges = [
|
|
6
|
+
{
|
|
7
|
+
id: 'beginner',
|
|
8
|
+
icon: '/path/to/beginner-icon.png',
|
|
9
|
+
title: 'Beginner',
|
|
10
|
+
description: 'Achieved beginner level',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
id: 'intermediate',
|
|
14
|
+
icon: '/path/to/intermediate-icon.png',
|
|
15
|
+
title: 'Intermediate',
|
|
16
|
+
description: 'Achieved intermediate level',
|
|
17
|
+
},
|
|
18
|
+
// Add more badges as needed
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const levels = [
|
|
22
|
+
{ level: 1, threshold: 10, badgeId: 'beginner' },
|
|
23
|
+
{ level: 2, threshold: 50, badgeId: 'intermediate' },
|
|
24
|
+
// Add more levels as needed
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
28
|
+
|
|
29
|
+
function getDefaultExportFromCjs (x) {
|
|
30
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var reactConfetti_min = {exports: {}};
|
|
34
|
+
|
|
35
|
+
(function (module, exports) {
|
|
36
|
+
!function(t,e){module.exports=e(React);}("undefined"!=typeof self?self:commonjsGlobal,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r});},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0});},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(e,n){e.exports=t;},function(t,e,n){var r={linear:function(t,e,n,r){return (n-e)*t/r+e},easeInQuad:function(t,e,n,r){return (n-e)*(t/=r)*t+e},easeOutQuad:function(t,e,n,r){return -(n-e)*(t/=r)*(t-2)+e},easeInOutQuad:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t+e:-i/2*(--t*(t-2)-1)+e},easeInCubic:function(t,e,n,r){return (n-e)*(t/=r)*t*t+e},easeOutCubic:function(t,e,n,r){return (n-e)*((t=t/r-1)*t*t+1)+e},easeInOutCubic:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t+e:i/2*((t-=2)*t*t+2)+e},easeInQuart:function(t,e,n,r){return (n-e)*(t/=r)*t*t*t+e},easeOutQuart:function(t,e,n,r){return -(n-e)*((t=t/r-1)*t*t*t-1)+e},easeInOutQuart:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t*t+e:-i/2*((t-=2)*t*t*t-2)+e},easeInQuint:function(t,e,n,r){return (n-e)*(t/=r)*t*t*t*t+e},easeOutQuint:function(t,e,n,r){return (n-e)*((t=t/r-1)*t*t*t*t+1)+e},easeInOutQuint:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t*t*t+e:i/2*((t-=2)*t*t*t*t+2)+e},easeInSine:function(t,e,n,r){var i=n-e;return -i*Math.cos(t/r*(Math.PI/2))+i+e},easeOutSine:function(t,e,n,r){return (n-e)*Math.sin(t/r*(Math.PI/2))+e},easeInOutSine:function(t,e,n,r){return -(n-e)/2*(Math.cos(Math.PI*t/r)-1)+e},easeInExpo:function(t,e,n,r){return 0==t?e:(n-e)*Math.pow(2,10*(t/r-1))+e},easeOutExpo:function(t,e,n,r){var i=n-e;return t==r?e+i:i*(1-Math.pow(2,-10*t/r))+e},easeInOutExpo:function(t,e,n,r){var i=n-e;return 0===t?e:t===r?e+i:(t/=r/2)<1?i/2*Math.pow(2,10*(t-1))+e:i/2*(2-Math.pow(2,-10*--t))+e},easeInCirc:function(t,e,n,r){return -(n-e)*(Math.sqrt(1-(t/=r)*t)-1)+e},easeOutCirc:function(t,e,n,r){return (n-e)*Math.sqrt(1-(t=t/r-1)*t)+e},easeInOutCirc:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?-i/2*(Math.sqrt(1-t*t)-1)+e:i/2*(Math.sqrt(1-(t-=2)*t)+1)+e},easeInElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:1==(t/=r)?e+c:((o=0)||(o=.3*r),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),-i*Math.pow(2,10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)+e)},easeOutElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:1==(t/=r)?e+c:((o=0)||(o=.3*r),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),i*Math.pow(2,-10*t)*Math.sin((t*r-a)*(2*Math.PI)/o)+c+e)},easeInOutElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:2==(t/=r/2)?e+c:((o=0)||(o=r*(.3*1.5)),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),t<1?i*Math.pow(2,10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)*-.5+e:i*Math.pow(2,-10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)*.5+c+e)},easeInBack:function(t,e,n,r,i){return void 0===i&&(i=1.70158),(n-e)*(t/=r)*t*((i+1)*t-i)+e},easeOutBack:function(t,e,n,r,i){return void 0===i&&(i=1.70158),(n-e)*((t=t/r-1)*t*((i+1)*t+i)+1)+e},easeInOutBack:function(t,e,n,r,i){var o=n-e;return void 0===i&&(i=1.70158),(t/=r/2)<1?o/2*(t*t*((1+(i*=1.525))*t-i))+e:o/2*((t-=2)*t*((1+(i*=1.525))*t+i)+2)+e},easeInBounce:function(t,e,n,i){var o=n-e;return o-r.easeOutBounce(i-t,0,o,i)+e},easeOutBounce:function(t,e,n,r){var i=n-e;return (t/=r)<1/2.75?i*(7.5625*t*t)+e:t<2/2.75?i*(7.5625*(t-=1.5/2.75)*t+.75)+e:t<2.5/2.75?i*(7.5625*(t-=2.25/2.75)*t+.9375)+e:i*(7.5625*(t-=2.625/2.75)*t+.984375)+e},easeInOutBounce:function(t,e,n,i){var o=n-e;return t<i/2?.5*r.easeInBounce(2*t,0,o,i)+e:.5*r.easeOutBounce(2*t-i,0,o,i)+.5*o+e}};t.exports=r;},function(t,e,n){t.exports=n(3);},function(t,e,n){n.r(e),n.d(e,"ReactConfetti",(function(){return Q}));var r,i,o=n(0),a=n.n(o),c=n(1),s=n.n(c);function u(t,e){return t+Math.random()*(e-t)}function f(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function h(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}!function(t){t[t.Circle=0]="Circle",t[t.Square=1]="Square",t[t.Strip=2]="Strip";}(r||(r={})),function(t){t[t.Positive=1]="Positive",t[t.Negative=-1]="Negative";}(i||(i={}));var l=function(){function t(e,n,r,o){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),h(this,"context",void 0),h(this,"radius",void 0),h(this,"x",void 0),h(this,"y",void 0),h(this,"w",void 0),h(this,"h",void 0),h(this,"vx",void 0),h(this,"vy",void 0),h(this,"shape",void 0),h(this,"angle",void 0),h(this,"angularSpin",void 0),h(this,"color",void 0),h(this,"rotateY",void 0),h(this,"rotationDirection",void 0),h(this,"getOptions",void 0),this.getOptions=n;var a,c,s=this.getOptions(),f=s.colors,l=s.initialVelocityX,p=s.initialVelocityY;this.context=e,this.x=r,this.y=o,this.w=u(5,20),this.h=u(5,20),this.radius=u(5,10),this.vx="number"==typeof l?u(-l,l):u(l.min,l.max),this.vy="number"==typeof p?u(-p,0):u(p.min,p.max),this.shape=(a=0,c=2,Math.floor(a+Math.random()*(c-a+1))),this.angle=u(0,360)*Math.PI/180,this.angularSpin=u(-.2,.2),this.color=f[Math.floor(Math.random()*f.length)],this.rotateY=u(0,1),this.rotationDirection=u(0,1)?i.Positive:i.Negative;}var e,n;return e=t,(n=[{key:"update",value:function(){var t=this.getOptions(),e=t.gravity,n=t.wind,o=t.friction,a=t.opacity,c=t.drawShape;this.x+=this.vx,this.y+=this.vy,this.vy+=e,this.vx+=n,this.vx*=o,this.vy*=o,this.rotateY>=1&&this.rotationDirection===i.Positive?this.rotationDirection=i.Negative:this.rotateY<=-1&&this.rotationDirection===i.Negative&&(this.rotationDirection=i.Positive);var s=.1*this.rotationDirection;if(this.rotateY+=s,this.angle+=this.angularSpin,this.context.save(),this.context.translate(this.x,this.y),this.context.rotate(this.angle),this.context.scale(1,this.rotateY),this.context.rotate(this.angle),this.context.beginPath(),this.context.fillStyle=this.color,this.context.strokeStyle=this.color,this.context.globalAlpha=a,this.context.lineCap="round",this.context.lineWidth=2,c&&"function"==typeof c)c.call(this,this.context);else switch(this.shape){case r.Circle:this.context.beginPath(),this.context.arc(0,0,this.radius,0,2*Math.PI),this.context.fill();break;case r.Square:this.context.fillRect(-this.w/2,-this.h/2,this.w,this.h);break;case r.Strip:this.context.fillRect(-this.w/6,-this.h/2,this.w/3,this.h);}this.context.closePath(),this.context.restore();}}])&&f(e.prototype,n),t}();function p(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var v=function t(e,n){var r=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),p(this,"canvas",void 0),p(this,"context",void 0),p(this,"getOptions",void 0),p(this,"x",0),p(this,"y",0),p(this,"w",0),p(this,"h",0),p(this,"lastNumberOfPieces",0),p(this,"tweenInitTime",Date.now()),p(this,"particles",[]),p(this,"particlesGenerated",0),p(this,"removeParticleAt",(function(t){r.particles.splice(t,1);})),p(this,"getParticle",(function(){var t=u(r.x,r.w+r.x),e=u(r.y,r.h+r.y);return new l(r.context,r.getOptions,t,e)})),p(this,"animate",(function(){var t=r.canvas,e=r.context,n=r.particlesGenerated,i=r.lastNumberOfPieces,o=r.getOptions(),a=o.run,c=o.recycle,s=o.numberOfPieces,u=o.debug,f=o.tweenFunction,h=o.tweenDuration;if(!a)return !1;var l=r.particles.length,p=c?l:n,v=Date.now();if(p<s){i!==s&&(r.tweenInitTime=v,r.lastNumberOfPieces=s);for(var y=r.tweenInitTime,d=f(v-y>h?h:Math.max(0,v-y),p,s,h),b=Math.round(d-p),g=0;g<b;g++)r.particles.push(r.getParticle());r.particlesGenerated+=b;}return u&&(e.font="12px sans-serif",e.fillStyle="#333",e.textAlign="right",e.fillText("Particles: ".concat(l),t.width-10,t.height-20)),r.particles.forEach((function(e,n){e.update(),(e.y>t.height||e.y<-100||e.x>t.width+100||e.x<-100)&&(c&&p<=s?r.particles[n]=r.getParticle():r.removeParticleAt(n));})),l>0||p<s})),this.canvas=e;var i=this.canvas.getContext("2d");if(!i)throw new Error("Could not get canvas context");this.context=i,this.getOptions=n;};function y(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r);}return n}function d(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?y(Object(n),!0).forEach((function(e){g(t,e,n[e]);})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e));}));}return t}function b(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function g(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var O={width:"undefined"!=typeof window?window.innerWidth:300,height:"undefined"!=typeof window?window.innerHeight:200,numberOfPieces:200,friction:.99,wind:0,gravity:.1,initialVelocityX:4,initialVelocityY:10,colors:["#f44336","#e91e63","#9c27b0","#673ab7","#3f51b5","#2196f3","#03a9f4","#00bcd4","#009688","#4CAF50","#8BC34A","#CDDC39","#FFEB3B","#FFC107","#FF9800","#FF5722","#795548"],opacity:1,debug:!1,tweenFunction:s.a.easeInOutQuad,tweenDuration:5e3,recycle:!0,run:!0},w=function(){function t(e,n){var r=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),g(this,"canvas",void 0),g(this,"context",void 0),g(this,"_options",void 0),g(this,"generator",void 0),g(this,"rafId",void 0),g(this,"setOptionsWithDefaults",(function(t){var e={confettiSource:{x:0,y:0,w:r.canvas.width,h:0}};r._options=d(d(d({},e),O),t),Object.assign(r,t.confettiSource);})),g(this,"update",(function(){var t=r.options,e=t.run,n=t.onConfettiComplete,i=r.canvas,o=r.context;e&&(o.fillStyle="white",o.clearRect(0,0,i.width,i.height)),r.generator.animate()?r.rafId=requestAnimationFrame(r.update):(n&&"function"==typeof n&&r.generator.particlesGenerated>0&&n.call(r,r),r._options.run=!1);})),g(this,"reset",(function(){r.generator&&r.generator.particlesGenerated>0&&(r.generator.particlesGenerated=0,r.generator.particles=[],r.generator.lastNumberOfPieces=0);})),g(this,"stop",(function(){r.options={run:!1},r.rafId&&(cancelAnimationFrame(r.rafId),r.rafId=void 0);})),this.canvas=e;var i=this.canvas.getContext("2d");if(!i)throw new Error("Could not get canvas context");this.context=i,this.generator=new v(this.canvas,(function(){return r.options})),this.options=n,this.update();}var e,n;return e=t,(n=[{key:"options",get:function(){return this._options},set:function(t){var e=this._options&&this._options.run,n=this._options&&this._options.recycle;this.setOptionsWithDefaults(t),this.generator&&(Object.assign(this.generator,this.options.confettiSource),"boolean"==typeof t.recycle&&t.recycle&&!1===n&&(this.generator.lastNumberOfPieces=this.generator.particles.length)),"boolean"==typeof t.run&&t.run&&!1===e&&this.update();}}])&&b(e.prototype,n),t}();function m(t){return function(t){if(Array.isArray(t))return C(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||S(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function x(t){return (x="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function P(){return (P=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r]);}return t}).apply(this,arguments)}function j(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r);}return n}function M(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?j(Object(n),!0).forEach((function(e){T(t,e,n[e]);})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):j(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e));}));}return t}function I(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],r=!0,i=!1,o=void 0;try{for(var a,c=t[Symbol.iterator]();!(r=(a=c.next()).done)&&(n.push(a.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t;}finally{try{r||null==c.return||c.return();}finally{if(i)throw o}}return n}(t,e)||S(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function S(t,e){if(t){if("string"==typeof t)return C(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return "Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?C(t,e):void 0}}function C(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function D(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function E(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function _(t,e){return (_=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function R(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return !1}}();return function(){var n,r=F(t);if(e){var i=F(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return k(this,n)}}function k(t,e){return !e||"object"!==x(e)&&"function"!=typeof e?A(t):e}function A(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function F(t){return (F=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function T(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var B=a.a.createRef(),N=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&_(t,e);}(o,t);var e,n,i=R(o);function o(t){var e;D(this,o);for(var n=arguments.length,r=new Array(n>1?n-1:0),c=1;c<n;c++)r[c-1]=arguments[c];return T(A(e=i.call.apply(i,[this,t].concat(r))),"canvas",a.a.createRef()),T(A(e),"confetti",void 0),e.canvas=t.canvasRef||B,e}return e=o,(n=[{key:"componentDidMount",value:function(){if(this.canvas.current){var t=q(this.props)[0];this.confetti=new w(this.canvas.current,t);}}},{key:"componentDidUpdate",value:function(){var t=q(this.props)[0];this.confetti&&(this.confetti.options=t);}},{key:"componentWillUnmount",value:function(){this.confetti&&this.confetti.stop(),this.confetti=void 0;}},{key:"render",value:function(){var t=I(q(this.props),2),e=t[0],n=t[1],r=M({zIndex:2,position:"absolute",pointerEvents:"none",top:0,left:0,bottom:0,right:0},n.style);return a.a.createElement("canvas",P({width:e.width,height:e.height,ref:this.canvas},n,{style:r}))}}])&&E(e.prototype,n),o}(o.Component);function q(t){var e={},n={},r=[].concat(m(Object.keys(O)),["confettiSource","drawShape","onConfettiComplete"]),i=["canvasRef"];for(var o in t){var a=t[o];r.includes(o)?e[o]=a:i.includes(o)?i[o]=a:n[o]=a;}return [e,n,{}]}T(N,"defaultProps",M({},O)),T(N,"displayName","ReactConfetti");var Q=a.a.forwardRef((function(t,e){return a.a.createElement(N,P({canvasRef:e},t))}));e.default=Q;}]).default}));
|
|
37
|
+
|
|
38
|
+
} (reactConfetti_min));
|
|
39
|
+
|
|
40
|
+
var reactConfetti_minExports = reactConfetti_min.exports;
|
|
41
|
+
var Confetti = /*@__PURE__*/getDefaultExportFromCjs(reactConfetti_minExports);
|
|
42
|
+
|
|
43
|
+
function on(obj) {
|
|
44
|
+
var args = [];
|
|
45
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
46
|
+
args[_i - 1] = arguments[_i];
|
|
47
|
+
}
|
|
48
|
+
if (obj && obj.addEventListener) {
|
|
49
|
+
obj.addEventListener.apply(obj, args);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function off(obj) {
|
|
53
|
+
var args = [];
|
|
54
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
55
|
+
args[_i - 1] = arguments[_i];
|
|
56
|
+
}
|
|
57
|
+
if (obj && obj.removeEventListener) {
|
|
58
|
+
obj.removeEventListener.apply(obj, args);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
var isBrowser = typeof window !== 'undefined';
|
|
62
|
+
|
|
63
|
+
var useEffectOnce = function (effect) {
|
|
64
|
+
React.useEffect(effect, []);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
var useUnmount = function (fn) {
|
|
68
|
+
var fnRef = React.useRef(fn);
|
|
69
|
+
// update the ref each render so if it change the newest callback will be invoked
|
|
70
|
+
fnRef.current = fn;
|
|
71
|
+
useEffectOnce(function () { return function () { return fnRef.current(); }; });
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
var useRafState = function (initialState) {
|
|
75
|
+
var frame = React.useRef(0);
|
|
76
|
+
var _a = React.useState(initialState), state = _a[0], setState = _a[1];
|
|
77
|
+
var setRafState = React.useCallback(function (value) {
|
|
78
|
+
cancelAnimationFrame(frame.current);
|
|
79
|
+
frame.current = requestAnimationFrame(function () {
|
|
80
|
+
setState(value);
|
|
81
|
+
});
|
|
82
|
+
}, []);
|
|
83
|
+
useUnmount(function () {
|
|
84
|
+
cancelAnimationFrame(frame.current);
|
|
85
|
+
});
|
|
86
|
+
return [state, setRafState];
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
var useWindowSize = function (initialWidth, initialHeight) {
|
|
90
|
+
if (initialWidth === void 0) { initialWidth = Infinity; }
|
|
91
|
+
if (initialHeight === void 0) { initialHeight = Infinity; }
|
|
92
|
+
var _a = useRafState({
|
|
93
|
+
width: isBrowser ? window.innerWidth : initialWidth,
|
|
94
|
+
height: isBrowser ? window.innerHeight : initialHeight,
|
|
95
|
+
}), state = _a[0], setState = _a[1];
|
|
96
|
+
React.useEffect(function () {
|
|
97
|
+
if (isBrowser) {
|
|
98
|
+
var handler_1 = function () {
|
|
99
|
+
setState({
|
|
100
|
+
width: window.innerWidth,
|
|
101
|
+
height: window.innerHeight,
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
on(window, 'resize', handler_1);
|
|
105
|
+
return function () {
|
|
106
|
+
off(window, 'resize', handler_1);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}, []);
|
|
110
|
+
return state;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const ConfettiWrapper = ({ show }) => {
|
|
114
|
+
const { width, height } = useWindowSize();
|
|
115
|
+
return show ? React.createElement(Confetti, { width: width, height: height }) : null;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const Badge = ({ icon, title, description, position = 'top-right' }) => {
|
|
119
|
+
const badgeStyle = {
|
|
120
|
+
position: 'fixed',
|
|
121
|
+
[position.split('-')[0]]: '10px',
|
|
122
|
+
[position.split('-')[1]]: '10px',
|
|
123
|
+
display: 'flex',
|
|
124
|
+
flexDirection: 'column',
|
|
125
|
+
alignItems: 'center',
|
|
126
|
+
backgroundColor: '#fff',
|
|
127
|
+
border: '1px solid #ccc',
|
|
128
|
+
borderRadius: '8px',
|
|
129
|
+
padding: '10px',
|
|
130
|
+
boxShadow: '0 0 10px rgba(0,0,0,0.1)',
|
|
131
|
+
};
|
|
132
|
+
return (React.createElement("div", { style: badgeStyle },
|
|
133
|
+
React.createElement("img", { src: icon, alt: title, style: { width: '50px', height: '50px' } }),
|
|
134
|
+
React.createElement("h4", null, title),
|
|
135
|
+
React.createElement("p", null, description)));
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const AchievementContext = React.createContext(undefined);
|
|
139
|
+
const AchievementProvider = ({ children }) => {
|
|
140
|
+
const [metric, setMetric] = React.useState(0);
|
|
141
|
+
const [achievedLevels, setAchievedLevels] = React.useState([]);
|
|
142
|
+
const [showConfetti, setShowConfetti] = React.useState(false);
|
|
143
|
+
return (React.createElement(AchievementContext.Provider, { value: { metric, setMetric, badges: defaultBadges, levels } },
|
|
144
|
+
children,
|
|
145
|
+
levels.map(levelConfig => {
|
|
146
|
+
var _a, _b, _c;
|
|
147
|
+
return achievedLevels.includes(levelConfig.level) ? (React.createElement(Badge, { key: levelConfig.level, icon: ((_a = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _a === void 0 ? void 0 : _a.icon) || '', title: ((_b = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _b === void 0 ? void 0 : _b.title) || '', description: ((_c = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _c === void 0 ? void 0 : _c.description) || '' })) : null;
|
|
148
|
+
}),
|
|
149
|
+
React.createElement(ConfettiWrapper, { show: showConfetti })));
|
|
150
|
+
};
|
|
151
|
+
const useAchievement = () => {
|
|
152
|
+
const context = React.useContext(AchievementContext);
|
|
153
|
+
if (context === undefined) {
|
|
154
|
+
throw new Error('useAchievement must be used within an AchievementProvider');
|
|
155
|
+
}
|
|
156
|
+
return context;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const Achievement = ({ metric, threshold, onAchieve, children }) => {
|
|
160
|
+
React.useEffect(() => {
|
|
161
|
+
if (metric >= threshold) {
|
|
162
|
+
onAchieve();
|
|
163
|
+
}
|
|
164
|
+
}, [metric, threshold, onAchieve]);
|
|
165
|
+
return React.createElement("div", null, children);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
exports.Achievement = Achievement;
|
|
169
|
+
exports.AchievementProvider = AchievementProvider;
|
|
170
|
+
exports.useAchievement = useAchievement;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState, useCallback, createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
const defaultBadges = [
|
|
4
|
+
{
|
|
5
|
+
id: 'beginner',
|
|
6
|
+
icon: '/path/to/beginner-icon.png',
|
|
7
|
+
title: 'Beginner',
|
|
8
|
+
description: 'Achieved beginner level',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
id: 'intermediate',
|
|
12
|
+
icon: '/path/to/intermediate-icon.png',
|
|
13
|
+
title: 'Intermediate',
|
|
14
|
+
description: 'Achieved intermediate level',
|
|
15
|
+
},
|
|
16
|
+
// Add more badges as needed
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const levels = [
|
|
20
|
+
{ level: 1, threshold: 10, badgeId: 'beginner' },
|
|
21
|
+
{ level: 2, threshold: 50, badgeId: 'intermediate' },
|
|
22
|
+
// Add more levels as needed
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
26
|
+
|
|
27
|
+
function getDefaultExportFromCjs (x) {
|
|
28
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var reactConfetti_min = {exports: {}};
|
|
32
|
+
|
|
33
|
+
(function (module, exports) {
|
|
34
|
+
!function(t,e){module.exports=e(React);}("undefined"!=typeof self?self:commonjsGlobal,(function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r});},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0});},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(e,n){e.exports=t;},function(t,e,n){var r={linear:function(t,e,n,r){return (n-e)*t/r+e},easeInQuad:function(t,e,n,r){return (n-e)*(t/=r)*t+e},easeOutQuad:function(t,e,n,r){return -(n-e)*(t/=r)*(t-2)+e},easeInOutQuad:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t+e:-i/2*(--t*(t-2)-1)+e},easeInCubic:function(t,e,n,r){return (n-e)*(t/=r)*t*t+e},easeOutCubic:function(t,e,n,r){return (n-e)*((t=t/r-1)*t*t+1)+e},easeInOutCubic:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t+e:i/2*((t-=2)*t*t+2)+e},easeInQuart:function(t,e,n,r){return (n-e)*(t/=r)*t*t*t+e},easeOutQuart:function(t,e,n,r){return -(n-e)*((t=t/r-1)*t*t*t-1)+e},easeInOutQuart:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t*t+e:-i/2*((t-=2)*t*t*t-2)+e},easeInQuint:function(t,e,n,r){return (n-e)*(t/=r)*t*t*t*t+e},easeOutQuint:function(t,e,n,r){return (n-e)*((t=t/r-1)*t*t*t*t+1)+e},easeInOutQuint:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?i/2*t*t*t*t*t+e:i/2*((t-=2)*t*t*t*t+2)+e},easeInSine:function(t,e,n,r){var i=n-e;return -i*Math.cos(t/r*(Math.PI/2))+i+e},easeOutSine:function(t,e,n,r){return (n-e)*Math.sin(t/r*(Math.PI/2))+e},easeInOutSine:function(t,e,n,r){return -(n-e)/2*(Math.cos(Math.PI*t/r)-1)+e},easeInExpo:function(t,e,n,r){return 0==t?e:(n-e)*Math.pow(2,10*(t/r-1))+e},easeOutExpo:function(t,e,n,r){var i=n-e;return t==r?e+i:i*(1-Math.pow(2,-10*t/r))+e},easeInOutExpo:function(t,e,n,r){var i=n-e;return 0===t?e:t===r?e+i:(t/=r/2)<1?i/2*Math.pow(2,10*(t-1))+e:i/2*(2-Math.pow(2,-10*--t))+e},easeInCirc:function(t,e,n,r){return -(n-e)*(Math.sqrt(1-(t/=r)*t)-1)+e},easeOutCirc:function(t,e,n,r){return (n-e)*Math.sqrt(1-(t=t/r-1)*t)+e},easeInOutCirc:function(t,e,n,r){var i=n-e;return (t/=r/2)<1?-i/2*(Math.sqrt(1-t*t)-1)+e:i/2*(Math.sqrt(1-(t-=2)*t)+1)+e},easeInElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:1==(t/=r)?e+c:((o=0)||(o=.3*r),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),-i*Math.pow(2,10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)+e)},easeOutElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:1==(t/=r)?e+c:((o=0)||(o=.3*r),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),i*Math.pow(2,-10*t)*Math.sin((t*r-a)*(2*Math.PI)/o)+c+e)},easeInOutElastic:function(t,e,n,r){var i,o,a,c=n-e;return a=1.70158,0===t?e:2==(t/=r/2)?e+c:((o=0)||(o=r*(.3*1.5)),(i=c)<Math.abs(c)?(i=c,a=o/4):a=o/(2*Math.PI)*Math.asin(c/i),t<1?i*Math.pow(2,10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)*-.5+e:i*Math.pow(2,-10*(t-=1))*Math.sin((t*r-a)*(2*Math.PI)/o)*.5+c+e)},easeInBack:function(t,e,n,r,i){return void 0===i&&(i=1.70158),(n-e)*(t/=r)*t*((i+1)*t-i)+e},easeOutBack:function(t,e,n,r,i){return void 0===i&&(i=1.70158),(n-e)*((t=t/r-1)*t*((i+1)*t+i)+1)+e},easeInOutBack:function(t,e,n,r,i){var o=n-e;return void 0===i&&(i=1.70158),(t/=r/2)<1?o/2*(t*t*((1+(i*=1.525))*t-i))+e:o/2*((t-=2)*t*((1+(i*=1.525))*t+i)+2)+e},easeInBounce:function(t,e,n,i){var o=n-e;return o-r.easeOutBounce(i-t,0,o,i)+e},easeOutBounce:function(t,e,n,r){var i=n-e;return (t/=r)<1/2.75?i*(7.5625*t*t)+e:t<2/2.75?i*(7.5625*(t-=1.5/2.75)*t+.75)+e:t<2.5/2.75?i*(7.5625*(t-=2.25/2.75)*t+.9375)+e:i*(7.5625*(t-=2.625/2.75)*t+.984375)+e},easeInOutBounce:function(t,e,n,i){var o=n-e;return t<i/2?.5*r.easeInBounce(2*t,0,o,i)+e:.5*r.easeOutBounce(2*t-i,0,o,i)+.5*o+e}};t.exports=r;},function(t,e,n){t.exports=n(3);},function(t,e,n){n.r(e),n.d(e,"ReactConfetti",(function(){return Q}));var r,i,o=n(0),a=n.n(o),c=n(1),s=n.n(c);function u(t,e){return t+Math.random()*(e-t)}function f(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function h(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}!function(t){t[t.Circle=0]="Circle",t[t.Square=1]="Square",t[t.Strip=2]="Strip";}(r||(r={})),function(t){t[t.Positive=1]="Positive",t[t.Negative=-1]="Negative";}(i||(i={}));var l=function(){function t(e,n,r,o){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),h(this,"context",void 0),h(this,"radius",void 0),h(this,"x",void 0),h(this,"y",void 0),h(this,"w",void 0),h(this,"h",void 0),h(this,"vx",void 0),h(this,"vy",void 0),h(this,"shape",void 0),h(this,"angle",void 0),h(this,"angularSpin",void 0),h(this,"color",void 0),h(this,"rotateY",void 0),h(this,"rotationDirection",void 0),h(this,"getOptions",void 0),this.getOptions=n;var a,c,s=this.getOptions(),f=s.colors,l=s.initialVelocityX,p=s.initialVelocityY;this.context=e,this.x=r,this.y=o,this.w=u(5,20),this.h=u(5,20),this.radius=u(5,10),this.vx="number"==typeof l?u(-l,l):u(l.min,l.max),this.vy="number"==typeof p?u(-p,0):u(p.min,p.max),this.shape=(a=0,c=2,Math.floor(a+Math.random()*(c-a+1))),this.angle=u(0,360)*Math.PI/180,this.angularSpin=u(-.2,.2),this.color=f[Math.floor(Math.random()*f.length)],this.rotateY=u(0,1),this.rotationDirection=u(0,1)?i.Positive:i.Negative;}var e,n;return e=t,(n=[{key:"update",value:function(){var t=this.getOptions(),e=t.gravity,n=t.wind,o=t.friction,a=t.opacity,c=t.drawShape;this.x+=this.vx,this.y+=this.vy,this.vy+=e,this.vx+=n,this.vx*=o,this.vy*=o,this.rotateY>=1&&this.rotationDirection===i.Positive?this.rotationDirection=i.Negative:this.rotateY<=-1&&this.rotationDirection===i.Negative&&(this.rotationDirection=i.Positive);var s=.1*this.rotationDirection;if(this.rotateY+=s,this.angle+=this.angularSpin,this.context.save(),this.context.translate(this.x,this.y),this.context.rotate(this.angle),this.context.scale(1,this.rotateY),this.context.rotate(this.angle),this.context.beginPath(),this.context.fillStyle=this.color,this.context.strokeStyle=this.color,this.context.globalAlpha=a,this.context.lineCap="round",this.context.lineWidth=2,c&&"function"==typeof c)c.call(this,this.context);else switch(this.shape){case r.Circle:this.context.beginPath(),this.context.arc(0,0,this.radius,0,2*Math.PI),this.context.fill();break;case r.Square:this.context.fillRect(-this.w/2,-this.h/2,this.w,this.h);break;case r.Strip:this.context.fillRect(-this.w/6,-this.h/2,this.w/3,this.h);}this.context.closePath(),this.context.restore();}}])&&f(e.prototype,n),t}();function p(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var v=function t(e,n){var r=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),p(this,"canvas",void 0),p(this,"context",void 0),p(this,"getOptions",void 0),p(this,"x",0),p(this,"y",0),p(this,"w",0),p(this,"h",0),p(this,"lastNumberOfPieces",0),p(this,"tweenInitTime",Date.now()),p(this,"particles",[]),p(this,"particlesGenerated",0),p(this,"removeParticleAt",(function(t){r.particles.splice(t,1);})),p(this,"getParticle",(function(){var t=u(r.x,r.w+r.x),e=u(r.y,r.h+r.y);return new l(r.context,r.getOptions,t,e)})),p(this,"animate",(function(){var t=r.canvas,e=r.context,n=r.particlesGenerated,i=r.lastNumberOfPieces,o=r.getOptions(),a=o.run,c=o.recycle,s=o.numberOfPieces,u=o.debug,f=o.tweenFunction,h=o.tweenDuration;if(!a)return !1;var l=r.particles.length,p=c?l:n,v=Date.now();if(p<s){i!==s&&(r.tweenInitTime=v,r.lastNumberOfPieces=s);for(var y=r.tweenInitTime,d=f(v-y>h?h:Math.max(0,v-y),p,s,h),b=Math.round(d-p),g=0;g<b;g++)r.particles.push(r.getParticle());r.particlesGenerated+=b;}return u&&(e.font="12px sans-serif",e.fillStyle="#333",e.textAlign="right",e.fillText("Particles: ".concat(l),t.width-10,t.height-20)),r.particles.forEach((function(e,n){e.update(),(e.y>t.height||e.y<-100||e.x>t.width+100||e.x<-100)&&(c&&p<=s?r.particles[n]=r.getParticle():r.removeParticleAt(n));})),l>0||p<s})),this.canvas=e;var i=this.canvas.getContext("2d");if(!i)throw new Error("Could not get canvas context");this.context=i,this.getOptions=n;};function y(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r);}return n}function d(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?y(Object(n),!0).forEach((function(e){g(t,e,n[e]);})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e));}));}return t}function b(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function g(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var O={width:"undefined"!=typeof window?window.innerWidth:300,height:"undefined"!=typeof window?window.innerHeight:200,numberOfPieces:200,friction:.99,wind:0,gravity:.1,initialVelocityX:4,initialVelocityY:10,colors:["#f44336","#e91e63","#9c27b0","#673ab7","#3f51b5","#2196f3","#03a9f4","#00bcd4","#009688","#4CAF50","#8BC34A","#CDDC39","#FFEB3B","#FFC107","#FF9800","#FF5722","#795548"],opacity:1,debug:!1,tweenFunction:s.a.easeInOutQuad,tweenDuration:5e3,recycle:!0,run:!0},w=function(){function t(e,n){var r=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),g(this,"canvas",void 0),g(this,"context",void 0),g(this,"_options",void 0),g(this,"generator",void 0),g(this,"rafId",void 0),g(this,"setOptionsWithDefaults",(function(t){var e={confettiSource:{x:0,y:0,w:r.canvas.width,h:0}};r._options=d(d(d({},e),O),t),Object.assign(r,t.confettiSource);})),g(this,"update",(function(){var t=r.options,e=t.run,n=t.onConfettiComplete,i=r.canvas,o=r.context;e&&(o.fillStyle="white",o.clearRect(0,0,i.width,i.height)),r.generator.animate()?r.rafId=requestAnimationFrame(r.update):(n&&"function"==typeof n&&r.generator.particlesGenerated>0&&n.call(r,r),r._options.run=!1);})),g(this,"reset",(function(){r.generator&&r.generator.particlesGenerated>0&&(r.generator.particlesGenerated=0,r.generator.particles=[],r.generator.lastNumberOfPieces=0);})),g(this,"stop",(function(){r.options={run:!1},r.rafId&&(cancelAnimationFrame(r.rafId),r.rafId=void 0);})),this.canvas=e;var i=this.canvas.getContext("2d");if(!i)throw new Error("Could not get canvas context");this.context=i,this.generator=new v(this.canvas,(function(){return r.options})),this.options=n,this.update();}var e,n;return e=t,(n=[{key:"options",get:function(){return this._options},set:function(t){var e=this._options&&this._options.run,n=this._options&&this._options.recycle;this.setOptionsWithDefaults(t),this.generator&&(Object.assign(this.generator,this.options.confettiSource),"boolean"==typeof t.recycle&&t.recycle&&!1===n&&(this.generator.lastNumberOfPieces=this.generator.particles.length)),"boolean"==typeof t.run&&t.run&&!1===e&&this.update();}}])&&b(e.prototype,n),t}();function m(t){return function(t){if(Array.isArray(t))return C(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||S(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function x(t){return (x="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function P(){return (P=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r]);}return t}).apply(this,arguments)}function j(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r);}return n}function M(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?j(Object(n),!0).forEach((function(e){T(t,e,n[e]);})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):j(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e));}));}return t}function I(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],r=!0,i=!1,o=void 0;try{for(var a,c=t[Symbol.iterator]();!(r=(a=c.next()).done)&&(n.push(a.value),!e||n.length!==e);r=!0);}catch(t){i=!0,o=t;}finally{try{r||null==c.return||c.return();}finally{if(i)throw o}}return n}(t,e)||S(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function S(t,e){if(t){if("string"==typeof t)return C(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return "Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?C(t,e):void 0}}function C(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function D(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function E(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r);}}function _(t,e){return (_=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function R(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return !1}}();return function(){var n,r=F(t);if(e){var i=F(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return k(this,n)}}function k(t,e){return !e||"object"!==x(e)&&"function"!=typeof e?A(t):e}function A(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function F(t){return (F=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function T(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var B=a.a.createRef(),N=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&_(t,e);}(o,t);var e,n,i=R(o);function o(t){var e;D(this,o);for(var n=arguments.length,r=new Array(n>1?n-1:0),c=1;c<n;c++)r[c-1]=arguments[c];return T(A(e=i.call.apply(i,[this,t].concat(r))),"canvas",a.a.createRef()),T(A(e),"confetti",void 0),e.canvas=t.canvasRef||B,e}return e=o,(n=[{key:"componentDidMount",value:function(){if(this.canvas.current){var t=q(this.props)[0];this.confetti=new w(this.canvas.current,t);}}},{key:"componentDidUpdate",value:function(){var t=q(this.props)[0];this.confetti&&(this.confetti.options=t);}},{key:"componentWillUnmount",value:function(){this.confetti&&this.confetti.stop(),this.confetti=void 0;}},{key:"render",value:function(){var t=I(q(this.props),2),e=t[0],n=t[1],r=M({zIndex:2,position:"absolute",pointerEvents:"none",top:0,left:0,bottom:0,right:0},n.style);return a.a.createElement("canvas",P({width:e.width,height:e.height,ref:this.canvas},n,{style:r}))}}])&&E(e.prototype,n),o}(o.Component);function q(t){var e={},n={},r=[].concat(m(Object.keys(O)),["confettiSource","drawShape","onConfettiComplete"]),i=["canvasRef"];for(var o in t){var a=t[o];r.includes(o)?e[o]=a:i.includes(o)?i[o]=a:n[o]=a;}return [e,n,{}]}T(N,"defaultProps",M({},O)),T(N,"displayName","ReactConfetti");var Q=a.a.forwardRef((function(t,e){return a.a.createElement(N,P({canvasRef:e},t))}));e.default=Q;}]).default}));
|
|
35
|
+
|
|
36
|
+
} (reactConfetti_min));
|
|
37
|
+
|
|
38
|
+
var reactConfetti_minExports = reactConfetti_min.exports;
|
|
39
|
+
var Confetti = /*@__PURE__*/getDefaultExportFromCjs(reactConfetti_minExports);
|
|
40
|
+
|
|
41
|
+
function on(obj) {
|
|
42
|
+
var args = [];
|
|
43
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
44
|
+
args[_i - 1] = arguments[_i];
|
|
45
|
+
}
|
|
46
|
+
if (obj && obj.addEventListener) {
|
|
47
|
+
obj.addEventListener.apply(obj, args);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function off(obj) {
|
|
51
|
+
var args = [];
|
|
52
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
53
|
+
args[_i - 1] = arguments[_i];
|
|
54
|
+
}
|
|
55
|
+
if (obj && obj.removeEventListener) {
|
|
56
|
+
obj.removeEventListener.apply(obj, args);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
var isBrowser = typeof window !== 'undefined';
|
|
60
|
+
|
|
61
|
+
var useEffectOnce = function (effect) {
|
|
62
|
+
useEffect(effect, []);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
var useUnmount = function (fn) {
|
|
66
|
+
var fnRef = useRef(fn);
|
|
67
|
+
// update the ref each render so if it change the newest callback will be invoked
|
|
68
|
+
fnRef.current = fn;
|
|
69
|
+
useEffectOnce(function () { return function () { return fnRef.current(); }; });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var useRafState = function (initialState) {
|
|
73
|
+
var frame = useRef(0);
|
|
74
|
+
var _a = useState(initialState), state = _a[0], setState = _a[1];
|
|
75
|
+
var setRafState = useCallback(function (value) {
|
|
76
|
+
cancelAnimationFrame(frame.current);
|
|
77
|
+
frame.current = requestAnimationFrame(function () {
|
|
78
|
+
setState(value);
|
|
79
|
+
});
|
|
80
|
+
}, []);
|
|
81
|
+
useUnmount(function () {
|
|
82
|
+
cancelAnimationFrame(frame.current);
|
|
83
|
+
});
|
|
84
|
+
return [state, setRafState];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
var useWindowSize = function (initialWidth, initialHeight) {
|
|
88
|
+
if (initialWidth === void 0) { initialWidth = Infinity; }
|
|
89
|
+
if (initialHeight === void 0) { initialHeight = Infinity; }
|
|
90
|
+
var _a = useRafState({
|
|
91
|
+
width: isBrowser ? window.innerWidth : initialWidth,
|
|
92
|
+
height: isBrowser ? window.innerHeight : initialHeight,
|
|
93
|
+
}), state = _a[0], setState = _a[1];
|
|
94
|
+
useEffect(function () {
|
|
95
|
+
if (isBrowser) {
|
|
96
|
+
var handler_1 = function () {
|
|
97
|
+
setState({
|
|
98
|
+
width: window.innerWidth,
|
|
99
|
+
height: window.innerHeight,
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
on(window, 'resize', handler_1);
|
|
103
|
+
return function () {
|
|
104
|
+
off(window, 'resize', handler_1);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}, []);
|
|
108
|
+
return state;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const ConfettiWrapper = ({ show }) => {
|
|
112
|
+
const { width, height } = useWindowSize();
|
|
113
|
+
return show ? React.createElement(Confetti, { width: width, height: height }) : null;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const Badge = ({ icon, title, description, position = 'top-right' }) => {
|
|
117
|
+
const badgeStyle = {
|
|
118
|
+
position: 'fixed',
|
|
119
|
+
[position.split('-')[0]]: '10px',
|
|
120
|
+
[position.split('-')[1]]: '10px',
|
|
121
|
+
display: 'flex',
|
|
122
|
+
flexDirection: 'column',
|
|
123
|
+
alignItems: 'center',
|
|
124
|
+
backgroundColor: '#fff',
|
|
125
|
+
border: '1px solid #ccc',
|
|
126
|
+
borderRadius: '8px',
|
|
127
|
+
padding: '10px',
|
|
128
|
+
boxShadow: '0 0 10px rgba(0,0,0,0.1)',
|
|
129
|
+
};
|
|
130
|
+
return (React.createElement("div", { style: badgeStyle },
|
|
131
|
+
React.createElement("img", { src: icon, alt: title, style: { width: '50px', height: '50px' } }),
|
|
132
|
+
React.createElement("h4", null, title),
|
|
133
|
+
React.createElement("p", null, description)));
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const AchievementContext = createContext(undefined);
|
|
137
|
+
const AchievementProvider = ({ children }) => {
|
|
138
|
+
const [metric, setMetric] = useState(0);
|
|
139
|
+
const [achievedLevels, setAchievedLevels] = useState([]);
|
|
140
|
+
const [showConfetti, setShowConfetti] = useState(false);
|
|
141
|
+
return (React.createElement(AchievementContext.Provider, { value: { metric, setMetric, badges: defaultBadges, levels } },
|
|
142
|
+
children,
|
|
143
|
+
levels.map(levelConfig => {
|
|
144
|
+
var _a, _b, _c;
|
|
145
|
+
return achievedLevels.includes(levelConfig.level) ? (React.createElement(Badge, { key: levelConfig.level, icon: ((_a = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _a === void 0 ? void 0 : _a.icon) || '', title: ((_b = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _b === void 0 ? void 0 : _b.title) || '', description: ((_c = defaultBadges.find(badge => badge.id === levelConfig.badgeId)) === null || _c === void 0 ? void 0 : _c.description) || '' })) : null;
|
|
146
|
+
}),
|
|
147
|
+
React.createElement(ConfettiWrapper, { show: showConfetti })));
|
|
148
|
+
};
|
|
149
|
+
const useAchievement = () => {
|
|
150
|
+
const context = useContext(AchievementContext);
|
|
151
|
+
if (context === undefined) {
|
|
152
|
+
throw new Error('useAchievement must be used within an AchievementProvider');
|
|
153
|
+
}
|
|
154
|
+
return context;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const Achievement = ({ metric, threshold, onAchieve, children }) => {
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
if (metric >= threshold) {
|
|
160
|
+
onAchieve();
|
|
161
|
+
}
|
|
162
|
+
}, [metric, threshold, onAchieve]);
|
|
163
|
+
return React.createElement("div", null, children);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export { Achievement, AchievementProvider, useAchievement };
|
package/dist/levels.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-achievements",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "This package allows users to transpose a React game engine over their React apps",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"badge",
|
|
8
|
+
"achievement"
|
|
9
|
+
],
|
|
10
|
+
"main": "dist/cjs.js",
|
|
11
|
+
"module": "dist/index.esm.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
15
|
+
"build": "rollup -c"
|
|
16
|
+
},
|
|
17
|
+
"author": "David Brown",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
21
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
22
|
+
"rollup": "^4.19.0",
|
|
23
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
24
|
+
"typescript": "^5.5.4"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@types/jest": "^29.5.12",
|
|
28
|
+
"@types/node": "^20.14.12",
|
|
29
|
+
"@types/react": "^18.3.3",
|
|
30
|
+
"@types/react-dom": "^18.3.0",
|
|
31
|
+
"react": "^18.3.1",
|
|
32
|
+
"react-confetti": "^6.1.0",
|
|
33
|
+
"react-use": "^17.5.1"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
input: 'src/index.ts',
|
|
7
|
+
output: [
|
|
8
|
+
{
|
|
9
|
+
file: 'dist/index.cjs.js',
|
|
10
|
+
format: 'cjs',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
file: 'dist/index.esm.js',
|
|
14
|
+
format: 'esm',
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
plugins: [
|
|
18
|
+
resolve(),
|
|
19
|
+
commonjs(),
|
|
20
|
+
typescript({ tsconfig: './tsconfig.json' })
|
|
21
|
+
],
|
|
22
|
+
external: ['react', 'react-dom']
|
|
23
|
+
};
|
package/src/badges.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface BadgeConfig {
|
|
2
|
+
id: string;
|
|
3
|
+
icon: string;
|
|
4
|
+
title: string;
|
|
5
|
+
description: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const defaultBadges: BadgeConfig[] = [
|
|
9
|
+
{
|
|
10
|
+
id: 'beginner',
|
|
11
|
+
icon: '/path/to/beginner-icon.png',
|
|
12
|
+
title: 'Beginner',
|
|
13
|
+
description: 'Achieved beginner level',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
id: 'intermediate',
|
|
17
|
+
icon: '/path/to/intermediate-icon.png',
|
|
18
|
+
title: 'Intermediate',
|
|
19
|
+
description: 'Achieved intermediate level',
|
|
20
|
+
},
|
|
21
|
+
// Add more badges as needed
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export { defaultBadges, BadgeConfig };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface AchievementProps {
|
|
4
|
+
metric: number;
|
|
5
|
+
threshold: number;
|
|
6
|
+
onAchieve: () => void;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const Achievement: React.FC<AchievementProps> = ({ metric, threshold, onAchieve, children }) => {
|
|
11
|
+
React.useEffect(() => {
|
|
12
|
+
if (metric >= threshold) {
|
|
13
|
+
onAchieve();
|
|
14
|
+
}
|
|
15
|
+
}, [metric, threshold, onAchieve]);
|
|
16
|
+
|
|
17
|
+
return <div>{children}</div>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default Achievement;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
interface BadgeProps {
|
|
4
|
+
icon: string;
|
|
5
|
+
title: string;
|
|
6
|
+
description: string;
|
|
7
|
+
position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const Badge: React.FC<BadgeProps> = ({ icon, title, description, position = 'top-right' }) => {
|
|
11
|
+
const badgeStyle: React.CSSProperties = {
|
|
12
|
+
position: 'fixed',
|
|
13
|
+
[position.split('-')[0]]: '10px',
|
|
14
|
+
[position.split('-')[1]]: '10px',
|
|
15
|
+
display: 'flex',
|
|
16
|
+
flexDirection: 'column',
|
|
17
|
+
alignItems: 'center',
|
|
18
|
+
backgroundColor: '#fff',
|
|
19
|
+
border: '1px solid #ccc',
|
|
20
|
+
borderRadius: '8px',
|
|
21
|
+
padding: '10px',
|
|
22
|
+
boxShadow: '0 0 10px rgba(0,0,0,0.1)',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div style={badgeStyle}>
|
|
27
|
+
<img src={icon} alt={title} style={{ width: '50px', height: '50px' }} />
|
|
28
|
+
<h4>{title}</h4>
|
|
29
|
+
<p>{description}</p>
|
|
30
|
+
</div>
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default Badge;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import Confetti from 'react-confetti';
|
|
3
|
+
import { useWindowSize } from 'react-use';
|
|
4
|
+
|
|
5
|
+
interface ConfettiWrapperProps {
|
|
6
|
+
show: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const ConfettiWrapper: React.FC<ConfettiWrapperProps> = ({ show }) => {
|
|
10
|
+
const { width, height } = useWindowSize();
|
|
11
|
+
|
|
12
|
+
return show ? <Confetti width={width} height={height} /> : null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default ConfettiWrapper;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
|
2
|
+
import { defaultBadges, BadgeConfig } from '../badges';
|
|
3
|
+
import { levels, LevelConfig } from '../levels';
|
|
4
|
+
import ConfettiWrapper from '../components/ConfettiWrapper';
|
|
5
|
+
import Badge from '../components/Badge';
|
|
6
|
+
|
|
7
|
+
interface AchievementContextProps {
|
|
8
|
+
metric: number;
|
|
9
|
+
setMetric: (value: number) => void;
|
|
10
|
+
badges: BadgeConfig[];
|
|
11
|
+
levels: LevelConfig[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const AchievementContext = createContext<AchievementContextProps | undefined>(undefined);
|
|
15
|
+
|
|
16
|
+
const AchievementProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
|
17
|
+
const [metric, setMetric] = useState(0);
|
|
18
|
+
const [achievedLevels, setAchievedLevels] = useState<number[]>([]);
|
|
19
|
+
const [showConfetti, setShowConfetti] = useState(false);
|
|
20
|
+
|
|
21
|
+
const handleAchieve = (level: number) => {
|
|
22
|
+
if (!achievedLevels.includes(level)) {
|
|
23
|
+
setAchievedLevels([...achievedLevels, level]);
|
|
24
|
+
setShowConfetti(true);
|
|
25
|
+
setTimeout(() => setShowConfetti(false), 5000); // Confetti for 5 seconds
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<AchievementContext.Provider value={{ metric, setMetric, badges: defaultBadges, levels }}>
|
|
31
|
+
{children}
|
|
32
|
+
{levels.map(levelConfig =>
|
|
33
|
+
achievedLevels.includes(levelConfig.level) ? (
|
|
34
|
+
<Badge
|
|
35
|
+
key={levelConfig.level}
|
|
36
|
+
icon={defaultBadges.find(badge => badge.id === levelConfig.badgeId)?.icon || ''}
|
|
37
|
+
title={defaultBadges.find(badge => badge.id === levelConfig.badgeId)?.title || ''}
|
|
38
|
+
description={defaultBadges.find(badge => badge.id === levelConfig.badgeId)?.description || ''}
|
|
39
|
+
/>
|
|
40
|
+
) : null
|
|
41
|
+
)}
|
|
42
|
+
<ConfettiWrapper show={showConfetti} />
|
|
43
|
+
</AchievementContext.Provider>
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const useAchievement = () => {
|
|
48
|
+
const context = useContext(AchievementContext);
|
|
49
|
+
if (context === undefined) {
|
|
50
|
+
throw new Error('useAchievement must be used within an AchievementProvider');
|
|
51
|
+
}
|
|
52
|
+
return context;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { AchievementProvider, useAchievement };
|
package/src/custom.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
declare module 'react-confetti' {
|
|
2
|
+
import { ComponentType } from 'react';
|
|
3
|
+
|
|
4
|
+
interface ConfettiProps {
|
|
5
|
+
width?: number;
|
|
6
|
+
height?: number;
|
|
7
|
+
numberOfPieces?: number;
|
|
8
|
+
recycle?: boolean;
|
|
9
|
+
run?: boolean;
|
|
10
|
+
colors?: string[];
|
|
11
|
+
gravity?: number;
|
|
12
|
+
wind?: number;
|
|
13
|
+
tweenDuration?: number;
|
|
14
|
+
drawShape?: (ctx: CanvasRenderingContext2D) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const Confetti: ComponentType<ConfettiProps>;
|
|
18
|
+
export default Confetti;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare module 'react-use'
|
package/src/index.ts
ADDED
package/src/levels.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface LevelConfig {
|
|
2
|
+
level: number;
|
|
3
|
+
threshold: number;
|
|
4
|
+
badgeId: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const levels: LevelConfig[] = [
|
|
8
|
+
{ level: 1, threshold: 10, badgeId: 'beginner' },
|
|
9
|
+
{ level: 2, threshold: 50, badgeId: 'intermediate' },
|
|
10
|
+
// Add more levels as needed
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
export { levels, LevelConfig };
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
"jsx": "react", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "ES6", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
43
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
44
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
45
|
+
|
|
46
|
+
/* JavaScript Support */
|
|
47
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
48
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
49
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
50
|
+
|
|
51
|
+
/* Emit */
|
|
52
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
53
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
54
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
55
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
56
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
57
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
58
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
59
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
60
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
62
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
63
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
64
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
65
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
66
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
67
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
68
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
69
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
70
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
71
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
72
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
73
|
+
|
|
74
|
+
/* Interop Constraints */
|
|
75
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
76
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
77
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
78
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
79
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
80
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
81
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
82
|
+
|
|
83
|
+
/* Type Checking */
|
|
84
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
85
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
86
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
87
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
88
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
89
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
90
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
91
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
92
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
93
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
94
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
95
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
96
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
97
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
98
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
99
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
100
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
101
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
102
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
103
|
+
|
|
104
|
+
/* Completeness */
|
|
105
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
106
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
107
|
+
},
|
|
108
|
+
"include": ["src"]
|
|
109
|
+
}
|