Fix blog hooks: unwrap API response envelope for authors and categories

The API returns { authors: {...} } and { categories: [...] } but hooks
were expecting the unwrapped values directly. Also add defensive guards
in components for undefined props during initial render.

Bunker Admin
This commit is contained in:
bunker-admin 2026-03-27 13:33:47 -06:00
parent 8b9ab93856
commit eb16815f91
4 changed files with 8 additions and 8 deletions

View File

@ -42,12 +42,12 @@ export function BlogFrontmatterPanel({
if (!frontmatter) return null; if (!frontmatter) return null;
const authorOptions = Object.entries(authors).map(([key, entry]) => ({ const authorOptions = Object.entries(authors || {}).map(([key, entry]) => ({
label: entry.name, label: entry.name,
value: key, value: key,
})); }));
const categoryOptions = categories.map((cat) => ({ const categoryOptions = (categories || []).map((cat) => ({
label: cat, label: cat,
value: cat, value: cat,
})); }));

View File

@ -42,12 +42,12 @@ export function NewBlogPostModal({
const dateStr = dateValue ? dateValue.format('YYYY-MM-DD') : dayjs().format('YYYY-MM-DD'); const dateStr = dateValue ? dateValue.format('YYYY-MM-DD') : dayjs().format('YYYY-MM-DD');
const previewFilename = slug ? `blog/posts/${dateStr}-${slug}.md` : ''; const previewFilename = slug ? `blog/posts/${dateStr}-${slug}.md` : '';
const authorOptions = Object.entries(authors).map(([key, entry]) => ({ const authorOptions = Object.entries(authors || {}).map(([key, entry]) => ({
label: entry.name, label: entry.name,
value: key, value: key,
})); }));
const categoryOptions = categories.map((cat) => ({ const categoryOptions = (categories || []).map((cat) => ({
label: cat, label: cat,
value: cat, value: cat,
})); }));

View File

@ -29,8 +29,8 @@ export function useBlogAuthors(): UseBlogAuthorsReturn {
const refetch = useCallback(async () => { const refetch = useCallback(async () => {
setLoading(true); setLoading(true);
try { try {
const res = await api.get<AuthorsMap>('/docs/blog/authors'); const res = await api.get<{ authors: AuthorsMap }>('/docs/blog/authors');
setAuthors(res.data); setAuthors(res.data.authors ?? {});
} catch { } catch {
// If no authors file exists yet, keep empty // If no authors file exists yet, keep empty
setAuthors({}); setAuthors({});

View File

@ -14,8 +14,8 @@ export function useBlogCategories(): UseBlogCategoriesReturn {
const refetch = useCallback(async () => { const refetch = useCallback(async () => {
setLoading(true); setLoading(true);
try { try {
const res = await api.get<string[]>('/docs/blog/categories'); const res = await api.get<{ categories: string[] }>('/docs/blog/categories');
setCategories(res.data); setCategories(res.data.categories ?? []);
} catch { } catch {
setCategories([]); setCategories([]);
} finally { } finally {