This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
COR-754 | Proposal: dynamic loading of Sanity keys in debug mode (#4328)
* wip: dynamic sanity content loading * wip: dynamic sanity content gathering * feat: custom hook for dynamic sanity content loading * fix: update lockfile * fix: revert changes in cms packages * fix: code cleanup * Update packages/app/src/utils/cms/use-dynamic-lokalize-texts.ts * fix: pr feedback Co-authored-by: VWSCoronaDashboard24 <[email protected]>
- Loading branch information
1 parent
74764ad
commit 2ae23bc
Showing
7 changed files
with
118 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import sanityClient, { ClientConfig } from '@sanity/client'; | ||
|
||
const clientConfig: ClientConfig = { | ||
apiVersion: '2021-03-25', | ||
projectId: '5mog5ask', | ||
useCdn: false, | ||
}; | ||
|
||
export function getClient(dataset = 'development') { | ||
return sanityClient({ | ||
...clientConfig, | ||
dataset, | ||
token: process.env.SANITY_TOKEN, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { LokalizeText } from '~/types/cms'; | ||
import { getClient } from './client'; | ||
import { createFlatTexts } from '@corona-dashboard/common'; | ||
import { Dataset } from '~/locale/use-lokalize-text'; | ||
|
||
export const fetchLokalizeTexts = async (dataset: Dataset) => { | ||
const client = getClient(dataset); | ||
const documents: LokalizeText[] = await client.fetch( | ||
`*[_type == 'lokalizeText' && (defined(key)) && !(_id in path('drafts.**'))] | order(key asc)` | ||
); | ||
return createFlatTexts(documents, false); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useIntl } from '~/intl'; | ||
import { SiteText } from '~/locale'; | ||
import { | ||
IS_STAGING_ENV, | ||
mapSiteTextValuesToKeys, | ||
} from '~/locale/use-lokalize-text'; | ||
import { fetchLokalizeTexts } from './fetch-lokalize-texts'; | ||
|
||
export const useDynamicLokalizeTexts = <T extends Record<string, unknown>>( | ||
initialTexts: T, | ||
selector: (text: SiteText) => T | ||
) => { | ||
const [texts, setTexts] = useState<T>(initialTexts); | ||
const { dataset, locale } = useIntl(); | ||
|
||
useEffect(() => { | ||
if (dataset === 'keys') { | ||
fetchLokalizeTexts(IS_STAGING_ENV ? 'production' : 'development') | ||
.catch((err) => { | ||
throw new Error( | ||
`[${useDynamicLokalizeTexts.name}] Error while fetching Sanity content: "${err}"` | ||
); | ||
}) | ||
.then((texts) => texts[locale] as unknown as SiteText) | ||
.then((texts) => mapSiteTextValuesToKeys(texts)) | ||
.then((texts) => setTexts(selector(texts))); | ||
} else { | ||
setTexts(initialTexts); | ||
} | ||
}, [initialTexts, dataset, locale, selector]); | ||
|
||
return texts; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters