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

feat: add workspace label to Workspace Selector #323

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -9,13 +9,14 @@ import {
type INavigationWorkspace,
type IWorkspaceSelectorDisplayItem,
Popover,
Typography,
} from 'src/components'
import { Flex } from 'src/components'
import React, { type ChangeEvent, useRef, useState } from 'react'
import { useCallback } from 'react'
import { useEffect } from 'react'
import { useMemo } from 'react'
import { debounce, hasImageAtSrc } from 'src/utils/utils'
import { debounce, hasContent, hasImageAtSrc, trimString } from 'src/utils/utils'
import { getInitials } from 'src/utils/utils'

import { type InputRef } from 'src/components'
Expand Down Expand Up @@ -62,6 +63,9 @@ function sortOrgsByActiveWorkspace(orgs: INavigationOrg[]): INavigationOrg[] {
return orgs
}

/** Number of characters to show under the avatar */
const WORKSPACE_LABEL_LIMIT = 7

export function WorkspaceSelector(props: IWorkspaceSelectorProps) {
const [searchTerm, setSearchTerm] = useState<string>('')
const inputRef = useRef<InputRef>(null)
Expand Down Expand Up @@ -109,6 +113,9 @@ export function WorkspaceSelector(props: IWorkspaceSelectorProps) {
}, [sortedOrgs])

const workspaceInitials = getInitials(activeWorkspace?.label)
const workspaceLabel = hasContent(activeWorkspace?.label)
? trimString(activeWorkspace?.label, WORKSPACE_LABEL_LIMIT)
: undefined
danielsiemens marked this conversation as resolved.
Show resolved Hide resolved

const hasSearchInput = !!searchTerm || menuItems.filter(item => !!item.label).length > 5

Expand Down Expand Up @@ -152,9 +159,12 @@ export function WorkspaceSelector(props: IWorkspaceSelectorProps) {
onClick={e => {
focusOnInput(true)
}}>
<Avatar {...props.avatarOptions} className="workspaceSelector__avatar">
{getInitialsIfNoImage(hasImage, workspaceInitials)}
</Avatar>
<Flex vertical align="center" justify="center">
<Avatar {...props.avatarOptions} className="workspaceSelector__avatar">
{getInitialsIfNoImage(hasImage, workspaceInitials)}
</Avatar>
{workspaceLabel && <Typography.Text className="workspaceSelector__label">{workspaceLabel}</Typography.Text>}
</Flex>
</div>
</Popover>
)
Expand Down Expand Up @@ -260,4 +270,4 @@ export function WorkspaceSelector(props: IWorkspaceSelectorProps) {
}
}
}
}
}
71 changes: 70 additions & 1 deletion src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInitials, getOS } from './utils'
import { getInitials, getOS, hasContent, trimString } from './utils'
import { expect, describe, it, beforeEach, vi } from 'vitest'

describe('Testing utils', () => {
Expand Down Expand Up @@ -55,4 +55,73 @@ describe('Testing utils', () => {
expect(actualOS).toBe('Macintosh')
})
})
describe('trimString()', () => {
it('should return the string when no limit given', () => {
const str = 'test string'
const result = trimString(str)
expect(result).toBe(str)
})
it('should return the trimmed string when no limit given and spaces included', () => {
const str = 'test string '
const result = trimString(str)
expect(result).toBe('test string')
})
it('should return the shortened string when limit given', () => {
const str = 'test string'
const limit = 5
const result = trimString(str, limit)
expect(result).toBe('test...')
})
it('should return an empty string when input is undefined', () => {
const str = undefined
const result = trimString(str)
expect(result).toBe('')
})
it('should return the string input when limit is not an integer', () => {
const str = 'test string'
const result = trimString(str)
expect(result).toBe(str)
})
it('should return the string input when limit is undefined', () => {
const str = 'test string'
const limit = undefined
const result = trimString(str, limit)
expect(result).toBe(str)
})
it('should return the string input when limit is not an integer', () => {
const str = 'test string'
const limit = 3.23
const result = trimString(str, limit)
expect(result).toBe(str)
})
it('should return the string input when limit is less than 0', () => {
const str = 'test string'
const limit = -5
const result = trimString(str, limit)
expect(result).toBe(str)
})
})

describe('hasContent()', () => {
it('should return true when string is valid', () => {
const str = 'test string'
const result = hasContent(str)
expect(result).toBe(true)
})
it('should return false when string is empty', () => {
const str = ''
const result = hasContent(str)
expect(result).toBe(false)
})
it('should return false when string is only spaces', () => {
const str = ' \n\t'
const result = hasContent(str)
expect(result).toBe(false)
})
it('should return false when string is undefined', () => {
const str = undefined
const result = hasContent(str)
expect(result).toBe(false)
})
})
})
15 changes: 15 additions & 0 deletions src/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ export function buildLinkFromHrefOptions(label: ReactNode, hrefOptions?: HrefOpt
</a>
)
}

/** Returns `true` when a string has contents that are not just spaces */
export const hasContent = (str?: string) => {
return (str?.trim?.()?.length ?? 0) > 0
}

/** Returns a string that is trimmed of extraneous spacing, and shortened to the `limit` if parameter provided/applicable */
export const trimString = (str?: string, limit?: number) => {
const _str = str?.trim() ?? ''
const _limit = limit && Number.isInteger(limit) && limit >= 0 ? limit : undefined
if (_limit !== undefined && _str.length > _limit) {
return `${_str.substring(0, limit).trim()}...`
}
return _str
}
Loading