Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

4.7 KB
Raw Blame
<template>
  <div class="app-version" v-if="isVersionValid()">
    <!-- Current Version -->
    <p class="current-version">
      {{ $t('updates.app-version-note') }} {{ appVersion }}
    </p>
    <div v-if="checksEnabled">
      <!-- Results haven't come in yet, either still checking, or error -->
      <p v-if="!finished">
        {{ error ? 'Error checking for updates.' : 'Chcekcing for Updates...' }}
      </p>
      <!-- App is up-to-date -->
      <p v-if="finished && isUpToDate" class="up-to-date">
        βœ… {{ $t('updates.up-to-date') }}
      </p>
      <!-- An update is available, but not too out-of-date -->
      <p  v-else-if="finished && !veryOutOfDate" class="update-availible">
        ⚠️{{ $t('updates.out-of-date') }}: <b>{{ latestVersion }}</b>
      </p>
      <!-- Update available, app is VERY out of date, show some additional info -->
      <p  v-else-if="finished && veryOutOfDate" class="big-update-availible">
        ❗ {{ $t('updates.out-of-date') }}: <b>{{ latestVersion }}</b>
        <span class="please-update">
          {{ $t('updates.unsupported-version-l1') }}.<br>
          {{ $t('updates.unsupported-version-l2') }} {{ latestVersion }}
        </span>
      </p>
    </div>
  </div>
</template>

<script>
import { Progress } from 'rsup-progress';
import request from '@/utils/request';
import ErrorHandler from '@/utils/logging/ErrorHandler';

export default {
  name: 'AppInfoModal',
  computed: {
    appConfig() {
      return this.$store.getters.appConfig;
    },
  },
  props: {
    doUpdateCheck: { type: Boolean, required: false, default: true },
  },
  data() {
    return {
      appVersion: import.meta.env.VITE_APP_VERSION, // Current version, from package.json
      progress: new Progress({ color: 'var(--progress-bar)' }),
      latestVersion: '', // Will store latest version, when request returns
      checksEnabled: true, // Should we check for updates
      isUpToDate: true, // Is current version === latest version
      veryOutOfDate: false, // If the app is more than 5 versions out of date
      finished: false, // Set to true when request is done
      error: false, // Set to true if checkig fails
    };
  },
  mounted() {
    if (!this.doUpdateCheck || !this.appVersion || (this.appConfig && this.appConfig.disableUpdateChecks)) {
      // Either current version isn't found, or user disabled checks
      this.checksEnabled = false;
    } else {
      this.checkVersion(); // Trigger the check
    }
  },
  methods: {
    /* Gets the apps latest version from Dashy's git repo */
    checkVersion() {
      const packageUrl = 'https://raw.githubusercontent.com/Lissy93/dashy/master/package.json';
      this.progress.start();
      request.get(packageUrl).then((response) => {
        const latest = response && response.data && response.data.version;
        if (typeof latest !== 'string' || !/^\d+(\.\d+)*$/.test(latest)) {
          this.error = true;
          return;
        }
        this.latestVersion = latest;
        this.isUpToDate = this.checkIfUpToDate(this.appVersion, latest);
        this.finished = true;
      }).catch(() => {
        this.error = true;
      }).finally(() => {
        this.progress.end();
      });
    },
    /* Compares the current version, with the package.json version */
    checkIfUpToDate(currentVersion, latestVersion) {
      const [curMaj = 0, curMin = 0, curPatch = 0] = currentVersion.split('.').map(Number);
      const [latMaj = 0, latMin = 0, latPatch = 0] = latestVersion.split('.').map(Number);
      if (latMaj > curMaj || latMin - curMin > 5) this.veryOutOfDate = true;
      if (latMaj !== curMaj) return latMaj < curMaj;
      if (latMin !== curMin) return latMin < curMin;
      return latPatch <= curPatch;
    },
    /* Checks that the input version is correctly parsed */
    isVersionValid() {
      const isValid = typeof this.appVersion === 'string'
        && /^\d+(\.\d+)*$/.test(this.appVersion);
      if (!isValid) { // If invalid, then record an error
        ErrorHandler(
          'Unable to check for updates, because current version is unavailible.'
          + ` ${this.appVersion} is not a valid version.`,
        );
      }
      return isValid;
    },
  },
};
</script>

<style scoped lang="scss">

div.app-version {
  color: var(--settings-text-color);
  text-align: center;
  p {
    margin: 0.5rem auto;
    color: var(--transparent-white-50);
    cursor: default;
    &.current-version {
        margin: 0;
    }
    &.up-to-date {
      color: var(--success);
      font-weight: bold;
      opacity: 0.8;
    }
    &.update-availible {
      color: var(--warning);
      opacity: 0.8;
    }
    &.big-update-availible {
      color: var(--danger);
      .please-update {
        font-size: 0.8rem;
        color: var(--danger);
        display: block;
      }
    }
  }
}

</style>