feat: improve Bible version selection logic and language switching
- Auto-select appropriate version when switching languages - Prioritize favorite version only if it matches current locale - Fall back to locale-specific default version - Redirect Bible pages to reader root when changing language to allow proper version selection 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -350,12 +350,15 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
|
|||||||
if (data.success && data.versions) {
|
if (data.success && data.versions) {
|
||||||
setVersions(data.versions)
|
setVersions(data.versions)
|
||||||
|
|
||||||
// Only auto-select if there's NO current selection
|
// Check if current version is available in the new locale's versions
|
||||||
if (!selectedVersion) {
|
const currentVersionAvailable = selectedVersion && data.versions.find((v: BibleVersion) => v.id === selectedVersion)
|
||||||
// Try to load user's favorite version first
|
|
||||||
|
// Auto-select if there's NO current selection OR if current version is not available in new locale
|
||||||
|
if (!selectedVersion || (!showAllVersions && !currentVersionAvailable)) {
|
||||||
let versionToSelect = null
|
let versionToSelect = null
|
||||||
|
|
||||||
if (user) {
|
// Try to load user's favorite version first, but only if it matches the current locale
|
||||||
|
if (user && !showAllVersions) {
|
||||||
const token = localStorage.getItem('authToken')
|
const token = localStorage.getItem('authToken')
|
||||||
if (token) {
|
if (token) {
|
||||||
try {
|
try {
|
||||||
@@ -365,6 +368,7 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
|
|||||||
const favoriteData = await favoriteRes.json()
|
const favoriteData = await favoriteRes.json()
|
||||||
|
|
||||||
if (favoriteData.success && favoriteData.favoriteBibleVersion) {
|
if (favoriteData.success && favoriteData.favoriteBibleVersion) {
|
||||||
|
// Check if favorite version is in the current locale's versions
|
||||||
const favoriteVersion = data.versions.find((v: BibleVersion) => v.id === favoriteData.favoriteBibleVersion)
|
const favoriteVersion = data.versions.find((v: BibleVersion) => v.id === favoriteData.favoriteBibleVersion)
|
||||||
if (favoriteVersion) {
|
if (favoriteVersion) {
|
||||||
versionToSelect = favoriteVersion
|
versionToSelect = favoriteVersion
|
||||||
@@ -376,9 +380,11 @@ export default function BibleReaderNew({ initialVersion, initialBook, initialCha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to default version or first version
|
// Fall back to default version for this locale or first version
|
||||||
if (!versionToSelect) {
|
if (!versionToSelect) {
|
||||||
versionToSelect = data.versions.find((v: BibleVersion) => v.isDefault) || data.versions[0]
|
versionToSelect = data.versions.find((v: BibleVersion) => v.isDefault && v.language.toLowerCase() === locale.toLowerCase())
|
||||||
|
|| data.versions.find((v: BibleVersion) => v.isDefault)
|
||||||
|
|| data.versions[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (versionToSelect) {
|
if (versionToSelect) {
|
||||||
|
|||||||
@@ -33,12 +33,22 @@ export function LanguageSwitcher() {
|
|||||||
setAnchorEl(null)
|
setAnchorEl(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleLanguageChange = (newLocale: string) => {
|
const handleLanguageChange = async (newLocale: string) => {
|
||||||
// Remove current locale from pathname and add new one
|
// Check if we're on a Bible page
|
||||||
const pathWithoutLocale = pathname.replace(`/${locale}`, '') || '/'
|
const isBiblePage = pathname.includes('/bible/')
|
||||||
const newPath = `/${newLocale}${pathWithoutLocale === '/' ? '' : pathWithoutLocale}`
|
|
||||||
|
if (isBiblePage) {
|
||||||
|
// For Bible pages, redirect to Bible reader without specific version
|
||||||
|
// Let the reader component select appropriate version for the new language
|
||||||
|
const newPath = `/${newLocale}/bible`
|
||||||
|
router.push(newPath)
|
||||||
|
} else {
|
||||||
|
// For other pages, just change the locale in the URL
|
||||||
|
const pathWithoutLocale = pathname.replace(`/${locale}`, '') || '/'
|
||||||
|
const newPath = `/${newLocale}${pathWithoutLocale === '/' ? '' : pathWithoutLocale}`
|
||||||
|
router.push(newPath)
|
||||||
|
}
|
||||||
|
|
||||||
router.push(newPath)
|
|
||||||
handleClose()
|
handleClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user