Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add names characters limit and truncate #1754

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CardThemed,
SqBadge,
} from '@genshin-optimizer/common/ui'
import { objKeyMap } from '@genshin-optimizer/common/util'
import { objKeyMap, truncateString } from '@genshin-optimizer/common/util'
import {
allArtifactSlotKeys,
charKeyToLocCharKey,
Expand Down Expand Up @@ -277,7 +277,7 @@ function InTeam() {
<CardThemed key={teamCharId} bgt="light">
<CardContent>
<Box sx={{ display: 'flex', gap: 1, alignItems: 'center' }}>
<Typography>{name}</Typography>
<Typography>{truncateString(name, 100)}</Typography>
<BootstrapTooltip
title={<Typography>{description}</Typography>}
>
Expand Down
12 changes: 9 additions & 3 deletions apps/frontend/src/app/PageTeam/BuildDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DropdownButtonProps } from '@genshin-optimizer/common/ui'
import { DropdownButton, SqBadge } from '@genshin-optimizer/common/ui'
import { truncateString } from '@genshin-optimizer/common/util'
import type { LoadoutDatum } from '@genshin-optimizer/gi/db'
import { useDatabase } from '@genshin-optimizer/gi/db-ui'
import CheckroomIcon from '@mui/icons-material/Checkroom'
Expand All @@ -26,7 +27,12 @@ export default function BuildDropdown({
startIcon={<CheckroomIcon />}
title={
<Box sx={{ display: 'flex', gap: 1, flexWrap: 'wrap' }}>
<span>{database.teams.getActiveBuildName(loadoutDatum)}</span>
<span>
{truncateString(
database.teams.getActiveBuildName(loadoutDatum),
50
)}
</span>
{buildType === 'tc' && <SqBadge color="success">TC</SqBadge>}
</Box>
}
Expand All @@ -50,7 +56,7 @@ export default function BuildDropdown({
}
sx={{ display: 'flex', gap: 1 }}
>
{name}
{truncateString(name, 50)}
</MenuItem>
)
})}
Expand All @@ -66,7 +72,7 @@ export default function BuildDropdown({
}
sx={{ display: 'flex', gap: 1 }}
>
<span>{name}</span>
<span>{truncateString(name, 50)}</span>
<SqBadge color="success">TC</SqBadge>
</MenuItem>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ function BuildEditor({
<TextField
fullWidth
label="Build Name"
placeholder="Build Name"
placeholder="Build Name (max 100 characters)"
value={name}
onChange={(e) => setName(e.target.value)}
inputProps={{ maxLength: 100 }}
/>
<TextField
fullWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ function BuildTcEditor({
<TextField
fullWidth
label="Build Name"
placeholder="Build Name"
placeholder="Build Name (max 100 characters)"
value={name}
onChange={(e) => setName(e.target.value)}
inputProps={{ maxLength: 100 }}
/>
<TextField
fullWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { LoadoutDropdown } from '../LoadoutDropdown'
import { BuildEquipped } from './Build/BuildEquipped'
import BuildReal from './Build/BuildReal'
import BuildTc from './Build/BuildTc'
import { truncateString } from '@genshin-optimizer/common/util'
// TODO: Translation
const columns = { xs: 1, sm: 1, md: 2, lg: 2 }
export default function LoadoutSettingElement() {
Expand Down Expand Up @@ -101,13 +102,18 @@ export default function LoadoutSettingElement() {
onClick={() => setOpen((o) => !o)}
>
<Typography sx={{ display: 'flex', gap: 1, alignItems: 'center' }}>
<strong>{teamChar.name}</strong>
<strong>{truncateString(teamChar.name, 100)}</strong>
<SqBadge
color="success"
sx={{ display: 'flex', gap: 1, alignItems: 'center' }}
>
<CheckroomIcon />
<span>{database.teams.getActiveBuildName(loadoutDatum)}</span>
<span>
{truncateString(
database.teams.getActiveBuildName(loadoutDatum),
50
)}
</span>
</SqBadge>
<SqBadge color={buildIds.length ? 'primary' : 'secondary'}>
{buildIds.length} Builds
Expand Down Expand Up @@ -154,9 +160,10 @@ export default function LoadoutSettingElement() {
<TextField
fullWidth
label="Loadout Name"
placeholder="Loadout Name"
placeholder="Loadout Name (max 300 characters)"
value={name}
onChange={(e) => setName(e.target.value)}
inputProps={{ maxLength: 300 }}
/>
<TextField
fullWidth
Expand Down
8 changes: 5 additions & 3 deletions apps/frontend/src/app/PageTeam/LoadoutDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ModalWrapper,
SqBadge,
} from '@genshin-optimizer/common/ui'
import { truncateString } from '@genshin-optimizer/common/util'
import { useDBMeta, useDatabase } from '@genshin-optimizer/gi/db-ui'
import { CharacterName } from '@genshin-optimizer/gi/ui'
import PersonIcon from '@mui/icons-material/Person'
Expand Down Expand Up @@ -75,9 +76,10 @@ export function LoadoutDropdown({
<TextField
fullWidth
label="New Loadout Name"
placeholder="New Loadout Name"
placeholder="New Loadout Name (max 300 characters)"
value={newName}
onChange={(e) => setNewName(e.target.value)}
inputProps={{ maxLength: 300 }}
/>
<TextField
fullWidth
Expand Down Expand Up @@ -115,7 +117,7 @@ export function LoadoutDropdown({
justifyContent: 'center',
}}
>
<span>{name}</span>
<span>{truncateString(name, 100)}</span>
<SqBadge color={buildIds.length ? 'success' : 'secondary'}>
{buildIds.length} Builds
</SqBadge>
Expand All @@ -142,7 +144,7 @@ export function LoadoutDropdown({
onClick={() => onChangeTeamCharId(tcId)}
sx={{ display: 'flex', gap: 1 }}
>
<span>{name}</span>
<span>{truncateString(name, 100)}</span>
<SqBadge
color={buildIds.length ? 'primary' : 'secondary'}
sx={{ marginLeft: 'auto' }}
Expand Down
6 changes: 4 additions & 2 deletions apps/frontend/src/app/PageTeam/TeamSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { useLocation, useNavigate } from 'react-router-dom'
import type { TeamCharacterContextObj } from '../../Context/TeamCharacterContext'
import { TeamCharacterContext } from '../../Context/TeamCharacterContext'
import BuildDropdown from '../BuildDropdown'
import { truncateString } from '@genshin-optimizer/common/util'
// TODO: Translation

export default function TeamSetting({
Expand Down Expand Up @@ -122,7 +123,7 @@ export default function TeamSetting({
endIcon={<SettingsIcon />}
onClick={() => setOpen((open) => !open)}
>
<Typography variant="h6">{team.name}</Typography>
<Typography variant="h6">{truncateString(team.name, 100)}</Typography>
</Button>
</BootstrapTooltip>

Expand Down Expand Up @@ -154,9 +155,10 @@ export default function TeamSetting({
<TextField
fullWidth
label="Team Name"
placeholder="Team Name"
placeholder="Team Name (max 300 characters)"
value={name}
onChange={(e) => setName(e.target.value)}
inputProps={{ maxLength: 300 }}
/>
<TextField
fullWidth
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/app/PageTeams/TeamCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BootstrapTooltip, CardThemed } from '@genshin-optimizer/common/ui'
import { hexToColor } from '@genshin-optimizer/common/util'
import { hexToColor, truncateString } from '@genshin-optimizer/common/util'
import type { CharacterKey, ElementKey } from '@genshin-optimizer/gi/consts'
import type { ICachedArtifact } from '@genshin-optimizer/gi/db'
import {
Expand Down Expand Up @@ -88,7 +88,7 @@ export default function TeamCard({
>
<CardActionArea onClick={() => onClick()} sx={{ p: 1 }}>
<Typography sx={{ display: 'flex', gap: 1 }}>
<span>{name}</span>{' '}
<span>{truncateString(name, 100)}</span>{' '}
<BootstrapTooltip title={<Typography>{description}</Typography>}>
<InfoIcon />
</BootstrapTooltip>
Expand Down
5 changes: 5 additions & 0 deletions libs/common/util/src/lib/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export function levenshteinDistance(str1: string, str2: string) {
}
return arr[str2.length][str1.length]
}

// truncate string to a certain length, and add ellipsis if it's longer
export function truncateString(str: string, length: number) {
return str.length > length ? str.slice(0, length) + '...' : str
Copy link
Owner

@frzyc frzyc Mar 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return str.length > length ? str.slice(0, length) + '...' : str
return str.length > length ? str.slice(0, length - 3) + '...' : str

}