Skip to content

Commit

Permalink
refactor: update utils imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hta218 committed Aug 13, 2023
1 parent 2cb9119 commit eda69a6
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 160 deletions.
13 changes: 3 additions & 10 deletions components/AnalyticsLink.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { siteMetadata } from '~/data/siteMetadata'
import { AreaChart } from 'lucide-react'

export function AnalyticsLink() {
return (
<button
aria-label="Open analytics"
type="button"
className="ml-1 rounded p-2 hover:bg-gray-200 dark:hover:bg-gray-700 sm:ml-2"
className="rounded p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700"
data-umami-event="nav-analytics"
onClick={() => window.open(siteMetadata.analyticsURL, '_blank')}
>
<svg
role="img"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
className="h-5 w-5 text-gray-900 dark:text-gray-100"
>
<path d="M1.019 13.019h3.849V24h-3.85zm8.943-6.68h3.85V24h-3.85zM19.132 0h3.85v24h-3.85z" />
</svg>
<AreaChart strokeWidth={1.5} size={20} />
</button>
)
}
2 changes: 1 addition & 1 deletion components/BuiltWith.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTranslation } from 'next-i18next'
import { siteMetadata } from '~/data/siteMetadata'
import { DevIcon } from './DevIcon'
import { Link } from './Link'
import { useTranslation } from 'next-i18next'

export function BuiltWith() {
const { t } = useTranslation('common')
Expand Down
117 changes: 62 additions & 55 deletions components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import clsx from 'clsx'
import { useTranslation } from 'next-i18next'
import NextImage from 'next/image'
import { useRouter } from 'next/router'
import { headerNavLinks } from '~/data/headerNavLinks'
import { AnalyticsLink } from './AnalyticsLink'
import { LanguageSwitcher } from './LanguageSwitcher'
import { Link } from './Link'
import { ThemeSwitcher } from './ThemeSwitcher'
import { LanguageSwitcher } from './LanguageSwitcher'
import { useTranslation } from 'next-i18next'
import { headerNavLinks } from '~/data/headerNavLinks'

export function Header({ onToggleNav }: { onToggleNav: () => void }) {
const { t } = useTranslation('common') // 'common' fa referència al fitxer common.json
let { t } = useTranslation('common')
let router = useRouter()

return (
<header className="supports-backdrop-blur:bg-white/95 sticky top-0 z-50 overflow-visible bg-white/75 py-3 backdrop-blur dark:bg-dark/75">
<header className="supports-backdrop-blur:bg-white/95 sticky top-0 z-50 overflow-hidden bg-white/75 py-3 backdrop-blur dark:bg-dark/75">
<div className="mx-auto flex max-w-3xl items-center justify-between px-3 xl:max-w-5xl xl:px-0">
<div className="flex items-center justify-between">
<Link href="/" aria-label="Leo's Blog">
<div className="flex items-center justify-between" data-umami-event="logo">
<div className="mr-3 flex items-center justify-center">
<NextImage
src="/static/images/logo.jpg"
alt="Leo's Blog logo"
width={45}
height={45}
className="rounded-full"
/>
</div>
<Link href="/" aria-label="Leo's Blog">
<div className="flex items-center justify-between" data-umami-event="logo">
<div className="mr-3 flex items-center justify-center">
<NextImage
src="/static/images/logo.jpg"
alt="Leo's Blog logo"
width={45}
height={45}
className="rounded-full"
/>
</div>
</Link>
<div className="hidden space-x-2 sm:block">
</div>
</Link>
<div className="flex items-center gap-4">
<div className="hidden space-x-1.5 sm:block">
{headerNavLinks.map((link) => (
<Link key={link.titleKey} href={link.href}>
<span
className={clsx(
'inline-block rounded px-2 py-1 font-medium text-gray-900 dark:text-gray-100 sm:px-3 sm:py-2',
'inline-block rounded px-3 py-1 font-medium',
router.pathname.startsWith(link.href)
? 'bg-gray-200 dark:bg-gray-700'
: 'hover:bg-gray-200 dark:hover:bg-gray-700'
Expand All @@ -45,43 +46,49 @@ export function Header({ onToggleNav }: { onToggleNav: () => void }) {
</Link>
))}
</div>
</div>
<div className="flex items-center">
<AnalyticsLink />
<ThemeSwitcher />
<LanguageSwitcher />
<button
className="ml-2 mr-1 h-8 w-8 rounded sm:hidden"
type="button"
aria-label="Toggle Menu"
onClick={onToggleNav}
data-umami-event="mobile-nav-toggle"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="text-gray-900 dark:text-gray-100"
>
<path
fillRule="evenodd"
d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
></path>
<path
fillRule="evenodd"
d="M3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
></path>
<path
fillRule="evenodd"
d="M3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
></path>
</svg>
</button>
<div className="flex items-center gap-1">
<AnalyticsLink />
<ThemeSwitcher />
<LanguageSwitcher />
<MobileNavToggle onToggleNav={onToggleNav} />
</div>
</div>
</div>
</header>
)
}

function MobileNavToggle({ onToggleNav }: { onToggleNav: () => void }) {
return (
<button
className="ml-2 mr-1 h-8 w-8 rounded sm:hidden"
type="button"
aria-label="Toggle Menu"
onClick={onToggleNav}
data-umami-event="mobile-nav-toggle"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="text-gray-900 dark:text-gray-100"
>
<path
fillRule="evenodd"
d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
/>
<path
fillRule="evenodd"
d="M3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
/>
<path
fillRule="evenodd"
d="M3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z"
clipRule="evenodd"
/>
</svg>
</button>
)
}
127 changes: 52 additions & 75 deletions components/LanguageSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,89 +1,66 @@
import { useEffect, useState } from 'react'
import { clsx } from 'clsx'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { LOCALES } from '~/constant'
import { Image } from './Image'
import { Popover, PopoverContent, PopoverTrigger } from './ui/Popover'

const localeNames = {
en: 'English',
es: 'Español',
ca: 'Català',
// add other languages as needed
}

function getLocaleName(localeCode) {
return localeNames[localeCode] || localeCode
function getLocale(code: string) {
return LOCALES.find((locale) => locale.code === code)
}

function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
export function LanguageSwitcher() {
const router = useRouter()
const [value, setValue] = useState(router.locale)
const [dropdownOpen, setDropdownOpen] = useState(false)
let router = useRouter()
let [localeCode, setLocaleCode] = useState(router.locale)
let currentLocale = getLocale(localeCode)

useEffect(() => {
setValue(router.locale)
setLocaleCode(router.locale)
}, [router.locale])

function translate_to(locale) {
router.push(router.pathname, router.asPath, { locale })
setDropdownOpen(false) // Tancar el menú desplegable un cop l'usuari hagi seleccionat una opció
function handleChange(newLocale: string) {
router.push(router.pathname, router.asPath, { locale: newLocale })
}

return (
<div className="relative inline-block text-left">
<button
id="dropdownDefaultButton"
data-dropdown-toggle="dropdown"
onClick={() => setDropdownOpen(!dropdownOpen)}
className=" font-medium text-gray-700 hover:bg-gray-200 rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center
dark:border-gray-600 dark:text-gray-200 dark:hover:bg-gray-700 dark:hover:text-white
"
type="button"
>
{capitalize(value)}
<svg
className="w-2.5 h-2.5 ml-2.5"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 10 6"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="m1 1 4 4 4-4"
/>
</svg>
</button>

{dropdownOpen && (
<div
id="dropdown"
className="origin-top-right absolute right-0 mt-2 w-44 rounded-lg shadow bg-white ring-1 ring-black ring-opacity-5 z-50 divide-y divide-gray-100
dark:divide-gray-600 dark:ring-gray-800 dark:ring-opacity-5 dark:ring-1 dark:bg-gray-700 dark:ring-offset-2 dark:ring-offset-gray-700"
>
<ul
className="py-2 text-sm text-gray-700 dark:text-gray-200"
aria-labelledby="dropdownDefaultButton"
>
{router.locales.map((locale) => {
const label = getLocaleName(locale)
return (
<li key={locale}>
<button
onClick={() => translate_to(locale)}
className="block px-4 py-2 w-full text-left hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white"
>
{label}
</button>
</li>
)
})}
</ul>
</div>
)}
</div>
<Popover>
<PopoverTrigger className="p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700 rounded">
<Image
src={currentLocale.flag}
alt={currentLocale.name}
width={20}
height={20}
shouldOpenLightbox={false}
/>
</PopoverTrigger>
<PopoverContent className="bg-white dark:bg-dark w-32">
<ul className="space-y-1">
{router.locales.map((code) => {
let locale = getLocale(code)
return (
<li key={code}>
<button
onClick={() => handleChange(code)}
className={clsx(
'inline-flex font-normal items-center gap-3 px-2 py-1 rounded w-full text-left hover:bg-gray-100 dark:hover:bg-gray-700',
code === localeCode && 'text-primary-500 dark:text-primary-400'
)}
>
<Image
src={locale.flag}
alt={locale.name}
width={20}
height={20}
shouldOpenLightbox={false}
className="shrink-0"
/>
<span>{locale.name}</span>
</button>
</li>
)
})}
</ul>
</PopoverContent>
</Popover>
)
}
2 changes: 1 addition & 1 deletion components/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from 'next/link'
import { kebabCase } from '~/utils/kebab-case'
import { kebabCase } from '~/utils/string'

export function Tag({ text }: { text: string }) {
return (
Expand Down
24 changes: 7 additions & 17 deletions components/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react'
import { useTheme } from 'next-themes'
import { Sun, MoonStar } from 'lucide-react'

export function ThemeSwitcher() {
let [mounted, setMounted] = useState(false)
Expand All @@ -13,26 +14,15 @@ export function ThemeSwitcher() {
<button
aria-label="Toggle Dark Mode"
type="button"
className="ml-1 rounded p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700 sm:ml-2"
className="rounded p-1.5 hover:bg-gray-200 dark:hover:bg-gray-700"
onClick={() => setTheme(isDark ? 'light' : 'dark')}
data-umami-event="nav-theme-switcher"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="h-6 w-6 text-gray-900 dark:text-gray-100"
>
{mounted && isDark ? (
<path
fillRule="evenodd"
d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z"
clipRule="evenodd"
/>
) : (
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z" />
)}
</svg>
{mounted && isDark ? (
<MoonStar strokeWidth={1.5} size={20} />
) : (
<Sun strokeWidth={1.5} size={20} />
)}
</button>
)
}
2 changes: 1 addition & 1 deletion components/Twemoji.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TwemojiProps } from '~/types'
import { kebabCase } from '~/utils/kebab-case'
import { kebabCase } from '~/utils/string'

export function Twemoji({ emoji, size = 'twa-lg', className }: TwemojiProps) {
let cls = `inline-block twa ${size} twa-${kebabCase(emoji)} ${className || ''}`
Expand Down

1 comment on commit eda69a6

@vercel
Copy link

@vercel vercel bot commented on eda69a6 Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

leo-huynh-dot-dev – ./

leo-huynh-dot-dev-git-main-hta218.vercel.app
www.leohuynh.dev
leo-huynh-dot-dev-hta218.vercel.app
leohuynh.dev

Please sign in to comment.