import { computed, defineComponent, onBeforeUpdate, onMounted, ref } from 'vue'; import type { BCMSMedia } from '@becomes/cms-sdk/types'; import { BCMSMediaType } from '@becomes/cms-sdk/types'; import BCMSIcon from '../icon'; import { useTranslation } from '../../translations'; const component = defineComponent({ props: { targetMediaId: { type: String, default: '', }, }, emits: { click: (_media: BCMSMedia) => { return true; }, }, setup(props, ctx) { const translations = computed(() => { return useTranslation(); }); const throwable = window.bcms.util.throwable; let lastMediaId = ''; const tree = ref([]); async function getParentTree(parentId?: string): Promise { const output: BCMSMedia[] = []; if (!parentId) { return output; } const parent: BCMSMedia | undefined = await throwable( async () => { return await window.bcms.sdk.media.getById(parentId); }, async (value) => { return value; } ); if (!parent) { return output; } output.push(parent); if (parent.isInRoot) { return output; } return [...output, ...(await getParentTree(parent.parentId))]; } onMounted(async () => { lastMediaId = props.targetMediaId; tree.value = (await getParentTree(props.targetMediaId)).reverse(); }); onBeforeUpdate(async () => { if (lastMediaId !== props.targetMediaId) { lastMediaId = props.targetMediaId; tree.value = (await getParentTree(props.targetMediaId)).reverse(); } }); return () => ( ); }, }); export default component;