Skip to content

Commit 91e5da0

Browse files
committed
refactor: Go back to Object.keys?
1 parent 6edd0aa commit 91e5da0

7 files changed

Lines changed: 43 additions & 56 deletions

File tree

plugins/rss-feed.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ export function rssFeedPlugin() {
2525
}
2626
});
2727

28-
for (const postPath in blogPosts) {
29-
const post = blogPosts[/** @type {keyof blogPosts} */ (postPath)];
28+
Object.entries(blogPosts).map(([postPath, post]) => {
3029
const postTranslation = englishTranslations.blogPosts[post.label];
3130

3231
feed.addItem({
@@ -36,7 +35,7 @@ export function rssFeedPlugin() {
3635
description: postTranslation.excerpt,
3736
date: new Date(post.date)
3837
});
39-
}
38+
});
4039

4140
function removeDefaultGenerator(str) {
4241
return str

src/components/blog-overview/index.jsx

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,31 @@ import { useTranslate } from '../../lib/i18n';
33
import { Time } from '../time';
44
import s from './style.module.css';
55

6-
/**
7-
* @typedef {import('../../locales/en.json')} Translations
8-
*/
9-
106
export default function BlogOverview() {
117
const translate = useTranslate();
128

13-
const posts = [];
14-
for (const post in blogPosts) {
15-
const translatedBlog = translate(
16-
'blogPosts',
17-
/** @type {keyof Translations['blogPosts']} */ (blogPosts[post].label)
18-
);
19-
20-
posts.push(
21-
<article class={s.post}>
22-
<div class={s.meta}>
23-
<Time value={blogPosts[post].date} />
24-
</div>
25-
<h2 class={s.title}>
26-
<a href={post}>{translatedBlog.label}</a>
27-
</h2>
28-
<p class={s.excerpt}>{translatedBlog.excerpt}</p>
29-
<a href={post} class="btn-small">
30-
{translate('i18n', 'continueReading')} &rarr;
31-
</a>
32-
</article>
33-
);
34-
}
35-
369
return (
3710
<div>
38-
<div class={s.postList}>{posts}</div>
11+
<div class={s.postList}>
12+
{Object.entries(blogPosts).map(([postPath, post]) => {
13+
const translatedBlog = translate('blogPosts', post.label);
14+
15+
return (
16+
<article class={s.post}>
17+
<div class={s.meta}>
18+
<Time value={post.date} />
19+
</div>
20+
<h2 class={s.title}>
21+
<a href={postPath}>{translatedBlog.label}</a>
22+
</h2>
23+
<p class={s.excerpt}>{translatedBlog.excerpt}</p>
24+
<a href={postPath} class="btn-small">
25+
{translate('i18n', 'continueReading')} &rarr;
26+
</a>
27+
</article>
28+
);
29+
})}
30+
</div>
3931
</div>
4032
);
4133
}

src/components/footer/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export default function Footer() {
5050
<label class={style.lang}>
5151
Language:{' '}
5252
<select value={lang || 'en'} onInput={onSelect}>
53-
{Object.keys(config.languages).map(id => (
53+
{Object.entries(config.locales).map(([id, label]) => (
5454
<option selected={id == lang} value={id}>
55-
{config.languages[id]}
55+
{label}
5656
</option>
5757
))}
5858
</select>

src/components/header/index.jsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,6 @@ function LanguagePicker() {
120120
const { lang, setLang } = useLanguageContext();
121121
const translate = useTranslate();
122122

123-
const languages = [];
124-
if (typeof window !== 'undefined') {
125-
for (const language in config.languages) {
126-
languages.push(
127-
<button
128-
class={cx(language == lang && style.current)}
129-
data-value={language}
130-
onClick={e => setLang(e.currentTarget.dataset.value)}
131-
>
132-
{config.languages[language]}
133-
</button>
134-
);
135-
}
136-
}
137-
138123
return (
139124
<div class={style.translation}>
140125
<NavMenu>
@@ -148,7 +133,16 @@ function LanguagePicker() {
148133
}
149134
aria-label={translate('i18n', 'selectYourLanguage')}
150135
>
151-
{languages}
136+
{typeof window !== 'undefined' &&
137+
Object.entries(config.locales).map(([id, label]) => (
138+
<button
139+
class={cx(id == lang && style.current)}
140+
data-locale={id}
141+
onClick={e => setLang(e.currentTarget.dataset.locale)}
142+
>
143+
{label}
144+
</button>
145+
))}
152146
</ExpandableNavLink>
153147
)}
154148
</NavMenu>

src/components/sidebar/index.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ export default function Sidebar() {
2929
),
3030
level: 2,
3131
href: null,
32-
routes: Object.keys(docPages[version][item]).map(nested => ({
33-
text: translate('sidebarNav', docPages[version][item][nested].label),
34-
level: 3,
35-
href: `/guide/${version}${nested}`
36-
}))
32+
routes: Object.entries(docPages[version][item]).map(
33+
([pagePath, page]) => ({
34+
text: translate('sidebarNav', page.label),
35+
level: 3,
36+
href: `/guide/${version}${pagePath}`
37+
})
38+
)
3739
});
3840
}
3941
}

src/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"languages": {
2+
"locales": {
33
"en": "English",
44
"de": "German",
55
"es": "Spanish",

src/lib/i18n.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function LanguageProvider({ children }) {
7272
const userLang =
7373
query.lang ||
7474
localStorageGet('lang') ||
75-
getNavigatorLanguage(config.languages) ||
75+
getNavigatorLanguage(config.locales) ||
7676
'en';
7777

7878
setLang(userLang);

0 commit comments

Comments
 (0)