Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add all pages to tsconfig (#2897)

Olga Bulatc5b674b819f4
diff --git a/frontend/src/components/VHomepageContent.vue b/frontend/src/components/VHomepageContent.vue
index 040579b97..4e470f0c2 100644
--- a/frontend/src/components/VHomepageContent.vue
+++ b/frontend/src/components/VHomepageContent.vue
@@ -113,13 +113,9 @@ export default defineComponent({
},
},
setup(props, { emit }) {
- const searchTypeButtonRef = ref<InstanceType<
- typeof VSearchTypeButton
- > | null>(null)
- const searchBarRef = ref<InstanceType<typeof VStandaloneSearchBar> | null>(
- null
- )
- const nodeRef = computed(() => searchBarRef.value?.$el as HTMLElement)
+ const searchTypeButtonRef = ref<{ $el: HTMLElement } | null>(null)
+ const searchBarRef = ref<{ $el: HTMLElement } | null>(null)
+ const nodeRef = computed(() => searchBarRef.value?.$el ?? null)
const { getSearchTypeProps } = useSearchType()
const uiStore = useUiStore()
@@ -132,7 +128,7 @@ export default defineComponent({
const isLg = computed(() => uiStore.isBreakpoint("lg"))
const triggerElement = computed(
- () => (searchTypeButtonRef.value?.$el as HTMLElement) || null
+ () => searchTypeButtonRef.value?.$el || null
)
const lockBodyScroll = computed(() => !isLg.value)
diff --git a/frontend/src/constants/screens.js b/frontend/src/constants/screens.js
deleted file mode 100644
index 81f3d1dff..000000000
--- a/frontend/src/constants/screens.js
+++ /dev/null
@@ -1,70 +0,0 @@
-/**
- * This file cannot be converted to TypeScript because it's referenced in the
- * Tailwind config. `RealBreakpoints` excludes 'xs' because it's not defined in
- * Tailwind (being mobile-first) but is used in JavaScript to refer to mobile
- * screens that are narrower than 640px ('sm').
- *
- * @typedef {'2xl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs'} Breakpoint
- * @typedef {Exclude<Breakpoint, 'xs'>} RealBreakpoint
- * @typedef {{ name: string, styles: { width: string, height: string }}} Viewport
- */
-
-/**
- * mapping of breakpoint names to the lower-bound of their screen width range
- */
-const SCREEN_SIZES = Object.freeze(
- /** @type {const} */ ({
- "2xl": 1536,
- xl: 1280,
- lg: 1024,
- md: 768,
- sm: 640,
- })
-)
-
-/**
- * the same as SCREEN_SIZES but with the 'xs' breakpoint added for use in JS.
- */
-const ALL_SCREEN_SIZES = Object.freeze(
- /** @type {const }*/ { ...SCREEN_SIZES, xs: 0 }
-)
-
-/**
- * mapping of breakpoint names to the lower-bound of their screen width range,
- * with the 'px' suffix attached; This is used by Tailwind.
- */
-const SCREENS = /** @type {Record<RealBreakpoint, string>} */ (
- Object.fromEntries(
- Object.entries(SCREEN_SIZES)
- .map(([key, val]) => [key, `${val}px`])
- .reverse()
- )
-)
-
-/**
- * mapping of breakpoint names to viewport specifications; This is used by
- * the viewports plugin of Storybook.
- */
-const VIEWPORTS = /** @type {Record<Breakpoint, Viewport>} */ (
- Object.fromEntries(
- /** @type {[Breakpoint, number][]} */ (Object.entries(SCREEN_SIZES))
- .concat([["xs", 340]])
- .map(([key, val]) => [
- key,
- {
- name: `${key} (${val}px)`,
- styles: { width: `${val}px`, height: "768px" },
- },
- ])
- )
-)
-
-/**
- * This module is consumed by Nuxt and Tailwind config, so it needs to use CJS modules.
- */
-module.exports = {
- ALL_SCREEN_SIZES,
- SCREEN_SIZES,
- SCREENS,
- VIEWPORTS,
-}
diff --git a/frontend/src/constants/screens.ts b/frontend/src/constants/screens.ts
new file mode 100644
index 000000000..8e9e4df6d
--- /dev/null
+++ b/frontend/src/constants/screens.ts
@@ -0,0 +1,56 @@
+/**
+ * `RealBreakpoints` excludes 'xs' because it's not defined in
+ * Tailwind (being mobile-first) but is used in JavaScript to refer to mobile
+ * screens that are narrower than 640px ('sm').
+ */
+export type Breakpoint = "2xl" | "xl" | "lg" | "md" | "sm" | "xs"
+export type RealBreakpoint = Exclude<Breakpoint, "xs">
+export type Viewport = {
+ name: string
+ styles: { width: string; height: string }
+}
+
+/**
+ * Mapping of breakpoint names to the lower-bound of their screen width range.
+ */
+export const SCREEN_SIZES = Object.freeze({
+ "2xl": 1536,
+ xl: 1280,
+ lg: 1024,
+ md: 768,
+ sm: 640,
+} as const)
+
+/**
+ * The same as SCREEN_SIZES but with the 'xs' breakpoint added for use in JS.
+ */
+export const ALL_SCREEN_SIZES = Object.freeze({
+ ...SCREEN_SIZES,
+ xs: 0,
+} as const)
+
+/**
+ * Mapping of breakpoint names to the lower-bound of their screen width range,
+ * with the 'px' suffix attached. This is used by Tailwind.
+ */
+export const SCREENS = Object.fromEntries(
+ Object.entries(SCREEN_SIZES)
+ .map(([key, val]) => [key, `${val}px`])
+ .reverse()
+) as Record<RealBreakpoint, string>
+
+/**
+ * Mapping of breakpoint names to viewport specifications. This is used by
+ * the viewports plugin of Storybook.
+ */
+export const VIEWPORTS = Object.fromEntries(
+ (Object.entries(SCREEN_SIZES) as [Breakpoint, number][])
+ .concat([["xs", 340]])
+ .map(([key, val]) => [
+ key,
+ {
+ name: `${key} (${val}px)`,
+ styles: { width: `${val}px`, height: "768px" },
+ },
+ ])
+) as Record<Breakpoint, Viewport>
diff --git a/frontend/src/pages/index.vue b/frontend/src/pages/index.vue
index 5ac8e9a8d..6289f5b7e 100644
--- a/frontend/src/pages/index.vue
+++ b/frontend/src/pages/index.vue
@@ -21,7 +21,9 @@ import { useMeta, useRouter } from "@nuxtjs/composition-api"
import {
ALL_MEDIA,
- SupportedSearchType,
+ isAdditionalSearchType,
+ isSupportedMediaType,
+ SearchType,
supportedSearchTypes,
} from "~/constants/media"
import { useAnalytics } from "~/composables/use-analytics"
@@ -31,6 +33,8 @@ import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import { useUiStore } from "~/stores/ui"
+import { useFeatureFlagStore } from "~/stores/feature-flag"
+
import VHomeGallery from "~/components/VHomeGallery/VHomeGallery.vue"
import VHomepageContent from "~/components/VHomepageContent.vue"
@@ -43,6 +47,7 @@ export default defineComponent({
setup() {
const router = useRouter()
+ const featureFlagStore = useFeatureFlagStore()
const mediaStore = useMediaStore()
const searchStore = useSearchStore()
const uiStore = useUiStore()
@@ -70,10 +75,17 @@ export default defineComponent({
const isXl = computed(() => uiStore.isBreakpoint("xl"))
- const searchType = ref<SupportedSearchType>(ALL_MEDIA)
-
- const setSearchType = (type: SupportedSearchType) => {
- searchType.value = type
+ const searchType = ref<SearchType>(ALL_MEDIA)
+
+ const setSearchType = (type: SearchType) => {
+ if (type === ALL_MEDIA || isSupportedMediaType(type)) {
+ searchType.value = type
+ } else if (
+ featureFlagStore.isOn("additional_search_types") &&
+ isAdditionalSearchType(type)
+ ) {
+ searchType.value = type
+ }
}
const handleSearch = (searchTerm: string) => {
diff --git a/frontend/src/pages/preferences.vue b/frontend/src/pages/preferences.vue
index 7327857ef..c1318abe3 100644
--- a/frontend/src/pages/preferences.vue
+++ b/frontend/src/pages/preferences.vue
@@ -27,7 +27,7 @@
class="mb-4 last:mb-0"
>
<VCheckbox
- v-if="getFlagStatus(featureData.features[name]) === SWITCHABLE"
+ v-if="isFlagName(name) && isSwitchable(name)"
:id="name"
class="flex-row items-center"
:checked="featureState(name) === ON"
@@ -40,20 +40,20 @@
</div>
<div
- v-for="isSwitchable in [false, true]"
- :key="isSwitchable"
+ v-for="isFlagSwitchable in [false, true]"
+ :key="`${isFlagSwitchable}`"
class="not-prose border-b border-dark-charcoal-20 py-6 last-of-type:border-b-0"
>
<h2 class="label-bold mb-2">
- {{ $t(`prefPage.${isSwitchable ? "s" : "nonS"}witchable.title`) }}
+ {{ $t(`prefPage.${isFlagSwitchable ? "s" : "nonS"}witchable.title`) }}
</h2>
<p class="label-regular mb-4">
- {{ $t(`prefPage.${isSwitchable ? "s" : "nonS"}witchable.desc`) }}
+ {{ $t(`prefPage.${isFlagSwitchable ? "s" : "nonS"}witchable.desc`) }}
</p>
<ul>
<template v-for="(feature, name) in flags">
<li
- v-if="(getFlagStatus(feature) === SWITCHABLE) === isSwitchable"
+ v-if="(getFlagStatus(feature) === SWITCHABLE) === isFlagSwitchable"
:key="name"
class="mb-4 flex flex-row items-center last:mb-0"
>
@@ -61,7 +61,7 @@
:id="name"
class="flex-row items-center"
:checked="featureState(name) === ON"
- :disabled="!isSwitchable"
+ :disabled="!isFlagSwitchable"
is-switch
@change="handleChange"
>
@@ -86,9 +86,15 @@ import { computed, defineComponent } from "vue"
import featureData from "~~/feat/feature-flags.json"
-import { useFeatureFlagStore, getFlagStatus } from "~/stores/feature-flag"
+import {
+ useFeatureFlagStore,
+ getFlagStatus,
+ isFlagName,
+} from "~/stores/feature-flag"
import { SWITCHABLE, ON, OFF, FEATURE_STATES } from "~/constants/feature-flag"
+import type { FeatureFlag } from "~/types/feature-flag"
+
import VContentPage from "~/components/VContentPage.vue"
import VCheckbox from "~/components/VCheckbox/VCheckbox.vue"
@@ -114,11 +120,28 @@ export default defineComponent({
checked,
}: {
name: string
- checked: boolean
+ checked?: boolean
}) => {
+ if (!isFlagName(name)) {
+ throw new Error(
+ `Cannot change the state of flag ${name}: it does not exist.`
+ )
+ }
featureFlagStore.toggleFeature(name, checked ? ON : OFF)
}
+ const isSwitchable = (name: string) => {
+ if (!isFlagName(name)) {
+ throw new Error(
+ `Error getting switchable status for flag ${name}: it does not exist.`
+ )
+ }
+ // TS does not convert the statuses for flag from string to FlagStatus.
+ // It parses the JSON values for env-based statuses as { staging: string; production: string; }
+ const flag = featureData.features[name] as FeatureFlag
+ return getFlagStatus(flag) === SWITCHABLE
+ }
+
return {
ON,
OFF,
@@ -130,9 +153,11 @@ export default defineComponent({
handleChange,
getFlagStatus,
+ isSwitchable,
featureData,
}
},
+ methods: { isFlagName },
})
</script>
diff --git a/frontend/src/pages/privacy.vue b/frontend/src/pages/privacy.vue
index a8fea1a82..d41a2f27b 100644
--- a/frontend/src/pages/privacy.vue
+++ b/frontend/src/pages/privacy.vue
@@ -93,7 +93,7 @@ export default defineComponent({
() => featureFlagStore.featureState("analytics") === ON
)
- const handleChange = ({ checked }: { checked: boolean }) => {
+ const handleChange = ({ checked }: { checked?: boolean }) => {
featureFlagStore.toggleFeature("analytics", checked ? ON : OFF)
}
diff --git a/frontend/src/stores/feature-flag.ts b/frontend/src/stores/feature-flag.ts
index 1a2f1f664..296e8cf16 100644
--- a/frontend/src/stores/feature-flag.ts
+++ b/frontend/src/stores/feature-flag.ts
@@ -25,6 +25,10 @@ import type { Context } from "@nuxt/types"
import type { Dictionary } from "vue-router/types/router"
type FlagName = keyof (typeof featureData)["features"]
+
+export const isFlagName = (name: string): name is FlagName => {
+ return Object.keys(featureData.features).includes(name)
+}
export interface FeatureFlagState {
flags: Record<FlagName, FeatureFlag>
}
diff --git a/tsconfig.json b/tsconfig.json
index 6e7458be5..428489300 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -46,21 +46,13 @@
"**/*.ts",
"frontend/test/proxy.js",
"frontend/src/components/**/*.vue",
-
- "frontend/src/pages/about.vue",
- "frontend/src/pages/feedback.vue",
- "frontend/src/pages/search-help.vue",
- "frontend/src/pages/audio/_id/index.vue",
- "frontend/src/pages/image/_id/index.vue",
- "frontend/src/pages/image/_id/report.vue",
- "frontend/src/pages/search.vue",
- "frontend/src/pages/search/**/*.vue",
+ "frontend/src/pages/**/*.vue",
/**
* Some files from this section cannot be converted to TypeScript because they are
- * used in non-TS contexts (like Tailwind configuration). Therefore they're left as
- * "vanilla JavaScript" with JSDoc based type annotations. This allows us to check
- * them with TypeScript without introducing syntax changes that are not compatible
- * with other tools we rely on.
+ * used in non-TS contexts. Therefore, they're left as "vanilla JavaScript" with
+ * JSDoc based type annotations. This allows us to check them with TypeScript
+ * without introducing syntax changes that are not compatible with other tools
+ * we rely on.
*/
"frontend/src/constants/**/*",
"frontend/src/server-middleware/healthcheck.js",