Skip to content

Commit 58b5fda

Browse files
committed
refactor to simplify code
1 parent 0138fbb commit 58b5fda

File tree

9 files changed

+286
-316
lines changed

9 files changed

+286
-316
lines changed

app/[lang]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function LangPage({ params }: { params: Params }) {
2323
<main className="min-h-screen">
2424
<LangRedirect currentLocale={params.lang} />
2525
<Navbar content={dict.nav} currentLocale={params.lang} />
26-
<div className="bg-page-gradient">
26+
<div className="bg-page-gradient bg-grid">
2727
<Hero content={dict.hero} />
2828
<About content={dict.about} />
2929
<Projects content={dict.projects} locale={params.lang} />

app/[lang]/projects/[slug]/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export default function CaseStudyLangPage({ params }: { params: Params }) {
2020
if (!study) return notFound();
2121

2222
const isEn = params.lang === 'en';
23-
const title = isEn && study.titleEn ? study.titleEn : study.title;
24-
const problem = isEn && study.problemEn ? study.problemEn : study.problem;
25-
const solution = isEn && study.solutionEn ? study.solutionEn : study.solution;
26-
const impact = isEn && study.impactEn ? study.impactEn : study.impact;
23+
const title = isEn ? study.title.en : study.title.es;
24+
const problem = isEn ? study.problem.en : study.problem.es;
25+
const solution = isEn ? study.solution.en : study.solution.es;
26+
const impact = isEn ? study.impact.en : study.impact.es;
2727

2828
const tabs = [
2929
{ id: 'context', label: isEn ? 'Context' : 'Contexto', content: problem },

app/page.tsx

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
import { About } from '@/components/sections/about';
2-
import { Architecture } from '@/components/sections/architecture';
3-
import { Contact } from '@/components/sections/contact';
4-
import { Hero } from '@/components/sections/hero';
5-
import { Projects } from '@/components/sections/projects';
6-
import { Navbar } from '@/components/sections/navbar';
7-
import { getDictionary } from '@/lib/i18n';
8-
import { LangRedirect } from '@/components/lang-redirect';
1+
"use client";
92

10-
export default function HomePage() {
11-
const dict = getDictionary('es');
3+
import { LangRedirect } from "@/components/lang-redirect";
4+
5+
export default function RootRedirect() {
126
return (
13-
<main className="min-h-screen">
14-
<LangRedirect currentLocale="es" />
15-
<Navbar content={dict.nav} currentLocale="es" />
16-
<div className="bg-page-gradient">
17-
<Hero content={dict.hero} />
18-
<About content={dict.about} />
19-
<Projects content={dict.projects} locale="es" />
20-
<Architecture content={dict.architecture} />
21-
<Contact content={dict.contact} />
22-
</div>
7+
<main className="min-h-screen flex items-center justify-center bg-page-gradient bg-grid text-base-muted">
8+
<LangRedirect currentLocale="es" targetPath="/" force />
9+
<p>Redirigiendo…</p>
2310
</main>
2411
);
2512
}

app/projects/[slug]/page.tsx

Lines changed: 0 additions & 80 deletions
This file was deleted.

components/lang-redirect.tsx

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
1-
'use client';
1+
"use client";
22

3-
import { normalizeLocale, type Locale } from '@/lib/i18n';
4-
import { useEffect } from 'react';
3+
import { normalizeLocale, type Locale } from "@/lib/i18n";
4+
import { useEffect } from "react";
55

6-
const STORAGE_KEY = 'preferred-lang';
6+
const STORAGE_KEY = "preferred-lang";
77

8-
function pathForLocale(locale: Locale): string {
9-
return locale === 'es' ? '/' : `/${locale}`;
8+
function pathForLocale(locale: Locale, targetPath = ""): string {
9+
const suffix = targetPath
10+
? targetPath.startsWith("/")
11+
? targetPath
12+
: `/${targetPath}`
13+
: "";
14+
const base = `/${locale}`;
15+
if (suffix === "/" || suffix === "") return base;
16+
return `${base}${suffix}`;
1017
}
1118

12-
export function LangRedirect({ currentLocale }: { currentLocale: Locale }) {
19+
export function LangRedirect({
20+
currentLocale,
21+
targetPath = "",
22+
force = false,
23+
}: {
24+
currentLocale: Locale;
25+
targetPath?: string;
26+
force?: boolean;
27+
}) {
1328
useEffect(() => {
1429
const stored = localStorage.getItem(STORAGE_KEY);
15-
if (stored === 'en' || stored === 'es') {
16-
if (stored !== currentLocale) {
17-
window.location.assign(pathForLocale(stored));
18-
}
19-
return;
20-
}
2130

2231
const navigatorLang =
23-
typeof navigator !== 'undefined' ? navigator.language || navigator.languages?.[0] : undefined;
32+
typeof navigator !== "undefined"
33+
? navigator.language || navigator.languages?.[0]
34+
: undefined;
2435
const detected = normalizeLocale(navigatorLang);
2536

26-
if (detected !== currentLocale) {
27-
localStorage.setItem(STORAGE_KEY, detected);
28-
window.location.assign(pathForLocale(detected));
37+
const preferred =
38+
stored === "en" || stored === "es"
39+
? (stored as Locale)
40+
: detected ?? currentLocale;
41+
42+
if (force || preferred !== currentLocale) {
43+
if (!stored) {
44+
localStorage.setItem(STORAGE_KEY, preferred);
45+
}
46+
window.location.assign(pathForLocale(preferred, targetPath));
2947
}
30-
}, [currentLocale]);
48+
}, [currentLocale, force, targetPath]);
3149

3250
return null;
3351
}

components/lang-switcher.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useRouter } from 'next/navigation';
88
const STORAGE_KEY = 'preferred-lang';
99

1010
function pathForLocale(locale: Locale): string {
11-
return locale === 'es' ? '/' : `/${locale}`;
11+
return `/${locale}`;
1212
}
1313

1414
type Props = {

components/sections/projects.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export function Projects({ content, locale }: Props) {
3838
</div>
3939
</CardHeader>
4040
<CardContent className="flex flex-col gap-4">
41-
<p className="text-base text-base-muted">{isEn && project.summaryEn ? project.summaryEn : project.summary}</p>
41+
<p className="text-base text-base-muted">
42+
{isEn ? project.summary.en : project.summary.es}
43+
</p>
4244
<div className="flex flex-wrap gap-2">
4345
{project.stack.map((item) => (
4446
<Badge key={item} variant="mint">
@@ -47,7 +49,7 @@ export function Projects({ content, locale }: Props) {
4749
))}
4850
</div>
4951
<div className="pt-2">
50-
<Link href={`${isEn ? '/en' : ''}/projects/${project.slug}`} className="inline-flex">
52+
<Link href={`/${locale}/projects/${project.slug}`} className="inline-flex">
5153
<Button variant="ghost" size="md">{content.cta}</Button>
5254
</Link>
5355
</div>

0 commit comments

Comments
 (0)