Skip to content

Commit 08d258a

Browse files
committed
feat: add unique initials and user list
1 parent 0ce15ff commit 08d258a

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/hooks/useGuests.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
142181
const getNextAvailableName = (guests: Guest[], guest: Guest) => {

src/pages/editor/Editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Canvas } from './components/Canvas';
55
import 'react-indiana-drag-scroll/dist/style.css';
66
import { ColorInfo } from './components/ColorInfo';
77
import { SeatInfo } from './components/SeatInfo';
8-
8+
import { UserListInfo } from './components/UserListInfo';
99
export const Editor = () => {
1010
return (
1111
<Grid>
@@ -14,6 +14,7 @@ export const Editor = () => {
1414
<InfoBoxes>
1515
<ColorInfo />
1616
<SeatInfo />
17+
<UserListInfo />
1718
</InfoBoxes>
1819
</CanvasWindow>
1920

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import styled from 'styled-components';
2+
import { Body } from '../../../components/uikit/Body';
3+
import { Guest } from '../../../hooks/types';
4+
import { useGuests, useUniqueInitials } from '../../../hooks/useGuests';
5+
import { InfoContainer } from './InfoContainer';
6+
7+
export const UserListInfo = () => {
8+
const guests = useGuests((s) => s.guests);
9+
10+
return (
11+
<InfoContainer>
12+
<Body variant="bold">Guests</Body>
13+
<div
14+
style={{ display: 'flex', flexDirection: 'column', gap: '0.125rem' }}
15+
>
16+
{guests.map((guest) => (
17+
<GuestName key={guest.name} guest={guest} />
18+
))}
19+
</div>
20+
</InfoContainer>
21+
);
22+
};
23+
24+
const GuestName = ({ guest }: { guest: Guest }) => {
25+
const initials = useUniqueInitials(guest);
26+
return (
27+
<StyledName>
28+
{guest.name} ({initials})
29+
</StyledName>
30+
);
31+
};
32+
33+
const StyledName = styled.span`
34+
display: flex;
35+
align-items: center;
36+
font-size: 14px;
37+
`;

src/pages/editor/components/table/Seat.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { MouseEventHandler, useState } from 'react';
22
import styled from 'styled-components';
33
import { PaperSvg } from '../../../../components/PaperSvg';
44
import { Seat as SeatType, Side } from '../../../../hooks/types';
5-
import { getInitials, useGuests } from '../../../../hooks/useGuests';
5+
import { useGuests, useUniqueInitials } from '../../../../hooks/useGuests';
66
import { useHighlightedSeats } from '../../../../hooks/useSeatHighlight';
77
import { SEAT_SIZE } from '../config';
88
import { useJesse } from '../Jesse';
@@ -35,6 +35,8 @@ export const Seat = ({ seat }: { seat: SeatType }) => {
3535

3636
const highlight = getHighlight();
3737

38+
const initials = useUniqueInitials(guest);
39+
3840
return (
3941
<StyledSeat
4042
id={seat.id}
@@ -51,7 +53,7 @@ export const Seat = ({ seat }: { seat: SeatType }) => {
5153
height={SEAT_SIZE}
5254
color={highlight.color}
5355
/>
54-
{guest && <GuestName>{getInitials(guest)}</GuestName>}
56+
{guest && <GuestName>{initials}</GuestName>}
5557
{guest && isHovering && <Tooltip side={seat.side}>{guest.name}</Tooltip>}
5658
</StyledSeat>
5759
);

0 commit comments

Comments
 (0)