@molecule/app-reputation-badge-react 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/LICENSE +115 -0
- package/README.md +300 -0
- package/dist/BadgeShelf.d.ts +36 -0
- package/dist/BadgeShelf.d.ts.map +1 -0
- package/dist/BadgeShelf.js +31 -0
- package/dist/ReputationBadge.d.ts +37 -0
- package/dist/ReputationBadge.d.ts.map +1 -0
- package/dist/ReputationBadge.js +70 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/levels.d.ts +33 -0
- package/dist/levels.d.ts.map +1 -0
- package/dist/levels.js +61 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T00:40:56.992Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-reputation-badge-react
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
React reputation/karma surfaces — `<ReputationBadge>` and `<BadgeShelf>` —
|
|
16
|
+
for forum, discussion-board, and social flagships.
|
|
17
|
+
|
|
18
|
+
`<ReputationBadge score={n} />` renders a compact-formatted score ("1.5k")
|
|
19
|
+
plus a tier chip; the tier derives from `score` via `levelForScore` and
|
|
20
|
+
`DEFAULT_THRESHOLDS` (contributor 100, trusted 500, veteran 2000,
|
|
21
|
+
legend 10000) unless you pass `level` or `thresholds`. `variant` is
|
|
22
|
+
`'compact'` (default) or `'full'` (stacked with a "Reputation" caption).
|
|
23
|
+
`<BadgeShelf badges limit onClick />` renders earned-badge icons with a
|
|
24
|
+
trailing `+N` overflow chip (`onClick(null)` signals "expand all").
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { BadgeShelf, ReputationBadge } from '@molecule/app-reputation-badge-react'
|
|
30
|
+
import type { Badge } from '@molecule/app-reputation-badge-react'
|
|
31
|
+
|
|
32
|
+
function ProfileHeader({
|
|
33
|
+
score,
|
|
34
|
+
badges,
|
|
35
|
+
onBadgeClick,
|
|
36
|
+
}: {
|
|
37
|
+
score: number
|
|
38
|
+
badges: Badge[]
|
|
39
|
+
onBadgeClick: (badge: Badge | null) => void
|
|
40
|
+
}) {
|
|
41
|
+
return (
|
|
42
|
+
<div>
|
|
43
|
+
<ReputationBadge score={score} variant="full" />
|
|
44
|
+
<BadgeShelf badges={badges} limit={5} onClick={onBadgeClick} />
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Type
|
|
51
|
+
|
|
52
|
+
`feature`
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm install @molecule/app-reputation-badge-react @molecule/app-react @molecule/app-ui @molecule/app-ui-react react
|
|
58
|
+
npm install -D @types/react
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
### Interfaces
|
|
64
|
+
|
|
65
|
+
#### `Badge`
|
|
66
|
+
|
|
67
|
+
A single earned badge — one icon on the `<BadgeShelf>` row.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface Badge {
|
|
71
|
+
/** Stable identifier (used as React key + click payload). */
|
|
72
|
+
id: string
|
|
73
|
+
/** Display name (translated upstream — passed verbatim). */
|
|
74
|
+
name: string
|
|
75
|
+
/** Optional longer description, shown in the native tooltip. */
|
|
76
|
+
description?: string
|
|
77
|
+
/** Icon glyph or short emoji (rendered inline). Mutually exclusive with `iconSrc`. */
|
|
78
|
+
icon?: string
|
|
79
|
+
/** Image URL for the badge icon. */
|
|
80
|
+
iconSrc?: string
|
|
81
|
+
/** ISO date string the badge was earned (passed back via `onClick`). */
|
|
82
|
+
earnedAt?: string
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### `BadgeShelfProps`
|
|
87
|
+
|
|
88
|
+
Props accepted by the {@link BadgeShelf} component.
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
interface BadgeShelfProps {
|
|
92
|
+
/** Earned badges to display, ordered most-prominent-first. */
|
|
93
|
+
badges: Badge[]
|
|
94
|
+
/**
|
|
95
|
+
* Maximum number of badge icons rendered inline. Any remaining are
|
|
96
|
+
* summarised in a trailing `+N` overflow chip.
|
|
97
|
+
*
|
|
98
|
+
* @default 5
|
|
99
|
+
*/
|
|
100
|
+
limit?: number
|
|
101
|
+
/**
|
|
102
|
+
* Click handler — invoked with the clicked badge (or `null` when the
|
|
103
|
+
* trailing overflow chip is clicked, signalling "expand all").
|
|
104
|
+
*/
|
|
105
|
+
onClick?: (badge: Badge | null) => void
|
|
106
|
+
/** Extra classes merged onto the outer row. */
|
|
107
|
+
className?: string
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### `ReputationBadgeProps`
|
|
112
|
+
|
|
113
|
+
Props accepted by the {@link ReputationBadge} component.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
interface ReputationBadgeProps {
|
|
117
|
+
/** Numeric reputation / karma score. */
|
|
118
|
+
score: number
|
|
119
|
+
/**
|
|
120
|
+
* Explicit reputation level. If omitted, derived from `score` via
|
|
121
|
+
* {@link levelForScore} using `thresholds` (or {@link DEFAULT_THRESHOLDS}).
|
|
122
|
+
*/
|
|
123
|
+
level?: ReputationLevel
|
|
124
|
+
/** Override the default score → level thresholds. */
|
|
125
|
+
thresholds?: ReputationThresholds
|
|
126
|
+
/**
|
|
127
|
+
* Layout variant.
|
|
128
|
+
* - `'compact'` (default) — score number inline with a small level chip.
|
|
129
|
+
* - `'full'` — stacked layout with a "Reputation" caption above the row.
|
|
130
|
+
*/
|
|
131
|
+
variant?: 'compact' | 'full'
|
|
132
|
+
/** Extra classes merged onto the outer wrapper. */
|
|
133
|
+
className?: string
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### `ReputationThresholds`
|
|
138
|
+
|
|
139
|
+
Optional thresholds for deriving a `ReputationLevel` from a numeric score.
|
|
140
|
+
|
|
141
|
+
If a `level` prop isn't passed to `<ReputationBadge>`, the component will
|
|
142
|
+
pick the highest level whose threshold the `score` meets or exceeds.
|
|
143
|
+
|
|
144
|
+
Default thresholds:
|
|
145
|
+
|
|
146
|
+
- `contributor` >= 100
|
|
147
|
+
- `trusted` >= 500
|
|
148
|
+
- `veteran` >= 2000
|
|
149
|
+
- `legend` >= 10000
|
|
150
|
+
- below 100 → `newcomer`
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
interface ReputationThresholds {
|
|
154
|
+
/** Score >= contributor threshold ⇒ `'contributor'`. */
|
|
155
|
+
contributor: number
|
|
156
|
+
/** Score >= trusted threshold ⇒ `'trusted'`. */
|
|
157
|
+
trusted: number
|
|
158
|
+
/** Score >= veteran threshold ⇒ `'veteran'`. */
|
|
159
|
+
veteran: number
|
|
160
|
+
/** Score >= legend threshold ⇒ `'legend'`. */
|
|
161
|
+
legend: number
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Types
|
|
166
|
+
|
|
167
|
+
#### `ReputationLevel`
|
|
168
|
+
|
|
169
|
+
Reputation level identifiers — used to pick a tier color/label.
|
|
170
|
+
|
|
171
|
+
Apps that need finer granularity can pass a `level` string of any value;
|
|
172
|
+
unknown values fall back to `'newcomer'` styling.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
type ReputationLevel = 'newcomer' | 'contributor' | 'trusted' | 'veteran' | 'legend'
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Functions
|
|
179
|
+
|
|
180
|
+
#### `BadgeShelf(props)`
|
|
181
|
+
|
|
182
|
+
Horizontal row of small badge icons earned by a user — used alongside
|
|
183
|
+
`<ReputationBadge>` to surface community achievements on profile
|
|
184
|
+
headers, post bylines, and detail pages.
|
|
185
|
+
|
|
186
|
+
Each icon shows its name in a native `title` tooltip for low-friction
|
|
187
|
+
accessibility. Clicking a badge invokes `onClick(badge)`; clicking the
|
|
188
|
+
trailing `+N` chip invokes `onClick(null)` so the host page can expand
|
|
189
|
+
a full list.
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
function BadgeShelf({
|
|
193
|
+
badges,
|
|
194
|
+
limit = 5,
|
|
195
|
+
onClick,
|
|
196
|
+
className,
|
|
197
|
+
}: BadgeShelfProps): React.JSX.Element | null
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
- `props` — Component props (see {@link BadgeShelfProps}).
|
|
201
|
+
|
|
202
|
+
**Returns:** The rendered shelf, or `null` if no badges are supplied.
|
|
203
|
+
|
|
204
|
+
#### `colorForLevel(level)`
|
|
205
|
+
|
|
206
|
+
Return the semantic Badge color for a given level.
|
|
207
|
+
|
|
208
|
+
Unknown levels fall back to `'secondary'`.
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
function colorForLevel(
|
|
212
|
+
level: ReputationLevel,
|
|
213
|
+
): 'primary' | 'secondary' | 'success' | 'warning' | 'info'
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
- `level` — The reputation level.
|
|
217
|
+
|
|
218
|
+
**Returns:** A badge color name from `@molecule/app-ui-react`.
|
|
219
|
+
|
|
220
|
+
#### `levelForScore(score, thresholds)`
|
|
221
|
+
|
|
222
|
+
Derive a {@link ReputationLevel} from a numeric score.
|
|
223
|
+
|
|
224
|
+
Picks the highest level whose threshold is met or exceeded. Any score
|
|
225
|
+
below `thresholds.contributor` returns `'newcomer'`.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
function levelForScore(score: number, thresholds?: ReputationThresholds): ReputationLevel
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
- `score` — Reputation score (any non-negative number).
|
|
232
|
+
- `thresholds` — Optional override thresholds; defaults to {@link DEFAULT_THRESHOLDS}.
|
|
233
|
+
|
|
234
|
+
**Returns:** The derived reputation level.
|
|
235
|
+
|
|
236
|
+
#### `ReputationBadge(props)`
|
|
237
|
+
|
|
238
|
+
User reputation / karma display.
|
|
239
|
+
|
|
240
|
+
Renders a numeric score paired with a tier chip whose color reflects the
|
|
241
|
+
derived (or explicit) {@link ReputationLevel}. Used by discussion-boards,
|
|
242
|
+
forum, and social-media flagships to surface community standing.
|
|
243
|
+
|
|
244
|
+
Tier color comes from the active ClassMap bond's `cm.badge()` helper, so
|
|
245
|
+
apps swapping styling libraries restyle these chips for free.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
function ReputationBadge({
|
|
249
|
+
score,
|
|
250
|
+
level,
|
|
251
|
+
thresholds = DEFAULT_THRESHOLDS,
|
|
252
|
+
variant = 'compact',
|
|
253
|
+
className,
|
|
254
|
+
}: ReputationBadgeProps): React.JSX.Element
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
- `props` — Component props (see {@link ReputationBadgeProps}).
|
|
258
|
+
|
|
259
|
+
**Returns:** The rendered reputation badge element.
|
|
260
|
+
|
|
261
|
+
### Constants
|
|
262
|
+
|
|
263
|
+
#### `DEFAULT_THRESHOLDS`
|
|
264
|
+
|
|
265
|
+
Default thresholds used when no `thresholds` prop is supplied.
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
const DEFAULT_THRESHOLDS: ReputationThresholds
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Injection Notes
|
|
272
|
+
|
|
273
|
+
### Requirements
|
|
274
|
+
|
|
275
|
+
Peer dependencies:
|
|
276
|
+
|
|
277
|
+
- `@molecule/app-react` ^1.0.0
|
|
278
|
+
- `@molecule/app-ui` ^1.0.0
|
|
279
|
+
- `@molecule/app-ui-react` ^1.0.0
|
|
280
|
+
- `react` ^18.0.0 || ^19.0.0
|
|
281
|
+
|
|
282
|
+
### Runtime Dependencies
|
|
283
|
+
|
|
284
|
+
- `@molecule/app-react`
|
|
285
|
+
- `@molecule/app-ui`
|
|
286
|
+
- `@molecule/app-ui-react`
|
|
287
|
+
- `react`
|
|
288
|
+
|
|
289
|
+
- Both components throw unless rendered inside `<I18nProvider>` (from
|
|
290
|
+
`@molecule/app-react`) with a ClassMap bonded via `setClassMap()`.
|
|
291
|
+
- This package exports a `Badge` TYPE (earned-badge data shape) — distinct
|
|
292
|
+
from the `Badge` COMPONENT in `@molecule/app-ui-react`; alias one when
|
|
293
|
+
importing both in a file.
|
|
294
|
+
- Server-side score/badge issuance pairs with `@molecule/api-reputation`.
|
|
295
|
+
- Translations live in the companion `@molecule/app-locales-reputation-badge`
|
|
296
|
+
bond (registered).
|
|
297
|
+
|
|
298
|
+
## Translations
|
|
299
|
+
|
|
300
|
+
Translation strings are provided by `@molecule/app-locales-reputation-badge`.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { Badge } from './types.js';
|
|
3
|
+
/** Props accepted by the {@link BadgeShelf} component. */
|
|
4
|
+
export interface BadgeShelfProps {
|
|
5
|
+
/** Earned badges to display, ordered most-prominent-first. */
|
|
6
|
+
badges: Badge[];
|
|
7
|
+
/**
|
|
8
|
+
* Maximum number of badge icons rendered inline. Any remaining are
|
|
9
|
+
* summarised in a trailing `+N` overflow chip.
|
|
10
|
+
*
|
|
11
|
+
* @default 5
|
|
12
|
+
*/
|
|
13
|
+
limit?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Click handler — invoked with the clicked badge (or `null` when the
|
|
16
|
+
* trailing overflow chip is clicked, signalling "expand all").
|
|
17
|
+
*/
|
|
18
|
+
onClick?: (badge: Badge | null) => void;
|
|
19
|
+
/** Extra classes merged onto the outer row. */
|
|
20
|
+
className?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Horizontal row of small badge icons earned by a user — used alongside
|
|
24
|
+
* `<ReputationBadge>` to surface community achievements on profile
|
|
25
|
+
* headers, post bylines, and detail pages.
|
|
26
|
+
*
|
|
27
|
+
* Each icon shows its name in a native `title` tooltip for low-friction
|
|
28
|
+
* accessibility. Clicking a badge invokes `onClick(badge)`; clicking the
|
|
29
|
+
* trailing `+N` chip invokes `onClick(null)` so the host page can expand
|
|
30
|
+
* a full list.
|
|
31
|
+
*
|
|
32
|
+
* @param props - Component props (see {@link BadgeShelfProps}).
|
|
33
|
+
* @returns The rendered shelf, or `null` if no badges are supplied.
|
|
34
|
+
*/
|
|
35
|
+
export declare function BadgeShelf({ badges, limit, onClick, className, }: BadgeShelfProps): React.JSX.Element | null;
|
|
36
|
+
//# sourceMappingURL=BadgeShelf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BadgeShelf.d.ts","sourceRoot":"","sources":["../src/BadgeShelf.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAK9B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC,0DAA0D;AAC1D,MAAM,WAAW,eAAe;IAC9B,8DAA8D;IAC9D,MAAM,EAAE,KAAK,EAAE,CAAA;IACf;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,CAAA;IACvC,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,EACzB,MAAM,EACN,KAAS,EACT,OAAO,EACP,SAAS,GACV,EAAE,eAAe,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,CAwG5C"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useTranslation } from '@molecule/app-react';
|
|
3
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
4
|
+
/**
|
|
5
|
+
* Horizontal row of small badge icons earned by a user — used alongside
|
|
6
|
+
* `<ReputationBadge>` to surface community achievements on profile
|
|
7
|
+
* headers, post bylines, and detail pages.
|
|
8
|
+
*
|
|
9
|
+
* Each icon shows its name in a native `title` tooltip for low-friction
|
|
10
|
+
* accessibility. Clicking a badge invokes `onClick(badge)`; clicking the
|
|
11
|
+
* trailing `+N` chip invokes `onClick(null)` so the host page can expand
|
|
12
|
+
* a full list.
|
|
13
|
+
*
|
|
14
|
+
* @param props - Component props (see {@link BadgeShelfProps}).
|
|
15
|
+
* @returns The rendered shelf, or `null` if no badges are supplied.
|
|
16
|
+
*/
|
|
17
|
+
export function BadgeShelf({ badges, limit = 5, onClick, className, }) {
|
|
18
|
+
const cm = getClassMap();
|
|
19
|
+
const { t } = useTranslation();
|
|
20
|
+
if (!badges || badges.length === 0)
|
|
21
|
+
return null;
|
|
22
|
+
const visible = badges.slice(0, limit);
|
|
23
|
+
const overflow = Math.max(0, badges.length - visible.length);
|
|
24
|
+
const shelfLabel = t('badgeShelf.aria', { count: badges.length }, { defaultValue: '{{count}} badges earned' });
|
|
25
|
+
return (_jsxs("ul", { className: cm.cn(cm.flex({ align: 'center', gap: 'xs' }), className), "data-mol-id": "badge-shelf", "aria-label": shelfLabel, children: [visible.map((badge) => {
|
|
26
|
+
const tooltip = badge.description ? `${badge.name} — ${badge.description}` : badge.name;
|
|
27
|
+
const interactive = typeof onClick === 'function';
|
|
28
|
+
const content = badge.iconSrc ? (_jsx("img", { src: badge.iconSrc, alt: "", className: cm.cn(cm.shrink0), style: { width: 24, height: 24 }, "data-mol-id": "badge-shelf-icon-img" })) : (_jsx("span", { "aria-hidden": "true", "data-mol-id": "badge-shelf-icon-glyph", children: badge.icon ?? '*' }));
|
|
29
|
+
return (_jsx("li", { className: cm.cn(cm.flex({ align: 'center', justify: 'center' })), "data-mol-id": "badge-shelf-item", "data-badge-id": badge.id, children: interactive ? (_jsx("button", { type: "button", className: cm.cn(cm.flex({ align: 'center', justify: 'center' }), cm.roundedFull), onClick: () => onClick?.(badge), title: tooltip, "aria-label": badge.name, "data-mol-id": "badge-shelf-button", children: content })) : (_jsx("span", { title: tooltip, "aria-label": badge.name, children: content })) }, badge.id));
|
|
30
|
+
}), overflow > 0 && (_jsx("li", { className: cm.cn(cm.flex({ align: 'center', justify: 'center' })), "data-mol-id": "badge-shelf-overflow", children: typeof onClick === 'function' ? (_jsxs("button", { type: "button", className: cm.cn(cm.flex({ align: 'center', justify: 'center' }), cm.roundedFull, cm.textSize('xs'), cm.fontWeight('semibold')), onClick: () => onClick(null), "aria-label": t('badgeShelf.overflow.aria', { count: overflow }, { defaultValue: 'Show {{count}} more badges' }), "data-mol-id": "badge-shelf-overflow-button", children: ["+", overflow] })) : (_jsxs("span", { className: cm.cn(cm.textSize('xs'), cm.fontWeight('semibold')), "aria-label": t('badgeShelf.overflow.aria', { count: overflow }, { defaultValue: 'Show {{count}} more badges' }), children: ["+", overflow] })) }))] }));
|
|
31
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { ReputationLevel, ReputationThresholds } from './types.js';
|
|
3
|
+
/** Props accepted by the {@link ReputationBadge} component. */
|
|
4
|
+
export interface ReputationBadgeProps {
|
|
5
|
+
/** Numeric reputation / karma score. */
|
|
6
|
+
score: number;
|
|
7
|
+
/**
|
|
8
|
+
* Explicit reputation level. If omitted, derived from `score` via
|
|
9
|
+
* {@link levelForScore} using `thresholds` (or {@link DEFAULT_THRESHOLDS}).
|
|
10
|
+
*/
|
|
11
|
+
level?: ReputationLevel;
|
|
12
|
+
/** Override the default score → level thresholds. */
|
|
13
|
+
thresholds?: ReputationThresholds;
|
|
14
|
+
/**
|
|
15
|
+
* Layout variant.
|
|
16
|
+
* - `'compact'` (default) — score number inline with a small level chip.
|
|
17
|
+
* - `'full'` — stacked layout with a "Reputation" caption above the row.
|
|
18
|
+
*/
|
|
19
|
+
variant?: 'compact' | 'full';
|
|
20
|
+
/** Extra classes merged onto the outer wrapper. */
|
|
21
|
+
className?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* User reputation / karma display.
|
|
25
|
+
*
|
|
26
|
+
* Renders a numeric score paired with a tier chip whose color reflects the
|
|
27
|
+
* derived (or explicit) {@link ReputationLevel}. Used by discussion-boards,
|
|
28
|
+
* forum, and social-media flagships to surface community standing.
|
|
29
|
+
*
|
|
30
|
+
* Tier color comes from the active ClassMap bond's `cm.badge()` helper, so
|
|
31
|
+
* apps swapping styling libraries restyle these chips for free.
|
|
32
|
+
*
|
|
33
|
+
* @param props - Component props (see {@link ReputationBadgeProps}).
|
|
34
|
+
* @returns The rendered reputation badge element.
|
|
35
|
+
*/
|
|
36
|
+
export declare function ReputationBadge({ score, level, thresholds, variant, className, }: ReputationBadgeProps): React.JSX.Element;
|
|
37
|
+
//# sourceMappingURL=ReputationBadge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReputationBadge.d.ts","sourceRoot":"","sources":["../src/ReputationBadge.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAO9B,OAAO,KAAK,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAEvE,+DAA+D;AAC/D,MAAM,WAAW,oBAAoB;IACnC,wCAAwC;IACxC,KAAK,EAAE,MAAM,CAAA;IACb;;;OAGG;IACH,KAAK,CAAC,EAAE,eAAe,CAAA;IACvB,qDAAqD;IACrD,UAAU,CAAC,EAAE,oBAAoB,CAAA;IACjC;;;;OAIG;IACH,OAAO,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;IAC5B,mDAAmD;IACnD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,KAAK,EACL,UAA+B,EAC/B,OAAmB,EACnB,SAAS,GACV,EAAE,oBAAoB,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CA+D1C"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useTranslation } from '@molecule/app-react';
|
|
3
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
4
|
+
import { Badge } from '@molecule/app-ui-react';
|
|
5
|
+
import { colorForLevel, DEFAULT_THRESHOLDS, levelForScore } from './levels.js';
|
|
6
|
+
/**
|
|
7
|
+
* User reputation / karma display.
|
|
8
|
+
*
|
|
9
|
+
* Renders a numeric score paired with a tier chip whose color reflects the
|
|
10
|
+
* derived (or explicit) {@link ReputationLevel}. Used by discussion-boards,
|
|
11
|
+
* forum, and social-media flagships to surface community standing.
|
|
12
|
+
*
|
|
13
|
+
* Tier color comes from the active ClassMap bond's `cm.badge()` helper, so
|
|
14
|
+
* apps swapping styling libraries restyle these chips for free.
|
|
15
|
+
*
|
|
16
|
+
* @param props - Component props (see {@link ReputationBadgeProps}).
|
|
17
|
+
* @returns The rendered reputation badge element.
|
|
18
|
+
*/
|
|
19
|
+
export function ReputationBadge({ score, level, thresholds = DEFAULT_THRESHOLDS, variant = 'compact', className, }) {
|
|
20
|
+
const cm = getClassMap();
|
|
21
|
+
const { t } = useTranslation();
|
|
22
|
+
const resolvedLevel = level ?? levelForScore(score, thresholds);
|
|
23
|
+
const color = colorForLevel(resolvedLevel);
|
|
24
|
+
const levelLabel = t(`reputationBadge.level.${resolvedLevel}`, {}, {
|
|
25
|
+
defaultValue: {
|
|
26
|
+
newcomer: 'Newcomer',
|
|
27
|
+
contributor: 'Contributor',
|
|
28
|
+
trusted: 'Trusted',
|
|
29
|
+
veteran: 'Veteran',
|
|
30
|
+
legend: 'Legend',
|
|
31
|
+
}[resolvedLevel],
|
|
32
|
+
});
|
|
33
|
+
const formattedScore = formatScore(score);
|
|
34
|
+
const ariaLabel = t('reputationBadge.aria', { score: formattedScore, level: levelLabel }, { defaultValue: '{{score}} reputation, {{level}}' });
|
|
35
|
+
if (variant === 'full') {
|
|
36
|
+
return (_jsxs("div", { className: cm.cn(cm.stack(1), className), "data-mol-id": "reputation-badge", "aria-label": ariaLabel, children: [_jsx("span", { className: cm.cn(cm.textSize('xs'), cm.fontWeight('medium')), children: t('reputationBadge.caption', {}, { defaultValue: 'Reputation' }) }), _jsxs("div", { className: cm.flex({ align: 'center', gap: 'sm' }), "data-mol-id": "reputation-badge-row", children: [_jsx("span", { className: cm.cn(cm.textSize('lg'), cm.fontWeight('bold')), "data-mol-id": "reputation-badge-score", children: formattedScore }), _jsx(Badge, { color: color, children: levelLabel })] })] }));
|
|
37
|
+
}
|
|
38
|
+
return (_jsxs("span", { className: cm.cn(cm.flex({ align: 'center', gap: 'xs' }), className), "data-mol-id": "reputation-badge", "aria-label": ariaLabel, children: [_jsx("span", { className: cm.fontWeight('semibold'), "data-mol-id": "reputation-badge-score", children: formattedScore }), _jsx(Badge, { color: color, children: levelLabel })] }));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Compact-thousands formatter — keeps the badge readable at large scores.
|
|
42
|
+
*
|
|
43
|
+
* Examples: `42` → `"42"`, `1500` → `"1.5k"`, `12300` → `"12.3k"`,
|
|
44
|
+
* `1_250_000` → `"1.3m"`.
|
|
45
|
+
*
|
|
46
|
+
* @param score - The score to format.
|
|
47
|
+
* @returns A short human-readable string.
|
|
48
|
+
*/
|
|
49
|
+
function formatScore(score) {
|
|
50
|
+
if (!Number.isFinite(score))
|
|
51
|
+
return '0';
|
|
52
|
+
const sign = score < 0 ? '-' : '';
|
|
53
|
+
const abs = Math.abs(score);
|
|
54
|
+
if (abs >= 1_000_000) {
|
|
55
|
+
return `${sign}${trimZero((abs / 1_000_000).toFixed(1))}m`;
|
|
56
|
+
}
|
|
57
|
+
if (abs >= 1_000) {
|
|
58
|
+
return `${sign}${trimZero((abs / 1_000).toFixed(1))}k`;
|
|
59
|
+
}
|
|
60
|
+
return `${sign}${Math.round(abs)}`;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Drop a trailing `.0` from a fixed-precision number string.
|
|
64
|
+
*
|
|
65
|
+
* @param s - The fixed-precision string (e.g. `"1.0"`).
|
|
66
|
+
* @returns The cleaned string (e.g. `"1"`).
|
|
67
|
+
*/
|
|
68
|
+
function trimZero(s) {
|
|
69
|
+
return s.endsWith('.0') ? s.slice(0, -2) : s;
|
|
70
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React reputation/karma surfaces — `<ReputationBadge>` and `<BadgeShelf>` —
|
|
3
|
+
* for forum, discussion-board, and social flagships.
|
|
4
|
+
*
|
|
5
|
+
* `<ReputationBadge score={n} />` renders a compact-formatted score ("1.5k")
|
|
6
|
+
* plus a tier chip; the tier derives from `score` via `levelForScore` and
|
|
7
|
+
* `DEFAULT_THRESHOLDS` (contributor 100, trusted 500, veteran 2000,
|
|
8
|
+
* legend 10000) unless you pass `level` or `thresholds`. `variant` is
|
|
9
|
+
* `'compact'` (default) or `'full'` (stacked with a "Reputation" caption).
|
|
10
|
+
* `<BadgeShelf badges limit onClick />` renders earned-badge icons with a
|
|
11
|
+
* trailing `+N` overflow chip (`onClick(null)` signals "expand all").
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { BadgeShelf, ReputationBadge } from '@molecule/app-reputation-badge-react'
|
|
16
|
+
* import type { Badge } from '@molecule/app-reputation-badge-react'
|
|
17
|
+
*
|
|
18
|
+
* function ProfileHeader({ score, badges, onBadgeClick }: {
|
|
19
|
+
* score: number
|
|
20
|
+
* badges: Badge[]
|
|
21
|
+
* onBadgeClick: (badge: Badge | null) => void
|
|
22
|
+
* }) {
|
|
23
|
+
* return (
|
|
24
|
+
* <div>
|
|
25
|
+
* <ReputationBadge score={score} variant="full" />
|
|
26
|
+
* <BadgeShelf badges={badges} limit={5} onClick={onBadgeClick} />
|
|
27
|
+
* </div>
|
|
28
|
+
* )
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - Both components throw unless rendered inside `<I18nProvider>` (from
|
|
34
|
+
* `@molecule/app-react`) with a ClassMap bonded via `setClassMap()`.
|
|
35
|
+
* - This package exports a `Badge` TYPE (earned-badge data shape) — distinct
|
|
36
|
+
* from the `Badge` COMPONENT in `@molecule/app-ui-react`; alias one when
|
|
37
|
+
* importing both in a file.
|
|
38
|
+
* - Server-side score/badge issuance pairs with `@molecule/api-reputation`.
|
|
39
|
+
* - Translations live in the companion `@molecule/app-locales-reputation-badge`
|
|
40
|
+
* bond (registered).
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
export * from './BadgeShelf.js';
|
|
45
|
+
export * from './levels.js';
|
|
46
|
+
export * from './ReputationBadge.js';
|
|
47
|
+
export * from './types.js';
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AACpC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React reputation/karma surfaces — `<ReputationBadge>` and `<BadgeShelf>` —
|
|
3
|
+
* for forum, discussion-board, and social flagships.
|
|
4
|
+
*
|
|
5
|
+
* `<ReputationBadge score={n} />` renders a compact-formatted score ("1.5k")
|
|
6
|
+
* plus a tier chip; the tier derives from `score` via `levelForScore` and
|
|
7
|
+
* `DEFAULT_THRESHOLDS` (contributor 100, trusted 500, veteran 2000,
|
|
8
|
+
* legend 10000) unless you pass `level` or `thresholds`. `variant` is
|
|
9
|
+
* `'compact'` (default) or `'full'` (stacked with a "Reputation" caption).
|
|
10
|
+
* `<BadgeShelf badges limit onClick />` renders earned-badge icons with a
|
|
11
|
+
* trailing `+N` overflow chip (`onClick(null)` signals "expand all").
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* import { BadgeShelf, ReputationBadge } from '@molecule/app-reputation-badge-react'
|
|
16
|
+
* import type { Badge } from '@molecule/app-reputation-badge-react'
|
|
17
|
+
*
|
|
18
|
+
* function ProfileHeader({ score, badges, onBadgeClick }: {
|
|
19
|
+
* score: number
|
|
20
|
+
* badges: Badge[]
|
|
21
|
+
* onBadgeClick: (badge: Badge | null) => void
|
|
22
|
+
* }) {
|
|
23
|
+
* return (
|
|
24
|
+
* <div>
|
|
25
|
+
* <ReputationBadge score={score} variant="full" />
|
|
26
|
+
* <BadgeShelf badges={badges} limit={5} onClick={onBadgeClick} />
|
|
27
|
+
* </div>
|
|
28
|
+
* )
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - Both components throw unless rendered inside `<I18nProvider>` (from
|
|
34
|
+
* `@molecule/app-react`) with a ClassMap bonded via `setClassMap()`.
|
|
35
|
+
* - This package exports a `Badge` TYPE (earned-badge data shape) — distinct
|
|
36
|
+
* from the `Badge` COMPONENT in `@molecule/app-ui-react`; alias one when
|
|
37
|
+
* importing both in a file.
|
|
38
|
+
* - Server-side score/badge issuance pairs with `@molecule/api-reputation`.
|
|
39
|
+
* - Translations live in the companion `@molecule/app-locales-reputation-badge`
|
|
40
|
+
* bond (registered).
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
export * from './BadgeShelf.js';
|
|
45
|
+
export * from './levels.js';
|
|
46
|
+
export * from './ReputationBadge.js';
|
|
47
|
+
export * from './types.js';
|
package/dist/levels.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for mapping reputation scores to levels and tier colors.
|
|
3
|
+
*
|
|
4
|
+
* Kept framework-free so they're trivially unit-testable.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { ReputationLevel, ReputationThresholds } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Default thresholds used when no `thresholds` prop is supplied.
|
|
11
|
+
*/
|
|
12
|
+
export declare const DEFAULT_THRESHOLDS: ReputationThresholds;
|
|
13
|
+
/**
|
|
14
|
+
* Derive a {@link ReputationLevel} from a numeric score.
|
|
15
|
+
*
|
|
16
|
+
* Picks the highest level whose threshold is met or exceeded. Any score
|
|
17
|
+
* below `thresholds.contributor` returns `'newcomer'`.
|
|
18
|
+
*
|
|
19
|
+
* @param score - Reputation score (any non-negative number).
|
|
20
|
+
* @param thresholds - Optional override thresholds; defaults to {@link DEFAULT_THRESHOLDS}.
|
|
21
|
+
* @returns The derived reputation level.
|
|
22
|
+
*/
|
|
23
|
+
export declare function levelForScore(score: number, thresholds?: ReputationThresholds): ReputationLevel;
|
|
24
|
+
/**
|
|
25
|
+
* Return the semantic Badge color for a given level.
|
|
26
|
+
*
|
|
27
|
+
* Unknown levels fall back to `'secondary'`.
|
|
28
|
+
*
|
|
29
|
+
* @param level - The reputation level.
|
|
30
|
+
* @returns A badge color name from `@molecule/app-ui-react`.
|
|
31
|
+
*/
|
|
32
|
+
export declare function colorForLevel(level: ReputationLevel): 'primary' | 'secondary' | 'success' | 'warning' | 'info';
|
|
33
|
+
//# sourceMappingURL=levels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"levels.d.ts","sourceRoot":"","sources":["../src/levels.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAEvE;;GAEG;AACH,eAAO,MAAM,kBAAkB,EAAE,oBAKhC,CAAA;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,UAAU,GAAE,oBAAyC,GACpD,eAAe,CAMjB;AAmBD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,eAAe,GACrB,SAAS,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,GAAG,MAAM,CAE1D"}
|
package/dist/levels.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for mapping reputation scores to levels and tier colors.
|
|
3
|
+
*
|
|
4
|
+
* Kept framework-free so they're trivially unit-testable.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Default thresholds used when no `thresholds` prop is supplied.
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_THRESHOLDS = {
|
|
12
|
+
contributor: 100,
|
|
13
|
+
trusted: 500,
|
|
14
|
+
veteran: 2000,
|
|
15
|
+
legend: 10000,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Derive a {@link ReputationLevel} from a numeric score.
|
|
19
|
+
*
|
|
20
|
+
* Picks the highest level whose threshold is met or exceeded. Any score
|
|
21
|
+
* below `thresholds.contributor` returns `'newcomer'`.
|
|
22
|
+
*
|
|
23
|
+
* @param score - Reputation score (any non-negative number).
|
|
24
|
+
* @param thresholds - Optional override thresholds; defaults to {@link DEFAULT_THRESHOLDS}.
|
|
25
|
+
* @returns The derived reputation level.
|
|
26
|
+
*/
|
|
27
|
+
export function levelForScore(score, thresholds = DEFAULT_THRESHOLDS) {
|
|
28
|
+
if (score >= thresholds.legend)
|
|
29
|
+
return 'legend';
|
|
30
|
+
if (score >= thresholds.veteran)
|
|
31
|
+
return 'veteran';
|
|
32
|
+
if (score >= thresholds.trusted)
|
|
33
|
+
return 'trusted';
|
|
34
|
+
if (score >= thresholds.contributor)
|
|
35
|
+
return 'contributor';
|
|
36
|
+
return 'newcomer';
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Maps a {@link ReputationLevel} to the semantic Badge color used by the
|
|
40
|
+
* shared `<Badge>` component (`@molecule/app-ui-react`).
|
|
41
|
+
*
|
|
42
|
+
* Apps that swap the ClassMap bond automatically restyle these chips.
|
|
43
|
+
*/
|
|
44
|
+
const LEVEL_TO_COLOR = {
|
|
45
|
+
newcomer: 'secondary',
|
|
46
|
+
contributor: 'info',
|
|
47
|
+
trusted: 'primary',
|
|
48
|
+
veteran: 'success',
|
|
49
|
+
legend: 'warning',
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Return the semantic Badge color for a given level.
|
|
53
|
+
*
|
|
54
|
+
* Unknown levels fall back to `'secondary'`.
|
|
55
|
+
*
|
|
56
|
+
* @param level - The reputation level.
|
|
57
|
+
* @returns A badge color name from `@molecule/app-ui-react`.
|
|
58
|
+
*/
|
|
59
|
+
export function colorForLevel(level) {
|
|
60
|
+
return LEVEL_TO_COLOR[level] ?? 'secondary';
|
|
61
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for the reputation-badge-react feature.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Reputation level identifiers — used to pick a tier color/label.
|
|
8
|
+
*
|
|
9
|
+
* Apps that need finer granularity can pass a `level` string of any value;
|
|
10
|
+
* unknown values fall back to `'newcomer'` styling.
|
|
11
|
+
*/
|
|
12
|
+
export type ReputationLevel = 'newcomer' | 'contributor' | 'trusted' | 'veteran' | 'legend';
|
|
13
|
+
/**
|
|
14
|
+
* Optional thresholds for deriving a `ReputationLevel` from a numeric score.
|
|
15
|
+
*
|
|
16
|
+
* If a `level` prop isn't passed to `<ReputationBadge>`, the component will
|
|
17
|
+
* pick the highest level whose threshold the `score` meets or exceeds.
|
|
18
|
+
*
|
|
19
|
+
* Default thresholds:
|
|
20
|
+
* - `contributor` >= 100
|
|
21
|
+
* - `trusted` >= 500
|
|
22
|
+
* - `veteran` >= 2000
|
|
23
|
+
* - `legend` >= 10000
|
|
24
|
+
* - below 100 → `newcomer`
|
|
25
|
+
*/
|
|
26
|
+
export interface ReputationThresholds {
|
|
27
|
+
/** Score >= contributor threshold ⇒ `'contributor'`. */
|
|
28
|
+
contributor: number;
|
|
29
|
+
/** Score >= trusted threshold ⇒ `'trusted'`. */
|
|
30
|
+
trusted: number;
|
|
31
|
+
/** Score >= veteran threshold ⇒ `'veteran'`. */
|
|
32
|
+
veteran: number;
|
|
33
|
+
/** Score >= legend threshold ⇒ `'legend'`. */
|
|
34
|
+
legend: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A single earned badge — one icon on the `<BadgeShelf>` row.
|
|
38
|
+
*/
|
|
39
|
+
export interface Badge {
|
|
40
|
+
/** Stable identifier (used as React key + click payload). */
|
|
41
|
+
id: string;
|
|
42
|
+
/** Display name (translated upstream — passed verbatim). */
|
|
43
|
+
name: string;
|
|
44
|
+
/** Optional longer description, shown in the native tooltip. */
|
|
45
|
+
description?: string;
|
|
46
|
+
/** Icon glyph or short emoji (rendered inline). Mutually exclusive with `iconSrc`. */
|
|
47
|
+
icon?: string;
|
|
48
|
+
/** Image URL for the badge icon. */
|
|
49
|
+
iconSrc?: string;
|
|
50
|
+
/** ISO date string the badge was earned (passed back via `onClick`). */
|
|
51
|
+
earnedAt?: string;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,aAAa,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,CAAA;AAE3F;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,oBAAoB;IACnC,wDAAwD;IACxD,WAAW,EAAE,MAAM,CAAA;IACnB,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAA;IACf,gDAAgD;IAChD,OAAO,EAAE,MAAM,CAAA;IACf,8CAA8C;IAC9C,MAAM,EAAE,MAAM,CAAA;CACf;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,6DAA6D;IAC7D,EAAE,EAAE,MAAM,CAAA;IACV,4DAA4D;IAC5D,IAAI,EAAE,MAAM,CAAA;IACZ,gEAAgE;IAChE,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,sFAAsF;IACtF,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,oCAAoC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-reputation-badge-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "User reputation/karma badge with score, level chip, and earned-badges shelf",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [
|
|
24
|
+
"molecule",
|
|
25
|
+
"reputation",
|
|
26
|
+
"badge",
|
|
27
|
+
"karma",
|
|
28
|
+
"react"
|
|
29
|
+
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@molecule/app-react": "^1.0.0",
|
|
33
|
+
"@molecule/app-ui": "^1.0.0",
|
|
34
|
+
"@molecule/app-ui-react": "^1.0.0",
|
|
35
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@molecule/app-i18n": "1.0.0",
|
|
39
|
+
"@molecule/app-react": "1.0.0",
|
|
40
|
+
"@molecule/app-ui": "1.0.0",
|
|
41
|
+
"@molecule/app-ui-react": "1.0.0",
|
|
42
|
+
"@testing-library/react": "16.3.2",
|
|
43
|
+
"@types/node": "26.1.2",
|
|
44
|
+
"@types/react": "19.2.17",
|
|
45
|
+
"jsdom": "30.0.1",
|
|
46
|
+
"react": "19.2.8",
|
|
47
|
+
"react-dom": "19.2.8",
|
|
48
|
+
"typescript": "6.0.3",
|
|
49
|
+
"vitest": "4.1.10"
|
|
50
|
+
}
|
|
51
|
+
}
|