All files / src/stores demo.store.ts

98.42% Statements 125/127
100% Branches 9/9
95.91% Functions 47/49
100% Lines 121/121

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293                                      9x     1x                   1x 1x 1x 184x 183x 100x     1x 1x 1x         9x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x     1x                           1x 1x       1x     1x     1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     2x 2x 1x 1x 1x 1x 1x           1x       19x     1x                           1x 1x       1x     1x     1x     1x 1x 1x     1x 1x 1x     2x 2x 2x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     1x 1x 1x     2x 2x 1x 1x 1x 1x 1x           1x       19x 6x 6x 6x   2x     4x       19x 4x   1x   3x         9x         19x                                    
import { defineStore, storeToRefs } from 'pinia';
import { computed, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useViewer3cr } from '@/composables/useViewer3cr';
import { mockDemoViewerStlDecimator } from '@/components/demo/mockDemoViewerStlDecimator';
import { LoadSessionState } from '@3cr/types-ts/types/LoadSessionState';
import { DEMO_BUCKET_URL } from '@/constants';
import { useUploadStore } from '@/stores/upload.store';
import {
  DemoType,
  SaveState,
  Upload,
  ViewerOptions,
  ViewerPayload,
} from '@3cr/viewer-types-ts';
import totalSegmentator from '@/assets/images/totalSegmentator.png';
import { v4 } from 'uuid';
import avatar from '@/assets/images/courtney.png';
 
const now = new Date();
 
function createUpload(file: File): Upload {
  return {
    id: v4(),
    title: file.name,
    subtitle: 'Uploading...',
    status: 'uploading' as const,
    progress: 0,
  };
}
 
function fakeUpload(upload: Upload) {
  upload.progress = 0;
  upload.status = 'uploading';
  const interval = setInterval(() => {
    if (upload.progress! < 1) {
      if (Math.random() >= 0.5) {
        upload.progress = upload.progress! + 0.01;
      }
    } else {
      upload.status = 'completed';
      upload.subtitle = 'Upload Complete';
      clearInterval(interval);
    }
  }, 100);
}
 
export const useDemoStore = defineStore('demo', () => {
  const { t, tm } = useI18n();
  const viewer3cr = useViewer3cr();
  const { uploads } = storeToRefs(useUploadStore());
 
  const isDemo = ref<boolean>(false);
  const demoTitle = ref<string>('');
  const demoSubtitles = ref<string[]>([]);
  const m_demoInfo = ref<boolean>(false);
  const m_demoEnableCloudStorage = ref<boolean>(false);
  const m_demoPurchaseCredits = ref<boolean>(false);
  const m_demoUpgradePlan = ref<boolean>(false);
  const profileDropdown = reactive({ modal: false, x: 0, y: 0 });
  const notificationDropdown = reactive({ modal: false, x: 0, y: 0 });
  const recentDropdown = reactive({ modal: false, x: 0, y: 0 });
 
  const demoLicenceOptions = computed<ViewerOptions>(() => ({
    fileName: `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()} CT Chest-Scan DICOM Series`,
    user: { name: t('data.profiles[0].name'), avatarUrl: avatar },
    getAIModels: () => [
      {
        key: 'TotalSegmentator',
        image: totalSegmentator,
        title: t('labels.autoAnnotate'),
        subtitle: t('texts.supportsFormats'),
        description: t('texts.runAutoAi'),
        url: 'https://google.com',
        tooltip: t('labels.totalSegmentator'),
        cost: 5,
        estimatedTimeSeconds: 1200,
      },
    ],
    onRunAIModel: async (_keys: string[]) => {
      mockDemoViewerStlDecimator();
      const message: LoadSessionState = {
        Version: '0.0.0',
        Url: `${DEMO_BUCKET_URL}/state/TotalSegmentator_1722837421466.3crms`,
      };
      await viewer3cr.loadMcadObjects({ message });
    },
    onCloudStorage: () => {
      m_demoEnableCloudStorage.value = true;
    },
    onPurchaseCredits: () => {
      m_demoPurchaseCredits.value = true;
    },
    onDownloadMcad: () => {
      demoTitle.value = t('texts.demo.licence.downloadMcad.title');
      demoSubtitles.value = tm('texts.demo.licence.downloadMcad.subtitles');
      m_demoInfo.value = true;
    },
    onMCADUpload: (files: Array<File>) => {
      const items = files.map((file) => createUpload(file));
      uploads.value.push(...items);
      uploads.value.forEach((upload) => fakeUpload(upload));
    },
    onDownloadScan: () => {
      demoTitle.value = t('texts.demo.licence.downloadScan.title');
      demoSubtitles.value = tm('texts.demo.licence.downloadScan.subtitles');
      m_demoInfo.value = true;
    },
    onExit: () => {
      demoTitle.value = t('texts.demo.licence.exit.title');
      demoSubtitles.value = tm('texts.demo.licence.exit.subtitles');
      m_demoInfo.value = true;
    },
    onLoadScan: () => {
      demoTitle.value = t('texts.demo.licence.loadScan.title');
      demoSubtitles.value = tm('texts.demo.licence.loadScan.subtitles');
      m_demoInfo.value = true;
    },
    onLoadSession: () => {
      demoTitle.value = t('texts.demo.licence.loadSession.title');
      demoSubtitles.value = tm('texts.demo.licence.loadSession.subtitles');
      m_demoInfo.value = true;
    },
    onProfileDropdown: (x, y) => {
      profileDropdown.x = x;
      profileDropdown.y = y;
      profileDropdown.modal = true;
    },
    onNotificationDropdown: (x: number, y: number) => {
      notificationDropdown.x = x;
      notificationDropdown.y = y;
      notificationDropdown.modal = true;
    },
    onRecentDropdown: (x: number, y: number) => {
      recentDropdown.x = x;
      recentDropdown.y = y;
      recentDropdown.modal = true;
    },
    onSaveSession: (scan, dataOverlay, mcad, callback) => {
      callback(SaveState.SAVING);
      setTimeout(() => {
        callback(SaveState.SAVED_TEMPORARY);
        setTimeout(() => {
          demoTitle.value = t('texts.demo.licence.saveSession.title');
          demoSubtitles.value = tm('texts.demo.licence.saveSession.subtitles');
          m_demoInfo.value = true;
        }, 500);
      }, 1000);
    },
    onShareScan: () => {},
    onUpgrade: () => {
      m_demoUpgradePlan.value = true;
    },
  }));
 
  const demoPatientOptions = computed<ViewerOptions>(() => ({
    fileName: `${now.getUTCFullYear()}-${now.getUTCMonth() + 1}-${now.getUTCDate()} CT Chest-Scan DICOM Series`,
    user: { name: t('data.profiles[0].name'), avatarUrl: avatar },
    getAIModels: () => [
      {
        key: 'TotalSegmentator',
        image: totalSegmentator,
        title: t('labels.autoAnnotate'),
        subtitle: t('texts.supportsFormats'),
        description: t('texts.runAutoAi'),
        url: 'https://google.com',
        tooltip: t('labels.totalSegmentator'),
        cost: 5,
        estimatedTimeSeconds: 1200,
      },
    ],
    onRunAIModel: async (_keys: string[]) => {
      mockDemoViewerStlDecimator();
      const message: LoadSessionState = {
        Version: '0.0.0',
        Url: `${DEMO_BUCKET_URL}/state/TotalSegmentator_1722837421466.3crms`,
      };
      await viewer3cr.loadMcadObjects({ message });
    },
    onCloudStorage: () => {
      m_demoEnableCloudStorage.value = true;
    },
    onPurchaseCredits: () => {
      m_demoPurchaseCredits.value = true;
    },
    onDownloadMcad: () => {
      demoTitle.value = t('texts.demo.patient.downloadMcad.title');
      demoSubtitles.value = tm('texts.demo.patient.downloadMcad.subtitles');
      m_demoInfo.value = true;
    },
    onDownloadScan: () => {
      demoTitle.value = t('texts.demo.patient.downloadScan.title');
      demoSubtitles.value = tm('texts.demo.patient.downloadScan.subtitles');
      m_demoInfo.value = true;
    },
    onMCADUpload: (files: Array<File>) => {
      const items = files.map((file) => createUpload(file));
      uploads.value.push(...items);
      uploads.value.forEach((upload) => fakeUpload(upload));
    },
    onExit: () => {
      demoTitle.value = t('texts.demo.patient.exit.title');
      demoSubtitles.value = tm('texts.demo.patient.exit.subtitles');
      m_demoInfo.value = true;
    },
    onLoadScan: () => {
      demoTitle.value = t('texts.demo.patient.loadScan.title');
      demoSubtitles.value = tm('texts.demo.patient.loadScan.subtitles');
      m_demoInfo.value = true;
    },
    onLoadSession: () => {
      demoTitle.value = t('texts.demo.patient.loadSession.title');
      demoSubtitles.value = tm('texts.demo.patient.loadSession.subtitles');
      m_demoInfo.value = true;
    },
    onProfileDropdown: (x, y) => {
      profileDropdown.x = x;
      profileDropdown.y = y;
      profileDropdown.modal = true;
    },
    onNotificationDropdown: (x: number, y: number) => {
      notificationDropdown.x = x;
      notificationDropdown.y = y;
      notificationDropdown.modal = true;
    },
    onRecentDropdown: (x: number, y: number) => {
      recentDropdown.x = x;
      recentDropdown.y = y;
      recentDropdown.modal = true;
    },
    onSaveSession: (scan, dataOverlay, mcad, callback) => {
      callback(SaveState.SAVING);
      setTimeout(() => {
        callback(SaveState.SAVED_TEMPORARY);
        setTimeout(() => {
          demoTitle.value = t('texts.demo.patient.saveSession.title');
          demoSubtitles.value = tm('texts.demo.patient.saveSession.subtitles');
          m_demoInfo.value = true;
        }, 500);
      }, 1000);
    },
    onShareScan: () => {},
    onUpgrade: () => {
      m_demoUpgradePlan.value = true;
    },
  }));
 
  const demoType = computed(() => {
    const urlParams = new URLSearchParams(window.location.search);
    const demoType = urlParams.get('demoType')?.toLowerCase();
    switch (demoType) {
      case 'licence':
        return DemoType.Licence;
      case 'patient':
      default:
        return DemoType.Patient;
    }
  });
 
  const demoOptions = computed<ViewerOptions>(() => {
    switch (demoType.value) {
      case DemoType.Licence:
        return demoLicenceOptions.value;
      case DemoType.Patient:
        return demoPatientOptions.value;
    }
  });
 
  function setIsDemo(payload: ViewerPayload) {
    isDemo.value = payload.Url.startsWith(
      'https://webgl-3dr.singular.health/demo',
    );
  }
 
  return {
    isDemo,
    demoTitle,
    demoSubtitles,
    m_demoInfo,
    m_demoEnableCloudStorage,
    m_demoUpgradePlan,
    m_demoPurchaseCredits,
    profileDropdown,
    notificationDropdown,
    recentDropdown,
    demoLicenceOptions,
    demoPatientOptions,
    demoType,
    demoOptions,
    setIsDemo,
  };
});