<script setup>
import { useStateStore } from 'stores/useStateStore';
import { onMounted, ref } from 'vue';
import { useQuasar } from 'quasar';
import LeftMenu from '../LeftMenu.vue';

const stateStore = useStateStore();
const $props = defineProps({
    leftDrawer: {
        type: Boolean,
        default: true,
    },
});
onMounted(
    () => (stateStore.leftDrawer = useQuasar().screen.gt.xs ? $props.leftDrawer : false)
);

const targetId = 'left-panel';
const teleportRef = ref({});
const hasContent = ref();
let observer;

onMounted(() => {
    if (teleportRef.value) {
        const checkContent = () => {
            hasContent.value = teleportRef.value.innerHTML.trim() !== '';
        };

        observer = new MutationObserver(checkContent);
        observer.observe(teleportRef.value, { childList: true, subtree: true });

        checkContent();
    }
});
</script>

<template>
    <QDrawer v-model="stateStore.leftDrawer" show-if-above :width="256">
        <QScrollArea class="fit text-grey-8">
            <div :id="targetId" ref="teleportRef"></div>
            <LeftMenu v-if="!hasContent" />
        </QScrollArea>
    </QDrawer>
    <QPageContainer>
        <QPage>
            <RouterView />
        </QPage>
    </QPageContainer>
</template>
