Skip to content

Commit

Permalink
馃毟 (share) Sanitize URL ID
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Oct 17, 2022
1 parent f437ad6 commit 020a37c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
25 changes: 19 additions & 6 deletions apps/builder/components/share/EditableUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
} from '@chakra-ui/react'
import { EditIcon } from 'assets/icons'
import { CopyButton } from 'components/shared/buttons/CopyButton'
import React from 'react'
import { useToast } from 'components/shared/hooks/useToast'
import React, { useState } from 'react'

type EditableUrlProps = {
hostname: string
Expand All @@ -24,12 +25,26 @@ export const EditableUrl = ({
pathname,
onPathnameChange,
}: EditableUrlProps) => {
const { showToast } = useToast()
const [value, setValue] = useState(pathname)

const handleSubmit = (newPathname: string) => {
if (/^[a-z]+(-[a-z]+)*$/.test(newPathname))
return onPathnameChange(newPathname)
setValue(pathname)
showToast({
title: 'Invalid ID',
description: 'Should contain only contain letters and dashes.',
})
}

return (
<Editable
as={HStack}
spacing={3}
defaultValue={pathname}
onSubmit={onPathnameChange}
value={value}
onChange={setValue}
onSubmit={handleSubmit}
>
<HStack spacing={1}>
<Text>{hostname}/</Text>
Expand Down Expand Up @@ -58,9 +73,7 @@ export const EditableUrl = ({
const EditButton = (props: ButtonProps) => {
const { isEditing, getEditButtonProps } = useEditableControls()

return isEditing ? (
<></>
) : (
return isEditing ? null : (
<Button leftIcon={<EditIcon />} {...props} {...getEditButtonProps()}>
Edit
</Button>
Expand Down
3 changes: 2 additions & 1 deletion apps/builder/components/shared/hooks/useToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export const useToast = () => {
const showToast = useCallback(
({ title, description, status = 'error', ...props }: UseToastOptions) => {
toast({
position: 'bottom-right',
position: 'top-right',
description,
title,
status,
isClosable: true,
...props,
})
},
Expand Down

0 comments on commit 020a37c

Please sign in to comment.