refactor to fix infinite loop when instance has no cookbooks

This commit is contained in:
Michael Genson 2025-07-07 19:18:37 +00:00
commit 517216e9eb

View file

@ -139,31 +139,47 @@ export default defineNuxtComponent({
const cookbookPreferences = useCookbookPreferences();
function getStoreData<T>(
ownGroupStoreFn: (i18n: any) => { store: Ref<T[]>; actions: { refresh: () => void } },
publicStoreFn: (groupSlug: string, i18n: any) => { store: Ref<T[]>; actions: { refresh: () => void } },
): T[] {
let store;
const ownCookbookStore = useCookbookStore(i18n);
const ownHouseholdStore = useHouseholdStore(i18n);
const publicCookbookStoreCache = ref<Record<string, ReturnType<typeof usePublicCookbookStore>>>({});
const publicHouseholdStoreCache = ref<Record<string, ReturnType<typeof usePublicHouseholdStore>>>({});
function getPublicCookbookStore(slug: string) {
if (!publicCookbookStoreCache.value[slug]) {
publicCookbookStoreCache.value[slug] = usePublicCookbookStore(slug, i18n);
}
return publicCookbookStoreCache.value[slug];
}
function getPublicHouseholdStore(slug: string) {
if (!publicHouseholdStoreCache.value[slug]) {
publicHouseholdStoreCache.value[slug] = usePublicHouseholdStore(slug, i18n);
}
return publicHouseholdStoreCache.value[slug];
}
const cookbooks = computed(() => {
if (isOwnGroup.value) {
store = ownGroupStoreFn(i18n);
return ownCookbookStore.store.value;
}
else if (groupSlug.value) {
store = publicStoreFn(groupSlug.value, i18n);
const publicStore = getPublicCookbookStore(groupSlug.value);
return unref(publicStore.store);
}
else {
return [];
}
});
if (!store.store.value.length) {
store.actions.refresh();
const households = computed(() => {
if (isOwnGroup.value) {
return ownHouseholdStore.store.value;
}
return store.store.value;
else if (groupSlug.value) {
const publicStore = getPublicHouseholdStore(groupSlug.value);
return unref(publicStore.store);
}
const cookbooks = computed(() => getStoreData(useCookbookStore, usePublicCookbookStore));
const households = computed(() => getStoreData(useHouseholdStore, usePublicHouseholdStore));
return [];
});
const householdsById = computed(() => {
return households.value.reduce((acc, household) => {
@ -196,6 +212,10 @@ export default defineNuxtComponent({
const currentUserHouseholdId = computed(() => $auth.user.value?.householdId);
const cookbookLinks = computed<SideBarLink[]>(() => {
if (!cookbooks.value?.length) {
return [];
}
const sortedCookbooks = [...cookbooks.value].sort((a, b) => (a.position || 0) - (b.position || 0));
const ownLinks: SideBarLink[] = [];