Skip to content

Commit

Permalink
fix: load API on detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
toniengelhardt committed Feb 13, 2023
1 parent 0151c95 commit 46241b9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
18 changes: 18 additions & 0 deletions composables/webapis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ export const useWebApiStatus = (api: WebAPI, available?: boolean) => {
return undefined
})
}

export const useLoadWebApis = (webApis?: WebAPI[]) => {
if (navigator) {
const webApiStatuses: Ref<{ [key: keyof typeof webApiData]: boolean }> = useState('webApiStatuses', () => ({}))
webApis = webApis || useWebApiList().value
webApis.forEach((webApi) => {
if (!webApiStatuses.value[webApi.id]) {
const check = webApi.check || defaultWebApiCheck
if (check.constructor.name === 'AsyncFunction') {
(check(webApi) as Promise<boolean>)
.then((available: boolean) => webApiStatuses.value[webApi.id] = available)
} else {
webApiStatuses.value[webApi.id] = check(webApi) as boolean
}
}
})
}
}
15 changes: 13 additions & 2 deletions pages/apis/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const webApiId = computed(() => route.params.id.toString())
const webApi = computed(() => ({ id: webApiId.value, ...webApiData[webApiId.value] }))
const available = computed(() => webAPIStatuses.value[webApiId.value])
useSeoMeta({
title: () => webApi.value.name,
description: () => `Details for "${webApi.value.name}". WebAPI device test for your current device, general information, and requirements.`,
})
const status = computed(() => {
if (available) {
if (webApi.value.experimental) {
Expand All @@ -37,13 +42,17 @@ const status = computed(() => {
}
return undefined
})
onMounted(() => useLoadWebApis([webApi.value]))
</script>

<template>
<div>
<NuxtLayout>
<div max-w-screen-lg mx-auto px-4>
<h1>{{ webApi.name }}</h1>
<h1>
{{ webApi.name }}
</h1>
<div class="status" :class="status?.name">
<span class="status-icon">
<Icon :name="status?.icon || ''" />
Expand All @@ -66,7 +75,9 @@ const status = computed(() => {
Source
</div>
<div class="value">
<ApiSource :api="webApi" />
<ClientOnly>
<ApiSource :api="webApi" />
</ClientOnly>
</div>
</div>
<div>
Expand Down
26 changes: 1 addition & 25 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

<script setup lang="ts">
import Fuse from 'fuse.js'
import * as shvl from 'shvl'
const config = useRuntimeConfig()
Expand Down Expand Up @@ -69,28 +68,5 @@ const filteredAPIs = computed(() => {
const supportedAPICount = computed(() => filteredAPIs.value.filter(api => !!webApiStatuses.value[api.id]).length)
const totalAPICount = computed(() => webApiList.length)
function defaultCheck(api: WebAPI) {
if (api.path) {
const partials = api.path.split('.')
const path = partials[0] === 'window' ? partials.slice(1).join('.') : api.path
return !!shvl.get(window, path, undefined)
}
return false
}
function loadAPIs() {
if (navigator && !Object.keys(webApiStatuses.value).length) {
webApiList.forEach((webApi) => {
const check = webApi.check || defaultCheck
if (check.constructor.name === 'AsyncFunction') {
(check(webApi) as Promise<boolean>)
.then((available: boolean) => webApiStatuses.value[webApi.id] = available)
} else {
webApiStatuses.value[webApi.id] = check(webApi) as boolean
}
})
}
}
onMounted(() => loadAPIs())
onMounted(() => useLoadWebApis())
</script>
10 changes: 10 additions & 0 deletions utils/webapis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import * as shvl from 'shvl'
import DetailBatteryStatusAPI from '~/components/detail/BatteryStatusAPI.vue'
import DetailGeolocationAPI from '~/components/detail/GeolocationAPI.vue'
import DetailNetworkConnectionAPI from '~/components/detail/NetworkConnectionAPI.vue'
import DetailVisualViewport from '~/components/detail/VisualViewport.vue'
import DetailWebCryptoAPI from '~/components/detail/WebCryptoAPI.vue'

export function defaultWebApiCheck(api: WebAPI) {
if (api.path) {
const partials = api.path.split('.')
const path = partials[0] === 'window' ? partials.slice(1).join('.') : api.path
return !!shvl.get(window, path, undefined)
}
return false
}

export const webApiData: { [slug: string]: Omit<WebAPI, 'id'> } = {
'accelerometer': {
name: 'Accelerometer',
Expand Down

0 comments on commit 46241b9

Please sign in to comment.