Skip to content

Commit

Permalink
Chore/update regex (#1703)
Browse files Browse the repository at this point in the history
* chore: update linkedin regex

* chore: update twitter and linkedin regex
  • Loading branch information
alexanderleegs authored Nov 28, 2023
1 parent 09f3ade commit e2089d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/layouts/Settings/SocialMediaSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ import {
TIKTOK_REGEX,
TELEGRAM_REGEX,
YOUTUBE_REGEX,
LINKEDIN_REGEX,
} from "utils/validators"

import { SiteSettings } from "types/settings"

interface SocialMediaField {
label: StringKeyOf<SiteSettings["socialMediaContent"]>
placeholder: string
customLabel?: string
}

const SOCIAL_MEDIA_LABELS: SocialMediaField[] = [
{ label: "facebook", placeholder: "https://www.facebook.com/" },
{ label: "twitter", placeholder: "https://www.twitter.com/" },
{
label: "twitter",
placeholder: "https://www.twitter.com/",
customLabel: "X / Twitter",
},
{ label: "youtube", placeholder: "https://www.youtube.com/" },
{ label: "instagram", placeholder: "https://www.instagram.com/" },
{ label: "linkedin", placeholder: "https://sg.linkedin.com/" },
Expand All @@ -46,8 +52,9 @@ export const SocialMediaSettings = ({
<Section id="social-media-fields">
<SectionHeader label="Social Media" />
<VStack spacing="1.5rem" align="flex-start" w="50%">
{SOCIAL_MEDIA_LABELS.map(({ label, placeholder }) => (
{SOCIAL_MEDIA_LABELS.map(({ label, placeholder, customLabel }) => (
<ValidatedFormInput
customLabel={customLabel}
label={label}
isError={isError}
placeholder={placeholder}
Expand All @@ -63,11 +70,12 @@ interface ValidatedFormInputProps extends SocialMediaField {

// Helper component for social media as this is the only validated input
const ValidatedFormInput = ({
customLabel,
label,
placeholder,
isError,
}: ValidatedFormInputProps) => {
const displayedLabel = upperFirst(label)
const displayedLabel = upperFirst(customLabel || label)
const { register } = useFormContext()
const { errors } = useFormState<{
socialMediaContent: Record<
Expand Down Expand Up @@ -107,6 +115,10 @@ const getRegExp = (
return RegExp(`${URL_REGEX_PREFIX}${TELEGRAM_REGEX}`)
case "tiktok":
return RegExp(`${URL_REGEX_PREFIX}${label}${TIKTOK_REGEX}`)
case "linkedin":
return RegExp(`${URL_REGEX_PREFIX}${label}${LINKEDIN_REGEX}`)
case "twitter":
return RegExp(`${URL_REGEX_PREFIX}(x|twitter)${URL_REGEX_SUFFIX}`)
default:
return RegExp(`${URL_REGEX_PREFIX}${label}${URL_REGEX_SUFFIX}`)
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const URL_REGEX_SUFFIX = ".com/)([a-zA-Z0-9_-]+([/.])?)+$"
export const YOUTUBE_REGEX = ".com/(@)?)([a-zA-Z0-9_-]+([/.])?)+$"
export const TELEGRAM_REGEX = "telegram|t).me/([a-zA-Z0-9_-]+([/.])?)+$"
export const TIKTOK_REGEX = ".com/@)([a-zA-Z0-9_-]+([/.])?)+$"
// Linkedin doesn't seem to restrict special characters
export const LINKEDIN_REGEX = ".com/).+$"
// Domain name regex (source: https://regexr.com/3au3g and https://stackoverflow.com/a/30007882)
// 1. The first group (up till the "+") matches each segment of the domain (split by ".")
// a. Each segment of the domain must start and end with an alphanumeric character
Expand Down

0 comments on commit e2089d5

Please sign in to comment.