feat: local caching for dev
This commit is contained in:
parent
c70fc72952
commit
522d8ca48a
21 changed files with 375 additions and 326 deletions
|
@ -1,39 +1,39 @@
|
|||
import { createClient } from '@supabase/supabase-js'
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
const SUPABASE_URL = import.meta.env.SUPABASE_URL
|
||||
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
|
||||
const SUPABASE_URL = import.meta.env.SUPABASE_URL;
|
||||
const SUPABASE_KEY = import.meta.env.SUPABASE_KEY;
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
||||
|
||||
let cachedNavigation = null;
|
||||
|
||||
export async function fetchNavigation() {
|
||||
const { data, error } = await supabase
|
||||
.from('optimized_navigation')
|
||||
.select('*')
|
||||
if (import.meta.env.MODE === "development" && cachedNavigation)
|
||||
return cachedNavigation;
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching navigation data:', error)
|
||||
return {}
|
||||
}
|
||||
const { data, error } = await supabase
|
||||
.from("optimized_navigation")
|
||||
.select("*");
|
||||
if (error) return {};
|
||||
|
||||
const menu = data.reduce((acc, item) => {
|
||||
const menuItem = {
|
||||
title: item['title'] || item['page_title'],
|
||||
permalink: item['permalink'] || item ['page_permalink'],
|
||||
icon: item['icon'],
|
||||
sort: item['sort']
|
||||
}
|
||||
title: item["title"] || item["page_title"],
|
||||
permalink: item["permalink"] || item["page_permalink"],
|
||||
icon: item["icon"],
|
||||
sort: item["sort"],
|
||||
};
|
||||
|
||||
if (!acc[item['menu_location']]) {
|
||||
acc[item['menu_location']] = [menuItem]
|
||||
} else {
|
||||
acc[item['menu_location']].push(menuItem)
|
||||
}
|
||||
if (!acc[item["menu_location"]]) acc[item["menu_location"]] = [menuItem];
|
||||
else acc[item["menu_location"]].push(menuItem);
|
||||
|
||||
return acc
|
||||
}, {})
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
Object.keys(menu).forEach(location => {
|
||||
menu[location].sort((a, b) => a['sort'] - b['sort'])
|
||||
})
|
||||
Object.keys(menu).forEach((location) => {
|
||||
menu[location].sort((a, b) => a["sort"] - b["sort"]);
|
||||
});
|
||||
|
||||
return menu
|
||||
}
|
||||
if (import.meta.env.MODE === "development") cachedNavigation = menu;
|
||||
|
||||
return menu;
|
||||
};
|
||||
|
|
Reference in a new issue