Skip to content

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

Extract use-external-sources composable (#2826)

Olga Bulat1a2d4a621397
diff --git a/frontend/src/components/VErrorSection/VNoResults.vue b/frontend/src/components/VErrorSection/VNoResults.vue
index 8d3e78b05..ff98c026e 100644
--- a/frontend/src/components/VErrorSection/VNoResults.vue
+++ b/frontend/src/components/VErrorSection/VNoResults.vue
@@ -28,35 +28,18 @@
</template>
<script lang="ts">
-import { defineComponent, onMounted, PropType } from "vue"
+import { defineComponent, onMounted } from "vue"
import { useAnalytics } from "~/composables/use-analytics"
+import { useExternalSources } from "~/composables/use-external-sources"
import { useUiStore } from "~/stores/ui"
-import type { ExternalSource } from "~/types/external-source"
-
-import type { MediaType } from "~/constants/media"
-
import VButton from "~/components/VButton.vue"
export default defineComponent({
name: "VNoResults",
components: { VButton },
props: {
- /**
- * The list of external sources information: their name and url.
- */
- externalSources: {
- type: Array as PropType<ExternalSource[]>,
- required: true,
- },
- /**
- * The media type to use as the criteria for filtering additional sources
- */
- mediaType: {
- type: String as PropType<MediaType>,
- required: true,
- },
/**
* The search term for which the external sources links are generated.
*/
@@ -66,11 +49,12 @@ export default defineComponent({
},
},
setup(props) {
+ const { externalSources, externalSourcesType } = useExternalSources()
const { sendCustomEvent } = useAnalytics()
const handleClick = (sourceName: string) => {
sendCustomEvent("SELECT_EXTERNAL_SOURCE", {
name: sourceName,
- mediaType: props.mediaType,
+ mediaType: externalSourcesType.value,
query: props.searchTerm,
component: "VNoResults",
})
@@ -83,6 +67,7 @@ export default defineComponent({
return {
handleClick,
+ externalSources,
}
},
})
diff --git a/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx b/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx
index 5558332e0..9af1bc9a3 100644
--- a/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx
+++ b/frontend/src/components/VErrorSection/meta/VErrorSection.stories.mdx
@@ -12,7 +12,6 @@ import VNoResults from "~/components/VErrorSection/VNoResults.vue"
import VServerTimeout from "~/components/VErrorSection/VServerTimeout.vue"
import { NO_RESULT, SERVER_TIMEOUT } from "~/constants/errors"
-import { INCLUDE_SENSITIVE_QUERY_PARAM } from "~/constants/content-safety"
<Meta title="Components/VErrorSection" component={VErrorSection} />
@@ -32,7 +31,7 @@ export const NoResultsTemplate = (args) => ({
<template #image>
<VErrorImage :error-code="args.errorCode" />
</template>
- <VNoResults :media-type="args.type" :search-term="args.searchTerm" :external-sources=[] />
+ <VNoResults :media-type="args.type" :search-term="args.searchTerm" />
</VErrorSection>
`,
components: { VErrorSection, VErrorImage, VNoResults },
diff --git a/frontend/src/components/VExternalSearch/VExternalSearchForm.vue b/frontend/src/components/VExternalSearch/VExternalSearchForm.vue
index 449f031d0..922c56790 100644
--- a/frontend/src/components/VExternalSearch/VExternalSearchForm.vue
+++ b/frontend/src/components/VExternalSearch/VExternalSearchForm.vue
@@ -1,6 +1,6 @@
<template>
<section
- :key="type"
+ :key="externalSourcesType"
ref="sectionRef"
class="external-sources flex flex-row place-items-center justify-center py-4"
data-testid="external-sources-form"
@@ -19,7 +19,9 @@
class="description-regular"
>
<template #openverse>Openverse</template>
- <template #type>{{ $t(`externalSources.form.types.${type}`) }}</template>
+ <template #type>{{
+ $t(`externalSources.form.types.${externalSourcesType}`)
+ }}</template>
</i18n>
<i18n
@@ -28,7 +30,9 @@
tag="p"
class="description-regular"
>
- <template #type>{{ $t(`externalSources.form.types.${type}`) }}</template>
+ <template #type>{{
+ $t(`externalSources.form.types.${externalSourcesType}`)
+ }}</template>
<template #query>{{ searchTerm }}</template>
</i18n>
@@ -64,8 +68,6 @@
>
<VExternalSourceList
class="flex flex-col"
- :external-sources="externalSources"
- :media-type="type"
:search-term="searchTerm"
@close="closeDialog"
/>
@@ -75,7 +77,7 @@
</template>
<script lang="ts">
-import { computed, defineComponent, PropType, ref, SetupContext } from "vue"
+import { computed, defineComponent, ref, SetupContext } from "vue"
import { storeToRefs } from "pinia"
@@ -87,9 +89,7 @@ import { useMediaStore } from "~/stores/media"
import { useDialogControl } from "~/composables/use-dialog-control"
import { useAnalytics } from "~/composables/use-analytics"
-
-import type { MediaType } from "~/constants/media"
-import type { ExternalSource } from "~/types/external-source"
+import { useExternalSources } from "~/composables/use-external-sources"
import VExternalSourceList from "~/components/VExternalSearch/VExternalSourceList.vue"
import VButton from "~/components/VButton.vue"
@@ -107,18 +107,10 @@ export default defineComponent({
VExternalSourceList,
},
props: {
- type: {
- type: String as PropType<MediaType>,
- required: true,
- },
searchTerm: {
type: String,
required: true,
},
- externalSources: {
- type: Array as PropType<ExternalSource[]>,
- required: true,
- },
isSupported: {
type: Boolean,
default: false,
@@ -133,14 +125,14 @@ export default defineComponent({
},
setup(_, { emit }) {
const sectionRef = ref<HTMLElement | null>(null)
- const triggerRef = ref<InstanceType<typeof VButton> | null>(null)
+ const triggerRef = ref<{ $el: HTMLElement } | null>(null)
const uiStore = useUiStore()
const searchStore = useSearchStore()
const { sendCustomEvent } = useAnalytics()
const isMd = computed(() => uiStore.isBreakpoint("md"))
- const triggerElement = computed(() => triggerRef.value?.$el as HTMLElement)
+ const triggerElement = computed(() => triggerRef.value?.$el)
const lockBodyScroll = computed(() => !isMd.value)
@@ -172,7 +164,10 @@ export default defineComponent({
onTriggerClick()
}
+ const { externalSourcesType } = useExternalSources()
+
return {
+ externalSourcesType,
sectionRef,
triggerRef,
triggerElement,
diff --git a/frontend/src/components/VExternalSearch/VExternalSourceList.vue b/frontend/src/components/VExternalSearch/VExternalSourceList.vue
index ee572d474..1b3001034 100644
--- a/frontend/src/components/VExternalSearch/VExternalSourceList.vue
+++ b/frontend/src/components/VExternalSearch/VExternalSourceList.vue
@@ -29,12 +29,11 @@
</template>
<script lang="ts">
-import { defineComponent, PropType } from "vue"
+import { defineComponent } from "vue"
import { useAnalytics } from "~/composables/use-analytics"
-import type { ExternalSource } from "~/types/external-source"
-import type { MediaType } from "~/constants/media"
+import { useExternalSources } from "~/composables/use-external-sources"
import VButton from "~/components/VButton.vue"
import VCloseButton from "~/components/VCloseButton.vue"
@@ -47,13 +46,6 @@ export default defineComponent({
name: "VExternalSourceList",
components: { VCloseButton, VButton },
props: {
- /**
- * The media type to use as the criteria for filtering additional sources
- */
- mediaType: {
- type: String as PropType<MediaType>,
- required: true,
- },
/**
* The search term for which the external sources links are generated.
*/
@@ -61,26 +53,23 @@ export default defineComponent({
type: String,
required: true,
},
- /**
- * The list of external sources information: their name and url.
- */
- externalSources: {
- type: Array as PropType<ExternalSource[]>,
- required: true,
- },
},
setup(props) {
+ const { externalSources, externalSourcesType } = useExternalSources()
+
const { sendCustomEvent } = useAnalytics()
const handleClick = (sourceName: string) => {
sendCustomEvent("SELECT_EXTERNAL_SOURCE", {
name: sourceName,
- mediaType: props.mediaType,
+ mediaType: externalSourcesType.value,
query: props.searchTerm,
component: "VExternalSourceList",
})
}
return {
+ externalSources,
+ externalSourcesType,
handleClick,
}
},
diff --git a/frontend/src/components/VSearchGrid.vue b/frontend/src/components/VSearchGrid.vue
index fcf8fb0a8..0f1d42f39 100644
--- a/frontend/src/components/VSearchGrid.vue
+++ b/frontend/src/components/VSearchGrid.vue
@@ -10,9 +10,7 @@
<VExternalSearchForm
v-if="!isAllView"
- :type="externalSourcesType"
:has-no-results="hasNoResults"
- :external-sources="externalSources"
:search-term="searchTerm"
:is-supported="supported"
/>
@@ -21,30 +19,18 @@
<template #image>
<VErrorImage error-code="NO_RESULT" />
</template>
- <VNoResults
- :external-sources="externalSources"
- :search-term="searchTerm"
- :media-type="externalSourcesType"
- />
+ <VNoResults :search-term="searchTerm" />
</VErrorSection>
</template>
<script lang="ts">
import { computed, defineComponent, PropType } from "vue"
-import {
- ALL_MEDIA,
- IMAGE,
- isAdditionalSearchType,
- isSupportedMediaType,
- SearchType,
-} from "~/constants/media"
+import { ALL_MEDIA, SearchType } from "~/constants/media"
import { NO_RESULT } from "~/constants/errors"
import { defineEvent } from "~/types/emits"
import type { FetchState } from "~/types/fetch-state"
import type { ApiQueryParams } from "~/utils/search-query-transform"
-import { getAdditionalSources } from "~/utils/get-additional-sources"
-import { useFeatureFlagStore } from "~/stores/feature-flag"
import VExternalSearchForm from "~/components/VExternalSearch/VExternalSearchForm.vue"
import VErrorSection from "~/components/VErrorSection/VErrorSection.vue"
@@ -99,29 +85,8 @@ export default defineComponent({
: false
})
- /**
- * External sources search form shows the external sources for current search type, or for images if the search type is 'All Content'.
- */
- const externalSourcesType = computed(() => {
- const featureFlagStore = useFeatureFlagStore()
- if (isSupportedMediaType(props.searchType)) {
- return props.searchType
- }
- if (
- featureFlagStore.isOn("additional_search_types") &&
- isAdditionalSearchType(props.searchType)
- ) {
- return props.searchType
- }
- return IMAGE
- })
-
const isAllView = computed(() => props.searchType === ALL_MEDIA)
- const externalSources = computed(() =>
- getAdditionalSources(externalSourcesType.value, props.query)
- )
-
const searchTerm = computed(() => props.query.q || "")
const showError = computed(() => {
@@ -134,10 +99,8 @@ export default defineComponent({
return {
hasNoResults,
- externalSourcesType,
isAllView,
NO_RESULT,
- externalSources,
searchTerm,
showError,
}
diff --git a/frontend/src/composables/use-external-sources.ts b/frontend/src/composables/use-external-sources.ts
new file mode 100644
index 000000000..301a2e628
--- /dev/null
+++ b/frontend/src/composables/use-external-sources.ts
@@ -0,0 +1,43 @@
+import { computed } from "vue"
+
+import { useFeatureFlagStore } from "~/stores/feature-flag"
+import { useSearchStore } from "~/stores/search"
+import {
+ IMAGE,
+ isAdditionalSearchType,
+ isSupportedMediaType,
+} from "~/constants/media"
+import { getAdditionalSources } from "~/utils/get-additional-sources"
+
+/**
+ * This component uses `useStore` composables, and should only be called from within a component's setup function.
+ */
+export const useExternalSources = () => {
+ const featureFlagStore = useFeatureFlagStore()
+ const searchStore = useSearchStore()
+ /**
+ * Show the sources of the current media type, or fall back to "image"
+ * when the search type is not supported.
+ */
+ const externalSourcesType = computed(() => {
+ const searchType = searchStore.searchType
+ if (
+ isSupportedMediaType(searchType) ||
+ (featureFlagStore.isOn("additional_search_types") &&
+ isAdditionalSearchType(searchType))
+ ) {
+ return searchType
+ }
+ return IMAGE
+ })
+ const externalSources = computed(() => {
+ const query = searchStore.searchQueryParams
+ const type = externalSourcesType.value
+ return getAdditionalSources(type, query)
+ })
+
+ return {
+ externalSources,
+ externalSourcesType,
+ }
+}
diff --git a/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js b/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js
index e4f231f22..44c299eda 100644
--- a/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js
+++ b/frontend/test/unit/specs/components/ExternalSources/v-external-source-list.spec.js
@@ -5,6 +5,7 @@ import { render } from "~~/test/unit/test-utils/render"
import { useAnalytics } from "~/composables/use-analytics"
import { IMAGE } from "~/constants/media"
+import { getAdditionalSources } from "~/utils/get-additional-sources"
import VExternalSourceList from "~/components/VExternalSearch/VExternalSourceList.vue"
@@ -15,6 +16,8 @@ jest.mock("~/composables/use-analytics", () => ({
describe("VExternalSourceList", () => {
let propsData
let sendCustomEventMock
+ const searchTerm = "cat"
+ const expectedImageSources = getAdditionalSources(IMAGE, { q: searchTerm })
beforeEach(() => {
sendCustomEventMock = jest.fn()
@@ -22,12 +25,8 @@ describe("VExternalSourceList", () => {
sendCustomEvent: sendCustomEventMock,
}))
propsData = {
- searchTerm: "cat",
+ searchTerm,
mediaType: IMAGE,
- externalSources: [
- { name: "source1", url: "https://source1.com/?q=cat" },
- { name: "source2", url: "https://source2.com/?q=cat" },
- ],
}
render(VExternalSourceList, {
props: propsData,
@@ -36,13 +35,13 @@ describe("VExternalSourceList", () => {
it("should render external sources links", () => {
expect(screen.queryAllByRole("link")).toHaveLength(
- propsData.externalSources.length
+ expectedImageSources.length
)
})
it("should send SELECT_EXTERNAL_SOURCE analytics event on CTA button click", async () => {
const source1Link = screen.getByRole("link", {
- name: propsData.externalSources[0].name,
+ name: expectedImageSources[0].name,
})
const user = userEvent.setup()
@@ -50,7 +49,7 @@ describe("VExternalSourceList", () => {
expect(sendCustomEventMock).toHaveBeenCalledWith("SELECT_EXTERNAL_SOURCE", {
mediaType: IMAGE,
- name: propsData.externalSources[0].name,
+ name: expectedImageSources[0].name,
query: propsData.searchTerm,
component: "VExternalSourceList",
})