Skip to content

Commit 46e573c

Browse files
authored
refactor: social media URL handling improvement (#165)
1 parent 1c64070 commit 46e573c

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

apps/web/src/modules/gallery/MasonryHeaderMasonryItem.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,40 @@ import { usePhotos } from '~/hooks/usePhotoViewer'
88

99
import { ActionGroup } from './ActionGroup'
1010

11+
function resolveSocialUrl(
12+
value: string,
13+
{ baseUrl, stripAt }: { baseUrl: string; stripAt?: boolean },
14+
): string | undefined {
15+
const trimmed = value.trim()
16+
17+
if (!trimmed) {
18+
return undefined
19+
}
20+
21+
if (/^https?:\/\//i.test(trimmed)) {
22+
return trimmed
23+
}
24+
25+
const normalized = stripAt ? trimmed.replace(/^@/, '') : trimmed
26+
if (!normalized) {
27+
return undefined
28+
}
29+
return `${baseUrl}${normalized}`
30+
}
31+
1132
export const MasonryHeaderMasonryItem = ({ style, className }: { style?: React.CSSProperties; className?: string }) => {
1233
const { t } = useTranslation()
1334
const { i18n } = useTranslation()
1435
const visiblePhotoCount = usePhotos().length
36+
const githubUrl =
37+
siteConfig.social && siteConfig.social.github
38+
? resolveSocialUrl(siteConfig.social.github, { baseUrl: 'https://github.com/' })
39+
: undefined
40+
const twitterUrl =
41+
siteConfig.social && siteConfig.social.twitter
42+
? resolveSocialUrl(siteConfig.social.twitter, { baseUrl: 'https://twitter.com/', stripAt: true })
43+
: undefined
44+
1545
return (
1646
<div
1747
className={clsxm(
@@ -48,9 +78,9 @@ export const MasonryHeaderMasonryItem = ({ style, className }: { style?: React.C
4878
{/* Social media links */}
4979
{siteConfig.social && (
5080
<div className="mt-1 mb-3 flex items-center justify-center gap-3">
51-
{siteConfig.social.github && (
81+
{githubUrl && (
5282
<a
53-
href={`https://github.com/${siteConfig.social.github}`}
83+
href={githubUrl}
5484
target="_blank"
5585
rel="noreferrer"
5686
className="text-text-secondary flex items-center justify-center p-2 duration-200 hover:text-[#E7E8E8]"
@@ -59,9 +89,9 @@ export const MasonryHeaderMasonryItem = ({ style, className }: { style?: React.C
5989
<i className="i-mingcute-github-fill text-sm" />
6090
</a>
6191
)}
62-
{siteConfig.social.twitter && (
92+
{twitterUrl && (
6393
<a
64-
href={`https://twitter.com/${siteConfig.social.twitter.replace('@', '')}`}
94+
href={twitterUrl}
6595
target="_blank"
6696
rel="noreferrer"
6797
className="text-text-secondary flex items-center justify-center p-2 duration-200 hover:text-[#1da1f2]"

0 commit comments

Comments
 (0)