File size: 3,153 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
import { computed, defineComponent, watch } from 'vue'
import { BridgeEvents, SharedData } from '@vue-devtools/shared-utils'
import { pendingSelectAppId, scanLegacyApps, useApps } from '@front/features/apps'
import { useOrientation } from '@front/features/layout/orientation'
import { useRouter } from 'vue-router'
import AppHeaderSelect from '../header/AppHeaderSelect.vue'
import { useBridge } from '../bridge'
import AppSelectItem from './AppSelectItem.vue'
import { useVueVersionCheck } from './vue-version-check'

export default defineComponent({
  components: {
    AppHeaderSelect,
    AppSelectItem,
  },

  setup() {
    const router = useRouter()
    const { bridge } = useBridge()

    const {
      apps,
      currentAppId,
      currentApp,
      selectApp,
    } = useApps()

    watch(currentAppId, (value) => {
      if (pendingSelectAppId.value !== value) {
        pendingSelectAppId.value = value
        bridge.send(BridgeEvents.TO_BACK_APP_SELECT, value)
      }
    }, {
      immediate: true,
    })

    let initDefaultAppId = false

    watch(apps, () => {
      if ((!currentApp.value || (SharedData.pageConfig?.defaultSelectedAppId && !initDefaultAppId)) && apps.value.length) {
        let targetId: string
        if (SharedData.pageConfig?.defaultSelectedAppId) {
          targetId = SharedData.pageConfig.defaultSelectedAppId
          initDefaultAppId = true
        }
        else if (currentAppId.value !== apps.value[0].id) {
          targetId = apps.value[0].id
        }
        if (targetId) {
          router.push({
            params: {
              appId: targetId,
              componentId: null,
            },
          })
        }
      }
    }, {
      immediate: true,
    })

    const { orientation } = useOrientation()

    // Vue version
    const { getLatestVersion } = useVueVersionCheck()
    const hasNewVueVersion = computed(() => apps.value.some(app => app.version !== getLatestVersion(app.version)))

    return {
      currentApp,
      apps,
      selectApp,
      orientation,
      hasNewVueVersion,
      scanLegacyApps,
    }
  },
})
</script>

<template>
  <AppHeaderSelect
    :items="apps"
    :selected-item="currentApp"
    @select="app => selectApp(app.id)"
  >
    <template #trigger>
      <VueButton
        class="flat icon-button"
      >
        <div class="flex items-center space-x-2 relative">
          <img src="~@front/assets/vue-logo.svg" class="w-8 h-8">
          <VueIcon
            v-if="hasNewVueVersion"
            icon="new_releases"
            class="text-green-400 absolute right-0 bottom-0 w-4 h-4"
          />
        </div>
      </VueButton>
    </template>

    <template #default="{ item }">
      <AppSelectItem
        :app="item"
        :selected="currentApp === item"
      />
    </template>

    <template #before>
      <VueButton
        v-if="$shared.legacyApps"
        class="flat m-1"
        icon-left="cached"
        @click="scanLegacyApps()"
      >
        Scan apps
      </VueButton>
    </template>
  </AppHeaderSelect>
</template>

<style lang="postcss" scoped>
.app-button {
  width: 220px;
}
</style>