ConnectionBanner.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <NotificationBlock
  3. v-if="connectionState.showDisconnectedBanner"
  4. id="connection-banner"
  5. type="severe"
  6. role="status"
  7. class="user-info-connection"
  8. >
  9. <span class="connection-banner-sr-only">{{ staticAnnouncement }}</span>
  10. <span aria-hidden="true">
  11. <a
  12. :href="websocketDocsUrl"
  13. target="_blank"
  14. rel="noopener noreferrer"
  15. class="connection-banner-link"
  16. >{{ linkText }}</a>{{ bannerSuffix }}
  17. </span>
  18. </NotificationBlock>
  19. </template>
  20. <script setup>
  21. import { ref, computed, watch, onUnmounted } from 'vue'
  22. import { useI18n } from 'vue-i18n'
  23. import NotificationBlock from 'picocrank/vue/components/NotificationBlock.vue'
  24. import { connectionState } from '../stores/connectionState.js'
  25. const { t } = useI18n()
  26. const websocketDocsUrl = 'https://docs.olivetin.app/troubleshooting/err-websocket-connection.html'
  27. const linkText = computed(() => t('disconnected-banner-link-text'))
  28. function formatShortRelative (ms) {
  29. if (ms < 0) return '0s'
  30. const secs = Math.floor(ms / 1000)
  31. const mins = Math.floor(secs / 60)
  32. const hours = Math.floor(mins / 60)
  33. if (hours > 0) return `${hours}h`
  34. if (mins > 0) return `${mins}m`
  35. return `${secs}s`
  36. }
  37. function formatShortTime (ts) {
  38. if (ts == null) return '--:--'
  39. return new Date(ts).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
  40. }
  41. const now = ref(Date.now())
  42. let ticker = null
  43. watch(() => connectionState.showDisconnectedBanner, (showBanner) => {
  44. if (ticker) {
  45. clearInterval(ticker)
  46. ticker = null
  47. }
  48. if (showBanner) {
  49. now.value = Date.now()
  50. ticker = setInterval(() => { now.value = Date.now() }, 1000)
  51. }
  52. }, { immediate: true })
  53. onUnmounted(() => {
  54. if (ticker) {
  55. clearInterval(ticker)
  56. ticker = null
  57. }
  58. })
  59. const staticAnnouncement = computed(() => t('disconnected-banner-announcement'))
  60. const bannerSuffix = computed(() => {
  61. const at = connectionState.disconnectedAt
  62. const next = connectionState.nextReconnectAt
  63. const n = now.value
  64. const disconnectedSince = formatShortTime(at)
  65. if (next != null && next > n) {
  66. const reconnectIn = formatShortRelative(next - n)
  67. return t('disconnected-banner-suffix', { disconnectedSince, reconnectIn })
  68. }
  69. return t('disconnected-banner-suffix-reconnecting', { disconnectedSince })
  70. })
  71. </script>
  72. <style scoped>
  73. #connection-banner.user-info-connection {
  74. font-weight: 500;
  75. }
  76. .connection-banner-link {
  77. color: inherit;
  78. text-decoration: underline;
  79. }
  80. .connection-banner-sr-only {
  81. position: absolute;
  82. width: 1px;
  83. height: 1px;
  84. padding: 0;
  85. margin: -1px;
  86. overflow: hidden;
  87. clip: rect(0, 0, 0, 0);
  88. white-space: nowrap;
  89. border: 0;
  90. }
  91. </style>