]> source.dussan.org Git - nextcloud-server.git/commitdiff
refactor(app-store): Provide composables for app rating and current category
authorFerdinand Thiessen <opensource@fthiessen.de>
Wed, 23 Oct 2024 10:39:58 +0000 (12:39 +0200)
committerFerdinand Thiessen <opensource@fthiessen.de>
Wed, 23 Oct 2024 11:00:29 +0000 (13:00 +0200)
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
apps/settings/src/composables/useAppRating.ts [new file with mode: 0644]
apps/settings/src/composables/useCurrentCategory.ts [new file with mode: 0644]

diff --git a/apps/settings/src/composables/useAppRating.ts b/apps/settings/src/composables/useAppRating.ts
new file mode 100644 (file)
index 0000000..e96cc90
--- /dev/null
@@ -0,0 +1,31 @@
+/*!
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+import type { IAppStoreApp } from '../constants/AppStoreTypes'
+import type { MaybeRef } from '@vueuse/core'
+
+import { toValue } from '@vueuse/core'
+import { computed } from 'vue'
+
+/**
+ * Get the app rating of an app.
+ * If not enough information is available `undefined` is returned.
+ *
+ * @param app The app to get the rating
+ */
+export function useAppRating(app: MaybeRef<IAppStoreApp>) {
+       const appRating = computed(() => {
+               const appValue = toValue(app)
+               if (appValue.ratingNumRecent > 5) {
+                       return appValue.ratingRecent
+               }
+               // Only show old ratings if there is at least one new rating
+               if (appValue.ratingNumRecent > 0 && appValue.ratingNumOverall > 5) {
+                       return appValue.score
+               }
+               return undefined
+       })
+       return appRating
+}
diff --git a/apps/settings/src/composables/useCurrentCategory.ts b/apps/settings/src/composables/useCurrentCategory.ts
new file mode 100644 (file)
index 0000000..78dec89
--- /dev/null
@@ -0,0 +1,25 @@
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+import { computed } from 'vue'
+import { useRoute } from 'vue-router/composables'
+import { AppStoreSectionNames } from '../constants/AppStoreConstants'
+import { useAppStore } from '../store/appStore'
+
+/**
+ * Get the current category of the app store
+ */
+export function useCurrentCategory() {
+       const route = useRoute()
+       const store = useAppStore()
+
+       const currentCategory = computed(() => route.params.category ?? route.params.name!)
+       const currentCategoryName = computed(() => AppStoreSectionNames[currentCategory.value] ?? store.getCategoryById(currentCategory.value)?.displayName)
+
+       return {
+               currentCategory,
+               /** The display name of the current category */
+               currentCategoryName,
+       }
+}
\ No newline at end of file