Skip to content

Commit

Permalink
feat(language-filter): implement fallback component for language-filt…
Browse files Browse the repository at this point in the history
…er (#3204)

(identical implementation to the i18n plugin)
  • Loading branch information
LiamMartens authored and bjoerge committed Mar 28, 2022
1 parent d6d4351 commit 75e75c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/@sanity/language-filter/src/SelectLanguageProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// @todo: remove the following line when part imports has been removed from this file
///<reference types="@sanity/types/parts" />

import React, {useRef, useEffect, useState} from 'react'
import React, {useRef, useEffect, useState, useMemo} from 'react'
import config from 'part:@sanity/language-filter/config'
import {SchemaType} from '@sanity/types'
import {Subscription} from 'rxjs'
import languageFilterImplementations from 'all:part:@sanity/desk-tool/language-select-component'
import {selectedLanguages$, setLangs} from './datastore'
import SelectLanguage from './SelectLanguage'

Expand All @@ -16,6 +17,20 @@ const SelectLanguageProvider = ({schemaType}: Props) => {
const subscriptionRef$ = useRef<Subscription | null>(null)
const [selected, setSelected] = useState<string[]>([])
const [currentDocumentType, setCurrentDocumentType] = useState<string | undefined>()
const shouldShow = useMemo(() => {
const {documentTypes} = config
return !!(documentTypes && schemaType?.name && documentTypes.includes(schemaType.name))
}, [schemaType])
const FallbackImplementation = useMemo(() => {
if (languageFilterImplementations && Array.isArray(languageFilterImplementations)) {
return (
languageFilterImplementations?.filter(
(component) => SelectLanguageProvider !== component
)?.[0] ?? null
)
}
return null
}, [])

useEffect(() => {
setCurrentDocumentType(schemaType?.name)
Expand All @@ -32,16 +47,24 @@ const SelectLanguageProvider = ({schemaType}: Props) => {
}
}, [])

return (
<SelectLanguage
languages={config.supportedLanguages}
defaultLanguages={config.defaultLanguages}
documentTypes={config.documentTypes}
currentDocumentType={currentDocumentType}
selected={selected}
onChange={setLangs}
/>
)
if (shouldShow) {
return (
<SelectLanguage
languages={config.supportedLanguages}
defaultLanguages={config.defaultLanguages}
documentTypes={config.documentTypes}
currentDocumentType={currentDocumentType}
selected={selected}
onChange={setLangs}
/>
)
}

if (FallbackImplementation) {
return <FallbackImplementation schemaType={schemaType} />
}

return null
}

export default SelectLanguageProvider
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
declare module 'all:part:@sanity/desk-tool/language-select-component' {
const implementations:
| React.FC<{
schemaType?: import('@sanity/types').SchemaType
}>[]
| undefined
export default implementations
}

declare module 'part:@sanity/language-filter/config' {
interface Config {
supportedLanguages: {
Expand Down

0 comments on commit 75e75c5

Please sign in to comment.