Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Stop opening `/search/video` from crashing the app (#1430)
Olga Bulatc83ae8521d7f
diff --git a/src/components/VContentSwitcher/VSearchTypeButton.vue b/src/components/VContentSwitcher/VSearchTypeButton.vueindex 184541333..fc68341e2 100644--- a/src/components/VContentSwitcher/VSearchTypeButton.vue+++ b/src/components/VContentSwitcher/VSearchTypeButton.vue@@ -23,14 +23,19 @@/></VButton></template>-<script>-import { computed, defineComponent, inject } from '@nuxtjs/composition-api'+<script lang="ts">+import {+ computed,+ defineComponent,+ inject,+ PropType,+ ref,+} from '@nuxtjs/composition-api'-import { ALL_MEDIA } from '~/constants/media'+import { ALL_MEDIA, SearchType } from '~/constants/media'import useSearchType from '~/composables/use-search-type'import { useI18n } from '~/composables/use-i18n'import { isMinScreen } from '~/composables/use-media-query'-import { isValidSearchType } from '~/utils/prop-validators'import VIcon from '~/components/VIcon/VIcon.vue'import VButton from '~/components/VButton.vue'@@ -46,19 +51,17 @@ export default defineComponent({required: true,},activeItem: {- type: String,+ type: String as PropType<SearchType>,default: ALL_MEDIA,- validator: isValidSearchType,},type: {- type: String,+ type: String as PropType<'header' | 'searchbar'>,default: 'header',- validator: (v) => ['header', 'searchbar'].includes(v),},},setup(props) {const i18n = useI18n()- const isHeaderScrolled = inject('isHeaderScrolled', null)+ const isHeaderScrolled = inject('isHeaderScrolled', ref(null))const isMinScreenMd = isMinScreen('md', { shouldPassInSSR: true })const { icons, activeType: activeItem } = useSearchType()diff --git a/src/components/VContentSwitcher/VSearchTypePopover.vue b/src/components/VContentSwitcher/VSearchTypePopover.vueindex 94b1c8268..da532cf62 100644--- a/src/components/VContentSwitcher/VSearchTypePopover.vue+++ b/src/components/VContentSwitcher/VSearchTypePopover.vue@@ -2,7 +2,7 @@<VPopoverref="contentMenuPopover"class="flex items-stretch"- :label="$t('search-type.label')"+ :label="$t('search-type.label').toString()"placement="bottom-end"><template #trigger="{ a11yProps }">@@ -28,10 +28,18 @@</VPopover></template>-<script>-import { computed, defineComponent, ref } from '@nuxtjs/composition-api'+<script lang="ts">+import {+ ComponentInstance,+ computed,+ defineComponent,+ PropType,+ ref,+} from '@nuxtjs/composition-api'import useSearchType from '~/composables/use-search-type'+import type { SearchType } from '~/constants/media'+import { defineEvent } from '~/types/emits'import VPopover from '~/components/VPopover/VPopover.vue'import VSearchTypeButton from '~/components/VContentSwitcher/VSearchTypeButton.vue'@@ -52,23 +60,25 @@ export default defineComponent({},props: {activeItem: {- type: String,+ type: String as PropType<SearchType>,required: true,},placement: {- type: String,+ type: String as PropType<'header' | 'searchbar'>,default: 'header',},},+ emits: {+ select: defineEvent<SearchType>(),+ },setup(props, { emit }) {const content = useSearchType()- const contentMenuPopover = ref(null)+ const contentMenuPopover = ref<ComponentInstance | null>(null)/*** When in the searchbar, content switcher button has a border when the* search bar group is hovered on.- * @type {ComputedRef<boolean>}*/const isInSearchBar = computed(() => props.placement === 'searchbar')@@ -81,7 +91,7 @@ export default defineComponent({}}- const selectItem = (item) => {+ const selectItem = (item: SearchType) => {emit('select', item)}diff --git a/src/components/VContentSwitcher/VSearchTypes.vue b/src/components/VContentSwitcher/VSearchTypes.vueindex 58d716ea6..16ef2e62c 100644--- a/src/components/VContentSwitcher/VSearchTypes.vue+++ b/src/components/VContentSwitcher/VSearchTypes.vue@@ -35,12 +35,12 @@</div></VItemGroup></template>-<script>-import { computed, defineComponent } from '@nuxtjs/composition-api'+<script lang="ts">+import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'import { isDev } from '~/utils/node-env'-import { supportedSearchTypes } from '~/constants/media'+import type { SearchType } from '~/constants/media'import useSearchType from '~/composables/use-search-type'import VItemGroup from '~/components/VItemGroup/VItemGroup.vue'@@ -55,14 +55,12 @@ export default defineComponent({* 'medium' size for larger screens.*/size: {- type: String,+ type: String as PropType<'small' | 'medium'>,default: 'small',- validator: (val) => ['small', 'medium'].includes(val),},activeItem: {- type: String,+ type: String as PropType<SearchType>,required: true,- validator: (val) => supportedSearchTypes.includes(val),},useLinks: {type: Boolean,diff --git a/src/components/VErrorSection/VNoResults.vue b/src/components/VErrorSection/VNoResults.vueindex b87ac844c..22e48e48c 100644--- a/src/components/VErrorSection/VNoResults.vue+++ b/src/components/VErrorSection/VNoResults.vue@@ -10,22 +10,24 @@</div></template>-<script>-import { isValidSearchType } from '~/utils/prop-validators'+<script lang="ts">+import { defineComponent, PropType } from '@nuxtjs/composition-api'++import type { MediaType } from '~/constants/media'+import type { ApiQueryParams } from '~/utils/search-query-transform'import VMetaSourceList from '~/components/VMetaSearch/VMetaSourceList.vue'-export default {+export default defineComponent({name: 'VNoResults',components: { VMetaSourceList },props: {type: {- type: String,- validator: isValidSearchType,+ type: String as PropType<MediaType>,},query: {- type: Object,+ type: Object as PropType<ApiQueryParams>,},},-}+})</script>diff --git a/src/components/VSearchGrid.vue b/src/components/VSearchGrid.vueindex 1ce5bb4e4..fcec1f9b1 100644--- a/src/components/VSearchGrid.vue+++ b/src/components/VSearchGrid.vue@@ -38,10 +38,9 @@<script lang="ts">import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'-import { ALL_MEDIA, IMAGE, SupportedSearchType } from '~/constants/media'+import { ALL_MEDIA, IMAGE, SearchType, mediaTypes } from '~/constants/media'import { NO_RESULT } from '~/constants/errors'import { defineEvent } from '~/types/emits'-import type { ApiQueryParams } from '~/utils/search-query-transform'import type { FetchState } from '~/composables/use-fetch-state'@@ -70,7 +69,7 @@ export default defineComponent({required: true,},searchType: {- type: String as PropType<SupportedSearchType>,+ type: String as PropType<SearchType>,required: true,},fetchState: {@@ -93,9 +92,17 @@ export default defineComponent({? props.query.q !== '' && props.resultsCount === 0: false})++ /**+ * Metasearch form shows the external sources for current search type, or for images if the search type is 'All Content'.+ */const metaSearchFormType = computed(() => {- return props.searchType === ALL_MEDIA ? IMAGE : props.searchType+ if (mediaTypes.includes(props.searchType)) {+ return props.searchType+ }+ return IMAGE})+const isAllView = computed(() => {return props.searchType === ALL_MEDIA})diff --git a/src/composables/use-search-type.js b/src/composables/use-search-type.jsindex f0d885bef..1defd1ff8 100644--- a/src/composables/use-search-type.js+++ b/src/composables/use-search-type.js@@ -6,6 +6,7 @@ import {AUDIO,IMAGE,MODEL_3D,+ VIDEO,} from '~/constants/media'import { useSearchStore } from '~/stores/search'@@ -13,19 +14,23 @@ import { useSearchStore } from '~/stores/search'import allIcon from '~/assets/icons/all-content.svg'import audioIcon from '~/assets/icons/audio-content.svg'import imageIcon from '~/assets/icons/image-content.svg'+import videoIcon from '~/assets/icons/video-content.svg'import model3dIcon from '~/assets/icons/model-3d.svg'const icons = {[ALL_MEDIA]: allIcon,[AUDIO]: audioIcon,[IMAGE]: imageIcon,+ [VIDEO]: videoIcon,[MODEL_3D]: model3dIcon,}const searchTypes = [...supportedSearchTypes]export default function useSearchType() {const activeType = computed(() => useSearchStore().searchType)+const previousSearchType = ref(activeType.value)+const setActiveType = (searchType) => {if (previousSearchType.value === searchType) returnuseSearchStore().setSearchType(searchType)diff --git a/src/stores/media/index.ts b/src/stores/media/index.tsindex c6beda371..bb1f941be 100644--- a/src/stores/media/index.ts+++ b/src/stores/media/index.ts@@ -18,7 +18,7 @@ import {supportedMediaTypes,} from '~/constants/media'import { services } from '~/stores/media/services'-import { useSearchStore } from '~/stores/search'+import { isSearchTypeSupported, useSearchStore } from '~/stores/search'import { useRelatedMediaStore } from '~/stores/media/related-media'import { deepFreeze } from '~/utils/deep-freeze'@@ -61,7 +61,8 @@ export const useMediaStore = defineStore('media', {getters: {_searchType() {- return useSearchStore().searchType+ const searchStore = useSearchStore()+ return searchStore.searchType},getItemById: (state) => {return (mediaType: SupportedMediaType, id: string): Media | undefined => {@@ -92,12 +93,13 @@ export const useMediaStore = defineStore('media', {},/**- * Returns the total count of results for selected search type, sums all media results for ALL_MEDIA.+ * Returns the total count of results for selected search type, sums all media results for ALL_MEDIA or additional types.* If the count is more than 10000, returns 10000 to match the API result.*/resultCount(state) {const types = (- this._searchType === ALL_MEDIA+ this._searchType === ALL_MEDIA ||+ !isSearchTypeSupported(this._searchType)? supportedMediaTypes: [this._searchType]) as SupportedMediaType[]@@ -139,8 +141,10 @@ export const useMediaStore = defineStore('media', {(type) => this.mediaFetchState[type].isFinished),}- } else {+ } else if (isSearchTypeSupported(this._searchType)) {return this.mediaFetchState[this._searchType]+ } else {+ return initialFetchState}},diff --git a/src/stores/search.ts b/src/stores/search.tsindex ca77c6100..19af9a0b1 100644--- a/src/stores/search.ts+++ b/src/stores/search.ts@@ -7,6 +7,7 @@ import {ALL_MEDIA,AUDIO,IMAGE,+ SearchType,SupportedMediaType,SupportedSearchType,supportedSearchTypes,@@ -28,8 +29,14 @@ import {import type { Dictionary } from 'vue-router/types/router'+export const isSearchTypeSupported = (+ st: SearchType+): st is SupportedSearchType => {+ return supportedSearchTypes.includes(st as SupportedSearchType)+}+export interface SearchState {- searchType: SupportedSearchType+ searchType: SearchTypesearchTerm: stringfilters: Filters}@@ -70,11 +77,15 @@ export const useSearchStore = defineStore('search', {* drops all parameters with blank values.*/searchQueryParams(state) {- return computeQueryParams(- state.searchType,- state.filters,- state.searchTerm- )+ if (isSearchTypeSupported(state.searchType)) {+ return computeQueryParams(+ state.searchType,+ state.filters,+ state.searchTerm+ )+ } else {+ return { q: state.searchTerm }+ }},/*** Returns the number of checked filters, excluding the `mature` filter.@@ -114,10 +125,10 @@ export const useSearchStore = defineStore('search', {)},/**- * Returns whether the current `seartchType` is a supported media type for search.+ * Returns whether the current `searchType` is a supported media type for search.*/searchTypeIsSupported(state) {- return supportedSearchTypes.includes(state.searchType)+ return isSearchTypeSupported(state.searchType)},},actions: {@@ -259,6 +270,7 @@ export const useSearchStore = defineStore('search', {this.setSearchTerm(urlQuery.q.trim())}this.searchType = queryStringToSearchType(path)+ if (!isSearchTypeSupported(this.searchType)) return// When setting filters from URL query, 'mature' has a value of 'true',// but we need the 'mature' code. Creating a local shallow copy to prevent mutation.const query = { ...urlQuery }diff --git a/src/utils/search-query-transform.ts b/src/utils/search-query-transform.tsindex 85ac63a81..7139f4087 100644--- a/src/utils/search-query-transform.ts+++ b/src/utils/search-query-transform.ts@@ -4,7 +4,13 @@ import {Filters,mediaFilterKeys,} from '~/constants/filters'-import { ALL_MEDIA, SupportedSearchType } from '~/constants/media'+import {+ ALL_MEDIA,+ mediaTypes,+ SearchType,+ SupportedSearchType,+ supportedSearchTypes,+} from '~/constants/media'import { getParameterByName } from '~/utils/url-params'import { deepClone } from '~/utils/clone'@@ -47,17 +53,11 @@ const filterPropertyMappings: Record<FilterCategory, ApiQueryKeys> = {mature: 'mature',}-const getMediaFilterTypes = (searchType: SupportedSearchType) => [- ...mediaFilterKeys[searchType],-]-// {-// license: 'cc0,pdm,by,by-sa,by-nc,by-nd,by-nc-sa,by-nc-nd',-// imageCategories: 'photograph,illustration,digitized_artwork',-// imageExtension: 'jpg,png',-// aspect_ratio: 'square',-// size: 'small',-// source: 'animaldiversity,bio_diversity,brooklynmuseum,CAPL,clevelandmuseum,deviantart'-// }+const getMediaFilterTypes = (searchType: SearchType) => {+ return supportedSearchTypes.includes(searchType as SupportedSearchType)+ ? [...mediaFilterKeys[searchType]]+ : []+}/*** Joins all the filters which have the checked property `true`@@ -99,19 +99,15 @@ export const filtersToQueryData = (/*** Extract search type from the url. Returns the last part- * of the path between `/search/` and query, or `all` by default.- * `/search/?q=test`: all- * `/search/image?q=test`: image- * @param queryString - the query string from the url+ * of the path after `/search/`, or `all` by default.+ * `/search/`: all+ * `/search/image`: image+ * @param queryString - the query path string from the url*/-export const queryStringToSearchType = (- queryString: string-): SupportedSearchType => {- const searchTypePattern = /\/search\/(image|audio|video)\?*/+export const queryStringToSearchType = (queryString: string): SearchType => {+ const searchTypePattern = new RegExp(`/search/(${mediaTypes.join('|')})`)const matchedType = queryString.match(searchTypePattern)- return matchedType === null- ? ALL_MEDIA- : (matchedType[1] as SupportedSearchType)+ return matchedType === null ? ALL_MEDIA : (matchedType[1] as SearchType)}/**@@ -233,6 +229,7 @@ export const queryStringToQueryData = (queryString: string) => {queryString)})+queryDataObject.q = getParameterByName('q', queryString)return queryDataObject