All files / src/components VersionFooter.vue

84.3% Statements 188/223
61.76% Branches 21/34
66.66% Functions 6/9
84.3% Lines 188/223

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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 4161x 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 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   10x 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 1x 1x       1x 1x 1x 1x   1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x     1x 1x 1x 10x   10x 10x 10x 10x 10x 10x 10x 10x 10x 10x                     10x   10x 10x 10x 10x   10x   10x 10x 10x 10x     10x 10x   10x 10x 10x 10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x     10x             10x   10x     10x   10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x                                                                                                                                                                                                                                                                                                                                        
<template>
  <footer class="version-footer">
    <!-- Error banner (teleported to body) -->
    <Teleport to="body">
      <div
        v-if="status === 'error' && !errorDismissed"
        class="fixed top-0 inset-x-0 z-50 bg-red-900 text-white text-sm px-4 py-2 flex items-center justify-between gap-4"
        data-testid="api-error-banner"
      >
        <span
          >⚠ Could not reach the API server — some features may be
          unavailable.</span
        >
        <div class="flex items-center gap-3 shrink-0">
          <button
            @click="retryFetch"
            class="underline hover:no-underline font-medium"
          >
            Retry
          </button>
          <button
            @click="errorDismissed = true"
            class="font-bold hover:opacity-75"
            aria-label="Dismiss"
          >
            ✕
          </button>
        </div>
      </div>
    </Teleport>
 
    <div class="version-container">
      <!-- Version info (left side) — internal info, members only -->
      <div class="version-info">
        <template v-if="authStore.isAuthenticated.value">
          <template v-if="version">
            <button
              type="button"
              class="version-text version-button"
              title="What's New"
              data-testid="whats-new-trigger"
              @click="$emit('open-whats-new')"
            >
              {{ version }}
            </button>
            <span v-if="environment !== 'production'" class="environment-badge">
              {{ environment }}
            </span>
          </template>
          <span v-else class="version-loading">Loading version...</span>
        </template>
      </div>
 
      <!-- Copyright/branding + support link (center) -->
      <div class="copyright">
        <div>© {{ currentYear }} Missing Table</div>
        <div class="support-line">
          Need help?
          <SupportEmailLink
            :subject="supportSubject"
            :body="supportBody"
            display-text="Contact support"
            class="support-footer-link"
            data-testid="footer-support-link"
          />
        </div>
        <!-- Invite-only: the APK is private; only authenticated users can get a
             short-lived download URL from the backend. Who sees the link is an
             admin's decision (SB-1460): accounts marked Android users, on any
             browser so a parent can find it on a laptop too, and admins. It
             used to follow the browser, which offered the app to every
             Android visitor. -->
        <div
          v-if="
            authStore.isAuthenticated.value &&
            (authStore.isAdmin.value || authStore.state.profile?.android_user)
          "
          class="android-line"
        >
          <button
            type="button"
            class="android-link"
            :disabled="androidLoading"
            title="Android only. After it downloads, allow installs from unknown sources if prompted."
            data-testid="android-install-link"
            @click="downloadAndroidApk"
          >
            📱 {{ androidLoading ? 'Preparing…' : 'Install the Android app' }}
          </button>
          <div
            v-if="androidError"
            class="android-error"
            data-testid="android-install-error"
          >
            {{ androidError }}
          </div>
        </div>
      </div>
 
      <!-- Status indicator (right side) — ops info, admins only -->
      <div class="status-indicator">
        <template v-if="authStore.isAdmin.value && status === 'healthy'">
          <span class="status-dot status-healthy"></span>
          <span class="status-text">Healthy</span>
        </template>
      </div>
    </div>
  </footer>
</template>
 
<script>
import { ref, onMounted, computed } from 'vue';
import { useAuthStore } from '@/stores/auth';
import { getApiBaseUrl } from '@/config/api';
import SupportEmailLink from '@/components/SupportEmailLink.vue';
 
export default {
  name: 'VersionFooter',
  components: { SupportEmailLink },
  emits: ['open-whats-new'],
  setup() {
    const authStore = useAuthStore();
    const version = ref(null);
    const environment = ref('unknown');
    const status = ref('healthy');
    const currentYear = computed(() => new Date().getFullYear());
    const errorDismissed = ref(false);
 
    // Android scorer APK download (SB-313). MT is invite-only, so the APK lives
    // in a PRIVATE R2 bucket. The button (shown only to authenticated users)
    // asks the backend for a short-lived presigned URL and navigates to it —
    // the URL is never embedded in the shipped bundle.
    const androidLoading = ref(false);
    const androidError = ref('');
    const downloadAndroidApk = async () => {
      if (androidLoading.value) return;
      androidLoading.value = true;
      androidError.value = '';
      const url = `${getApiBaseUrl()}/api/android/apk-url`;
      console.info('[android-apk] requesting download url:', url);
      try {
        const res = await authStore.apiRequest(url);
        console.info('[android-apk] response:', res);
        if (res?.download_url) {
          console.info('[android-apk] navigating to presigned url');
          window.location.href = res.download_url;
        } else {
          // Generic user message; real details go to the console only — never
          // surface backend/config internals to end users.
          androidError.value = "The Android app isn't available right now.";
          console.error('[android-apk] no download_url in response:', res);
        }
      } catch (err) {
        androidError.value = "The Android app isn't available right now.";
        console.error('[android-apk] request failed:', err);
      } finally {
        androidLoading.value = false;
      }
    };
 
    // Pre-fill support email with the user's account context when logged in.
    // Falls through to a generic "Help request" subject + empty body for
    // anonymous visitors.
    const supportSubject = computed(() => {
      const email = authStore.state.profile?.email;
      return email ? `[Account: ${email}] Help request` : 'Help request';
    });
    const supportBody = computed(() => {
      const profile = authStore.state.profile;
      if (!profile?.email) return '';
      const lines = [
        'Hi support team,',
        '',
        'I need help with my account.',
        '',
        `Account email: ${profile.email}`,
      ];
      if (profile.display_name)
        lines.push(`Display name: ${profile.display_name}`);
      return lines.join('\n');
    });
 
    // Determine environment based on current hostname
    const determineEnvironment = () => {
      const hostname = window.location.hostname;
      if (hostname === 'localhost' || hostname === '127.0.0.1') {
        return 'local';
      } else if (hostname === 'dev.missingtable.com') {
        return 'dev';
      } else if (
        hostname === 'missingtable.com' ||
        hostname === 'www.missingtable.com'
      ) {
        return 'production';
      }
      return 'unknown';
    };
 
    const fetchVersion = async () => {
      errorDismissed.value = false;
      try {
        const apiBase =
          window.location.hostname === 'localhost' ||
          window.location.hostname === '127.0.0.1'
            ? 'http://localhost:8000'
            : '';
        const response = await fetch(`${apiBase}/api/version`);
 
        if (response.ok) {
          const data = await response.json();
          version.value = data.version;
          status.value = data.status;
          // Set environment based on hostname, not API response
          environment.value = determineEnvironment();
        } else {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
      } catch (error) {
        console.warn('Failed to fetch version info:', error);
        version.value = 'Unknown';
        status.value = 'error';
        // Still set environment based on hostname even if API fails
        environment.value = determineEnvironment();
      }
    };
 
    const retryFetch = () => {
      errorDismissed.value = false;
      fetchVersion();
    };
 
    onMounted(() => {
      fetchVersion();
    });
 
    return {
      authStore,
      version,
      environment,
      status,
      currentYear,
      errorDismissed,
      fetchVersion,
      retryFetch,
      supportSubject,
      supportBody,
      androidLoading,
      androidError,
      downloadAndroidApk,
    };
  },
};
</script>
 
<style scoped>
.version-footer {
  background-color: rgb(var(--color-surface-alt));
  border-top: 1px solid rgb(var(--color-line));
  padding: 0.75rem 1rem;
  margin-top: 2rem;
  font-size: 0.875rem;
  color: rgb(var(--color-fg-muted));
}
 
.version-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}
 
.version-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
 
.version-text {
  font-family: 'Courier New', monospace;
  font-size: 0.8125rem;
  color: rgb(var(--color-fg));
}
 
.version-button {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}
 
.version-button:hover {
  color: #1e40af;
}
 
.version-loading {
  color: rgb(var(--color-fg-muted));
  font-style: italic;
}
 
.environment-badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  background-color: #ffc107;
  color: #212529;
  border-radius: 0.25rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  margin-left: 0.5rem;
}
 
.environment-badge.dev {
  background-color: #17a2b8;
  color: white;
}
 
.copyright {
  text-align: center;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.125rem;
}
 
.support-line {
  font-size: 0.8125rem;
  color: rgb(var(--color-fg-muted));
}
 
.support-footer-link {
  font-weight: 500;
}
 
.android-line {
  font-size: 0.8125rem;
}
 
.android-link {
  /* Rendered as a <button> but styled as an inline link. */
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  cursor: pointer;
  color: rgb(var(--color-fg-muted));
  font-weight: 500;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}
 
.android-link:disabled {
  cursor: default;
  opacity: 0.7;
  text-decoration: none;
}
 
.android-error {
  margin-top: 0.25rem;
  font-size: 0.75rem;
  color: #f87171;
}
 
.android-link:hover {
  color: #1e40af;
  text-decoration: underline;
}
 
.status-indicator {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}
 
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}
 
.status-healthy {
  background-color: #28a745;
  box-shadow: 0 0 4px rgba(40, 167, 69, 0.5);
}
 
.status-text {
  font-size: 0.75rem;
  color: rgb(var(--color-fg-muted));
  text-transform: capitalize;
}
 
/* Responsive adjustments */
@media (max-width: 768px) {
  .version-container {
    flex-direction: column;
    text-align: center;
    gap: 0.5rem;
  }
 
  .copyright {
    order: -1;
  }
 
  .version-info,
  .status-indicator {
    justify-content: center;
  }
}
</style>