94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { computed, ref } from 'vue';
|
|
import { fetchBusinessCategories } from '@/api/business-categories';
|
|
import { fetchBusiness, fetchBusinesses } from '@/api/businesses';
|
|
import type { Business } from '@/api/types';
|
|
import { MONTIME_BRAND, NIVASH_BRAND, type SiteBrand } from '@/constants/site-branding';
|
|
|
|
export const useTenantStore = defineStore('tenant', () => {
|
|
const loaded = ref(false);
|
|
const loading = ref(false);
|
|
const isSingleBusiness = ref(false);
|
|
const singleBusiness = ref<Business | null>(null);
|
|
/** Business landing opened from multi-tenant discovery (not platform-wide single-business mode). */
|
|
const focusedBusiness = ref<Business | null>(null);
|
|
|
|
const landingBusiness = computed(() =>
|
|
isSingleBusiness.value ? singleBusiness.value : focusedBusiness.value,
|
|
);
|
|
|
|
const landingBusinessId = computed(() => landingBusiness.value?.id ?? null);
|
|
|
|
const brand = computed<SiteBrand>(() => {
|
|
const biz = landingBusiness.value;
|
|
if (biz) {
|
|
return {
|
|
...NIVASH_BRAND,
|
|
name: biz.title,
|
|
shortName: biz.title,
|
|
copyrightName: biz.title,
|
|
aboutText: biz.description?.trim() || NIVASH_BRAND.aboutText,
|
|
};
|
|
}
|
|
|
|
return MONTIME_BRAND;
|
|
});
|
|
|
|
const singleBusinessId = computed(() => singleBusiness.value?.id ?? null);
|
|
|
|
function syncSingleBusiness(business: Business) {
|
|
if (isSingleBusiness.value) {
|
|
singleBusiness.value = business;
|
|
}
|
|
if (focusedBusiness.value?.id === business.id) {
|
|
focusedBusiness.value = business;
|
|
}
|
|
}
|
|
|
|
function setFocusedBusiness(business: Business | null) {
|
|
focusedBusiness.value = business;
|
|
}
|
|
|
|
async function bootstrap() {
|
|
if (loaded.value || loading.value) return;
|
|
loading.value = true;
|
|
try {
|
|
const [categories, businesses] = await Promise.all([
|
|
fetchBusinessCategories(),
|
|
fetchBusinesses({ isActive: true }),
|
|
]);
|
|
|
|
const activeCategories = categories.filter((c) => c.isActive);
|
|
isSingleBusiness.value =
|
|
activeCategories.length === 1 && businesses.length === 1;
|
|
|
|
if (isSingleBusiness.value) {
|
|
singleBusiness.value = await fetchBusiness(businesses[0].id);
|
|
} else {
|
|
singleBusiness.value = null;
|
|
}
|
|
} catch {
|
|
isSingleBusiness.value = false;
|
|
singleBusiness.value = null;
|
|
} finally {
|
|
loading.value = false;
|
|
loaded.value = true;
|
|
}
|
|
}
|
|
|
|
return {
|
|
loaded,
|
|
loading,
|
|
isSingleBusiness,
|
|
singleBusiness,
|
|
singleBusinessId,
|
|
focusedBusiness,
|
|
landingBusiness,
|
|
landingBusinessId,
|
|
brand,
|
|
bootstrap,
|
|
syncSingleBusiness,
|
|
setFocusedBusiness,
|
|
};
|
|
});
|