File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments