@@ -137,6 +137,45 @@ export const getInitials = (guest: Guest) => {
137137 . toUpperCase ( ) ;
138138} ;
139139
140+ const getSuperscriptNumber = ( num : number ) : string => {
141+ const superscriptDigits = [ '⁰' , '¹' , '²' , '³' , '⁴' , '⁵' , '⁶' , '⁷' , '⁸' , '⁹' ] ;
142+
143+ // Convert number to string and map each digit to its superscript equivalent
144+ return num
145+ . toString ( )
146+ . split ( '' )
147+ . map ( ( digit ) => superscriptDigits [ parseInt ( digit ) ] )
148+ . join ( '' ) ;
149+ } ;
150+
151+ export const useUniqueInitials = ( guest ?: Guest ) => {
152+ const { guests } = useGuests ( ) ;
153+
154+ if ( ! guest ) return undefined ;
155+ const initials = getInitials ( guest ) ;
156+
157+ // Get all guests with the same initials
158+ const guestsWithSameInitials = guests . filter (
159+ ( g ) => getInitials ( g ) === initials && g . name !== guest . name
160+ ) ;
161+
162+ // If no duplicates, return regular initials
163+ if ( guestsWithSameInitials . length === 0 ) {
164+ return initials ;
165+ }
166+
167+ // Sort guests by name to ensure consistent ordering
168+ const sortedGuests = [ guest , ...guestsWithSameInitials ] . sort ( ( a , b ) =>
169+ a . name . localeCompare ( b . name )
170+ ) ;
171+
172+ // Find index of current guest in sorted array (adding 1 to make it 1-based)
173+ const currentIndex = sortedGuests . findIndex ( ( g ) => g . name === guest . name ) + 1 ;
174+
175+ // Return initials with superscript number
176+ return `${ initials } ${ getSuperscriptNumber ( currentIndex ) } ` ;
177+ } ;
178+
140179// Name guests with same name like this:
141180// John Doe, John Doe (2), John Doe (3), John Doe (4)
142181const getNextAvailableName = ( guests : Guest [ ] , guest : Guest ) => {
0 commit comments