<template>
  <suspense>
    <preview :key="key"></preview>
  </suspense>
</template>

<script setup name="EchartPreview">
import { getSessionStorageInfo } from './utils/storage';
import { setSessionStorage } from '../utils/storage';
import { ref } from 'vue';
import Preview from './viewer.vue';

let key = ref(Date.now());

// 数据变更 -> 组件销毁重建
try {
  const listenerArr = ['SaveJSON', 'SaveChart'];
  listenerArr.forEach(saveEvent => {
    if (!window.opener || !window.opener.addEventListener) return;
    window.opener.addEventListener(saveEvent, async e => {
      const localStorageInfo = await getSessionStorageInfo();
      setSessionStorage('CHART_STORAGE_LIST', [{ ...e.detail, id: localStorageInfo.id }]);
      key.value = Date.now();
    });
  });
} catch (error) {
  console.log(error);
}
</script>
