Skip to content

Commit

Permalink
Merge pull request #538 from systemli/Remove-old-Twitter-Message-Limit
Browse files Browse the repository at this point in the history
🔥 Remove old Twitter Message Limit
  • Loading branch information
0x46616c6b authored Oct 20, 2023
2 parents 93c651e + 1b114cf commit 3cb9e6a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
21 changes: 17 additions & 4 deletions src/components/message/MessageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface FormValues {
message: string
}

export const MESSAGE_LIMIT = 280

const MessageForm: FC<Props> = ({ ticker }) => {
const {
formState: { isSubmitSuccessful, errors },
Expand All @@ -45,6 +43,21 @@ const MessageForm: FC<Props> = ({ ticker }) => {
}
const [map, setMap] = useState<FeatureCollection<Geometry, any>>(emptyMap)

Check warning on line 44 in src/components/message/MessageForm.tsx

View workflow job for this annotation

GitHub Actions / ESLint Results

src/components/message/MessageForm.tsx#L44

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)

/**
* Calculates the maximum length of a message based on the given ticker's configuration.
* If the ticker's Mastodon integration is active, the maximum length is 500 characters.
* Otherwise, the maximum length is 4096 characters (Telegram message limit).
* @param ticker - The ticker object to calculate the maximum message length for.
* @returns The maximum length of a message for the given ticker.
*/
const maxLength: number = (function (ticker: Ticker) {
if (ticker.mastodon.active) {
return 500
}

return 4096
})(ticker)

const onUpload = useCallback(
(uploads: Upload[]) => {
setAttachments(attachments.concat(uploads))
Expand Down Expand Up @@ -96,7 +109,7 @@ const MessageForm: FC<Props> = ({ ticker }) => {
<TextField
{...register('message', {
required: true,
maxLength: MESSAGE_LIMIT,
maxLength: maxLength,
})}
color={errors.message ? 'error' : 'primary'}
error={!!errors.message}
Expand All @@ -119,7 +132,7 @@ const MessageForm: FC<Props> = ({ ticker }) => {
</IconButton>
<MessageMapModal map={map} onChange={onMapUpdate} onClose={() => setMapDialogOpen(false)} open={mapDialogOpen} ticker={ticker} />
</Box>
<MessageFormCounter letterCount={message?.length || 0} />
<MessageFormCounter letterCount={message?.length || 0} maxLength={maxLength} />
</Stack>
<Box>
<AttachmentsPreview attachments={attachments} onDelete={onUploadDelete} />
Expand Down
29 changes: 29 additions & 0 deletions src/components/message/MessageFormCounter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import MessageFormCounter from './MessageFormCounter'

describe('MessageFormCounter', () => {
it('should render the correct letter count', () => {
const letterCount = 10
const maxLength = 100
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />)
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`)
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorSuccess')
})

it('should render a red chip when the letter count is over the limit', () => {
const letterCount = 110
const maxLength = 100
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />)
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`)
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorError')
})

it('should render a green chip when the letter count is under the limit', () => {
const letterCount = 90
const maxLength = 100
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />)
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`)
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorWarning')
})
})
12 changes: 6 additions & 6 deletions src/components/message/MessageFormCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { ChipPropsColorOverrides, Chip } from '@mui/material'
import { OverridableStringUnion } from '@mui/types'
import React, { FC, useEffect, useState } from 'react'
import { MESSAGE_LIMIT } from './MessageForm'

interface Props {
maxLength: number
letterCount: number
}

const MessageFormCounter: FC<Props> = ({ letterCount }) => {
const MessageFormCounter: FC<Props> = ({ letterCount, maxLength }) => {
const [color, setColor] =
useState<OverridableStringUnion<'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning', ChipPropsColorOverrides>>('default')

useEffect(() => {
if (letterCount > MESSAGE_LIMIT) {
if (letterCount > maxLength) {
setColor('error')
} else if (letterCount >= 240) {
} else if (letterCount >= maxLength * 0.9) {
setColor('warning')
} else if (letterCount === 0) {
setColor('default')
} else {
setColor('success')
}
}, [letterCount])
}, [letterCount, maxLength])

return <Chip color={color} label={`${letterCount}/${MESSAGE_LIMIT}`} size="small" variant="outlined" />
return <Chip color={color} label={`${letterCount}/${maxLength}`} size="small" variant="outlined" />
}

export default MessageFormCounter

0 comments on commit 3cb9e6a

Please sign in to comment.