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

✨ Better warning for long logo data uri #440

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/lucky-terms-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"socialify": minor
---

Added error handling for long svg data uri input, also added jest unit test cases for this.
10 changes: 9 additions & 1 deletion src/components/configuration/config.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRouter } from 'next/router'
import React, { useContext, useEffect } from 'react'
import React, { useContext, useEffect, useState } from 'react'

import { getOptionalConfig } from '@/common/configHelper'
import { RepoQueryResponse } from '@/common/github/repoQuery'
Expand All @@ -25,6 +25,8 @@ const Config = ({ repository }: ConfigProp) => {

const { config, setConfig } = useContext(ConfigContext)

const [isSvgUriError, setIsSvgUriError] = useState<boolean>(false)

const handleChanges = (changes: { value: any; key: keyof ConfigType }[]) => {
let newConfig: ConfigType = { ...config }
const urlParams = router.query
Expand Down Expand Up @@ -69,6 +71,10 @@ const Config = ({ repository }: ConfigProp) => {
}

const handleChange = (value: any, key: keyof ConfigType) => {
if (key === 'logo') {
setIsSvgUriError(!!value?.val && value?.val?.length > 1600)
}

handleChanges([{ value, key }])
}

Expand Down Expand Up @@ -169,6 +175,8 @@ const Config = ({ repository }: ConfigProp) => {
placeholder="Optional"
value={config.logo}
handleChange={handleChange}
spudoodle marked this conversation as resolved.
Show resolved Hide resolved
isError={isSvgUriError}
errorMessage="URI is too long, please use an SVG image URL instead."
spudoodle marked this conversation as resolved.
Show resolved Hide resolved
/>

<div className="columns-2">
Expand Down
49 changes: 49 additions & 0 deletions src/components/configuration/configuration.test.tsx
spudoodle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { fireEvent, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import InputWrapper from './inputWrapper'

describe('svg data uri error checking', () => {
const mockHandleChange = jest.fn()

const baseProps = {
title: 'SVG Logo',
keyName: 'logo' as any,
value: '',
placeholder: 'SVG Data URI',
handleChange: mockHandleChange,
}

test('shows error message for svg data uri having more that 1600 characters', () => {
const invalidInput = 'a'.repeat(1601)

render(
<InputWrapper
{...baseProps}
isError={true}
errorMessage="URI is too long, please use an SVG image URL instead."
/>
)

const input = screen.getByPlaceholderText('SVG Data URI')
fireEvent.change(input, { target: { value: invalidInput } })

const errorMessage = screen.getByText(
'URI is too long, please use an SVG image URL instead.'
)
expect(errorMessage).toBeInTheDocument()
})

test('does not show error message for svg data uri having less than 1601 characters', () => {
const validInput = 'a'.repeat(1600)

render(<InputWrapper {...baseProps} isError={false} />)

const input = screen.getByPlaceholderText('SVG Data URI')
fireEvent.change(input, { target: { value: validInput } })

const errorMessage = screen.queryByText(
/URI is too long, please use an SVG image URL instead./i
)
expect(errorMessage).not.toBeInTheDocument()
})
})
13 changes: 12 additions & 1 deletion src/components/configuration/inputWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type InputProps = {
placeholder: string
disabled?: boolean
handleChange: (value: any, key: keyof ConfigType) => void
isError?: boolean
errorMessage?: string
}

const InputWrapper = ({
Expand All @@ -18,6 +20,8 @@ const InputWrapper = ({
placeholder,
disabled,
handleChange,
isError,
errorMessage,
}: InputProps) => {
return (
<div className="form-control w-full">
Expand All @@ -26,7 +30,7 @@ const InputWrapper = ({
{alt && <span className="label-text-alt font-semibold">{alt}</span>}
</label>
<input
className="input input-sm input-bordered font-semibold w-full"
className={`input input-sm input-bordered font-semibold w-full ${isError && 'input-error'}`}
type="text"
value={value || ''}
disabled={!!disabled}
Expand All @@ -35,6 +39,13 @@ const InputWrapper = ({
handleChange({ val: e.target.value, required: true }, keyName)
}}
/>
{isError && (
<div className="label">
<span className="label-text-alt text-red-400 italic">
spudoodle marked this conversation as resolved.
Show resolved Hide resolved
{errorMessage}
</span>
</div>
)}
</div>
)
}
Expand Down
Loading