Skip to content

Commit dca27b5

Browse files
wontoryclaude
andcommitted
feat(web): add badge state management and URL utility
Add BadgeProvider context for badge customization state and URL constructor for badge API query parameters. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c72bbf1 commit dca27b5

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use client'
2+
3+
import {
4+
createContext,
5+
type Dispatch,
6+
type SetStateAction,
7+
useContext,
8+
useState,
9+
} from 'react'
10+
11+
type BadgeState = {
12+
slug: string
13+
setSlug: Dispatch<SetStateAction<string>>
14+
text: string
15+
setText: Dispatch<SetStateAction<string>>
16+
highlight: boolean
17+
setHighlight: Dispatch<SetStateAction<boolean>>
18+
textColor: string
19+
setTextColor: Dispatch<SetStateAction<string>>
20+
iconColor: string
21+
setIconColor: Dispatch<SetStateAction<string>>
22+
bgColor: string
23+
setBgColor: Dispatch<SetStateAction<string>>
24+
}
25+
26+
const BadgeContext = createContext<BadgeState | null>(null)
27+
28+
export function BadgeProvider({ children }: { children: React.ReactNode }) {
29+
const [slug, setSlug] = useState('')
30+
const [text, setText] = useState('')
31+
const [highlight, setHighlight] = useState(false)
32+
const [textColor, setTextColor] = useState('000000')
33+
const [iconColor, setIconColor] = useState('')
34+
const [bgColor, setBgColor] = useState('')
35+
36+
return (
37+
<BadgeContext.Provider
38+
value={{
39+
slug,
40+
setSlug,
41+
text,
42+
setText,
43+
highlight,
44+
setHighlight,
45+
textColor,
46+
setTextColor,
47+
iconColor,
48+
setIconColor,
49+
bgColor,
50+
setBgColor,
51+
}}
52+
>
53+
{children}
54+
</BadgeContext.Provider>
55+
)
56+
}
57+
58+
export const useBadgeState = () => {
59+
const context = useContext(BadgeContext)
60+
if (!context) {
61+
throw new Error('useBadgeState must be used within a BadgeProvider')
62+
}
63+
return context
64+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const constructBadgeUrl = (
2+
slug: string,
3+
text: string,
4+
highlight: boolean,
5+
textColor: string,
6+
iconColor: string,
7+
bgColor: string,
8+
): string => {
9+
const params = new URLSearchParams()
10+
if (slug) params.append('slug', slug)
11+
if (text) params.append('text', text)
12+
if (highlight) params.append('highlight', 'true')
13+
if (textColor) params.append('textColor', textColor)
14+
if (iconColor) params.append('iconColor', iconColor)
15+
if (bgColor) params.append('bgColor', bgColor)
16+
const queryString = params.toString()
17+
return `/api/badge${queryString ? `?${queryString}` : ''}`
18+
}

0 commit comments

Comments
 (0)