Skip to content

Commit baccb91

Browse files
committed
enh: create isActive function to share the logic
1 parent 59bd4f1 commit baccb91

6 files changed

Lines changed: 40 additions & 13 deletions

File tree

src/components/Header.astro

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { getRelativeLocaleUrl } from 'astro:i18n';
33
import Logo from '@/components/Logo.astro';
44
import MobileMenu from '@/components/MobileMenu.astro';
5-
import { getLangFromUrl, useTranslations } from '@/i18n/utils';
5+
import { getLangFromUrl, isActive, useTranslations } from '@/i18n/utils';
66
77
const lang = getLangFromUrl(Astro.url);
88
const t = useTranslations(lang);
@@ -51,15 +51,13 @@ const ctaLinks = {
5151
<ul class="menu menu-horizontal flex flex-1 flex-wrap gap-0 px-0 lg:gap-1">
5252
{
5353
navigationLinks.map((link) => {
54-
const hrefStr = String(link.href);
55-
const linkPathname = new URL(hrefStr).pathname.replace(/\/$/, '') || '/';
56-
const isActive = linkPathname === pathname;
54+
const isActiveLink = isActive(link.href, pathname);
5755
return (
5856
<li>
5957
<a
6058
class:list={[
6159
'btn btn-xs btn-ghost',
62-
isActive && 'btn-outline btn-primary',
60+
isActiveLink && 'btn-outline btn-primary',
6361
]}
6462
href={link.href}
6563
>

src/components/MobileMenu.astro

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import { Icon } from 'astro-icon/components';
33
import Logo from '@/components/Logo.astro';
4-
import { getLangFromUrl, useTranslations } from '@/i18n/utils';
4+
import { getLangFromUrl, isActive, useTranslations } from '@/i18n/utils';
55
66
interface Props {
77
navigationLinks: Array<{ href: string; label: string }>;
@@ -15,7 +15,6 @@ interface Props {
1515
const { navigationLinks, ctaLinks, pathname } = Astro.props;
1616
const lang = getLangFromUrl(Astro.url);
1717
const t = useTranslations(lang);
18-
const pathnameNormalized = pathname.replace(/\/$/, '') || '/';
1918
---
2019

2120
<div class="drawer drawer-start lg:hidden">
@@ -48,15 +47,13 @@ const pathnameNormalized = pathname.replace(/\/$/, '') || '/';
4847
<ul class="menu w-full gap-1 p-0">
4948
{
5049
navigationLinks.map((link) => {
51-
const hrefStr = String(link.href);
52-
const linkPathname = new URL(hrefStr).pathname.replace(/\/$/, '') || '/';
53-
const isActive = linkPathname === pathnameNormalized;
50+
const isActiveLink = isActive(link.href, pathname);
5451
return (
5552
<li>
5653
<a
5754
class:list={[
5855
'btn btn-ghost btn-sm w-full justify-start',
59-
isActive && 'btn-outline btn-primary',
56+
isActiveLink && 'btn-outline btn-primary',
6057
]}
6158
href={link.href}
6259
>

src/components/ui/HeroSection.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface Props {
1212
eyebrow?: string;
1313
title: string;
1414
subtitle?: string;
15-
stats?: Stat[];
15+
stats: Stat[];
1616
class?: string;
1717
}
1818
@@ -45,7 +45,7 @@ const { eyebrow, title, subtitle, stats, class: className = '' } = Astro.props;
4545
</p>
4646
)}
4747

48-
{stats && stats.length > 0 && (
48+
{stats.length > 0 && (
4949
<div class="flex flex-wrap gap-6 mt-10">
5050
{stats.map((stat) => {
5151
const colorClasses = {

src/i18n/ui.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ export const ui = {
194194
'contributing.hero.title': 'Como posso ajudar?',
195195
'contributing.hero.subtitle':
196196
'Como organização sem fins lucrativos, a PodCodar depende de pessoas e organizações que acreditam na democratização da educação em tecnologia. Você pode apoiar com recursos, tempo ou parcerias estratégicas.',
197+
'contributing.hero.stats.volunteers.value': '50+',
198+
'contributing.hero.stats.volunteers.label': 'Voluntários',
199+
'contributing.hero.stats.projects.value': '20+',
200+
'contributing.hero.stats.projects.label': 'Projetos',
201+
'contributing.hero.stats.years.value': '5+',
202+
'contributing.hero.stats.years.label': 'Anos',
197203
'contributing.donations.title': 'Doações',
198204
'contributing.donations.subtitle':
199205
'Doações de pessoas físicas e patrocínios ajudam a financiar sustentabilidade, impacto e crescimento — plataforma de ensino, oficinas e fortalecimento do time.',

src/i18n/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export function useTranslations(lang?: Lang) {
1717
);
1818
};
1919
}
20+
21+
export function isActive(linkPath: string, currentPath: string): boolean {
22+
const normalizedLink = linkPath.replace(/\/$/, '') || '/';
23+
const normalizedCurrent = currentPath.replace(/\/$/, '') || '/';
24+
return normalizedLink === normalizedCurrent;
25+
}

src/pages/contributing.astro

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ const description = t('contributing.hero.subtitle');
1818
eyebrow={t('contributing.hero.eyebrow')}
1919
title={t('contributing.hero.title')}
2020
subtitle={t('contributing.hero.subtitle')}
21+
stats={[
22+
{
23+
icon: 'lucide:users',
24+
value: t('contributing.hero.stats.volunteers.value'),
25+
label: t('contributing.hero.stats.volunteers.label'),
26+
color: 'violet',
27+
},
28+
{
29+
icon: 'lucide:folder-code',
30+
value: t('contributing.hero.stats.projects.value'),
31+
label: t('contributing.hero.stats.projects.label'),
32+
color: 'emerald',
33+
},
34+
{
35+
icon: 'lucide:calendar',
36+
value: t('contributing.hero.stats.years.value'),
37+
label: t('contributing.hero.stats.years.label'),
38+
color: 'blue',
39+
},
40+
]}
2141
>
2242
<Icon name="lucide:heart" slot="eyebrow-icon" class="w-4 h-4" />
2343
</HeroSection>

0 commit comments

Comments
 (0)