feat(deskotp): remember discover language
This commit is contained in:
@@ -3,35 +3,16 @@ import type { SupportedLanguages } from "@follow/models"
|
||||
import { defaultGeneralSettings } from "@follow/shared/settings/defaults"
|
||||
import { enhancedGeneralSettingKeys } from "@follow/shared/settings/enhanced"
|
||||
import type { GeneralSettings } from "@follow/shared/settings/interface"
|
||||
import LanguageDetector from "i18next-browser-languagedetector"
|
||||
import { useCallback, useMemo } from "react"
|
||||
|
||||
import { currentSupportedLanguages } from "~/@types/constants"
|
||||
import { I18N_LOCALE_KEY } from "~/constants"
|
||||
import { jotaiStore } from "~/lib/jotai"
|
||||
import { getDefaultLanguage } from "~/lib/load-language"
|
||||
|
||||
export const DEFAULT_ACTION_LANGUAGE = "default"
|
||||
|
||||
let language = "en"
|
||||
const languageDetector = new LanguageDetector(null, {
|
||||
order: ["querystring", "localStorage", "navigator"],
|
||||
lookupQuerystring: "lng",
|
||||
lookupLocalStorage: I18N_LOCALE_KEY,
|
||||
caches: ["localStorage"],
|
||||
})
|
||||
|
||||
const userLang = languageDetector.detect()
|
||||
if (userLang) {
|
||||
const firstUserLang = Array.isArray(userLang) ? userLang[0]! : userLang
|
||||
const currentLang = currentSupportedLanguages.find((lang) => lang.includes(firstUserLang))
|
||||
if (currentLang) {
|
||||
language = currentLang
|
||||
}
|
||||
}
|
||||
|
||||
const createDefaultSettings = (): GeneralSettings => ({
|
||||
...defaultGeneralSettings,
|
||||
language,
|
||||
language: getDefaultLanguage(),
|
||||
})
|
||||
|
||||
const {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { jotaiStore } from "@follow/utils/jotai"
|
||||
import { atom, useAtomValue, useSetAtom } from "jotai"
|
||||
import { useEventCallback } from "usehooks-ts"
|
||||
|
||||
import { getDefaultLanguage } from "~/lib/load-language"
|
||||
import { DEFAULT_ACTION_ORDER } from "~/modules/customize-toolbar/constant"
|
||||
|
||||
import { hookEnhancedSettings } from "./general"
|
||||
@@ -15,6 +16,9 @@ export const createDefaultSettings = (): UISettings => ({
|
||||
|
||||
// Action Order
|
||||
toolbarOrder: DEFAULT_ACTION_ORDER,
|
||||
|
||||
// Discover
|
||||
discoverLanguage: getDefaultLanguage().startsWith("zh") ? "all" : "eng",
|
||||
})
|
||||
|
||||
const zenModeAtom = atom(false)
|
||||
|
||||
@@ -3,18 +3,41 @@ import { EventBus } from "@follow/utils/event-bus"
|
||||
import { isEmptyObject } from "@follow/utils/utils"
|
||||
import dayjs from "dayjs"
|
||||
import i18next from "i18next"
|
||||
import LanguageDetector from "i18next-browser-languagedetector"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { currentSupportedLanguages, dayjsLocaleImportMap } from "~/@types/constants"
|
||||
import { defaultResources } from "~/@types/default-resource"
|
||||
import { fallbackLanguage, i18nAtom, langChain, LocaleCache } from "~/i18n"
|
||||
import { I18N_LOCALE_KEY } from "~/constants"
|
||||
import { i18nAtom, langChain, LocaleCache } from "~/i18n"
|
||||
import { jotaiStore } from "~/lib/jotai"
|
||||
|
||||
import { tipcClient } from "./client"
|
||||
import { appLog } from "./log"
|
||||
|
||||
const loadingLangLock = new Set<string>()
|
||||
const loadedLangs = new Set<string>([fallbackLanguage])
|
||||
const loadedLangs = new Set<string>(["en"])
|
||||
|
||||
let defaultLanguage = "en"
|
||||
const languageDetector = new LanguageDetector(null, {
|
||||
order: ["querystring", "localStorage", "navigator"],
|
||||
lookupQuerystring: "lng",
|
||||
lookupLocalStorage: I18N_LOCALE_KEY,
|
||||
caches: ["localStorage"],
|
||||
})
|
||||
|
||||
const userLang = languageDetector.detect()
|
||||
if (userLang) {
|
||||
const firstUserLang = Array.isArray(userLang) ? userLang[0]! : userLang
|
||||
const currentLang = currentSupportedLanguages.find((lang) => lang.includes(firstUserLang))
|
||||
if (currentLang) {
|
||||
defaultLanguage = currentLang
|
||||
}
|
||||
}
|
||||
|
||||
export const getDefaultLanguage = () => {
|
||||
return defaultLanguage
|
||||
}
|
||||
|
||||
export const loadLanguageAndApply = async (lang: string) => {
|
||||
const dayjsImport = dayjsLocaleImportMap[lang]
|
||||
|
||||
@@ -13,7 +13,7 @@ import { useCallback, useEffect, useMemo, useState } from "react"
|
||||
import { flushSync } from "react-dom"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useGeneralSettingKey } from "~/atoms/settings/general"
|
||||
import { setUISetting, useUISettingKey } from "~/atoms/settings/ui"
|
||||
import { DrawerModalLayout } from "~/components/ui/modal/stacked/custom-modal"
|
||||
import { useCurrentModal, useModalStack } from "~/components/ui/modal/stacked/hooks"
|
||||
import { useAuthQuery } from "~/hooks/common"
|
||||
@@ -29,35 +29,39 @@ const LanguageOptions = [
|
||||
},
|
||||
{
|
||||
label: "English",
|
||||
value: "en",
|
||||
value: "eng",
|
||||
},
|
||||
{
|
||||
label: "中文",
|
||||
value: "zh-CN",
|
||||
value: "cmn",
|
||||
},
|
||||
] as const
|
||||
|
||||
type Language = (typeof LanguageOptions)[number]["value"]
|
||||
type DiscoverCategories = (typeof RSSHubCategories)[number] | string
|
||||
|
||||
const LanguageMap = {
|
||||
all: "all",
|
||||
eng: "en",
|
||||
cmn: "zh-CN",
|
||||
} as const
|
||||
|
||||
const fetchRsshubPopular = (category: DiscoverCategories, lang: Language) => {
|
||||
return Queries.discover.rsshubCategory({
|
||||
category: "popular",
|
||||
categories: category === "all" ? "popular" : `popular,${category}`,
|
||||
lang,
|
||||
lang: LanguageMap[lang],
|
||||
})
|
||||
}
|
||||
let firstLoad = true
|
||||
export function Recommendations() {
|
||||
const { t } = useTranslation()
|
||||
const lang = useGeneralSettingKey("language")
|
||||
const { present } = useModalStack()
|
||||
|
||||
const defaultLang = !lang || ["zh-CN", "zh-HK", "zh-TW"].includes(lang) ? "all" : "en"
|
||||
const [category, setCategory] = useState<DiscoverCategories>("all")
|
||||
const [selectedLang, setSelectedLang] = useState<Language>(defaultLang)
|
||||
const lang = useUISettingKey("discoverLanguage")
|
||||
|
||||
const rsshubPopular = useAuthQuery(fetchRsshubPopular(category, selectedLang), {
|
||||
const rsshubPopular = useAuthQuery(fetchRsshubPopular(category, lang), {
|
||||
meta: {
|
||||
persist: true,
|
||||
},
|
||||
@@ -107,7 +111,7 @@ export function Recommendations() {
|
||||
const handleLangChange = useCallback(
|
||||
(value: string) => {
|
||||
flushSync(() => {
|
||||
setSelectedLang(value as Language)
|
||||
setUISetting("discoverLanguage", value as Language)
|
||||
})
|
||||
rsshubPopular.refetch()
|
||||
},
|
||||
@@ -152,7 +156,7 @@ export function Recommendations() {
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-text shrink-0 text-sm font-medium">{t("words.language")}</span>
|
||||
<ResponsiveSelect
|
||||
value={selectedLang}
|
||||
value={lang}
|
||||
onValueChange={handleLangChange}
|
||||
triggerClassName="h-8 rounded border-0"
|
||||
size="sm"
|
||||
|
||||
@@ -6,7 +6,7 @@ import { useQuery } from "@tanstack/react-query"
|
||||
import { useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
|
||||
import { useGeneralSettingKey } from "~/atoms/settings/general"
|
||||
import { setUISetting, useUISettingKey } from "~/atoms/settings/ui"
|
||||
import { apiClient } from "~/lib/api-fetch"
|
||||
|
||||
import { TrendingFeedCard } from "../discover/TrendingFeedCard"
|
||||
@@ -52,17 +52,16 @@ export function Trending({
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const { t: tCommon } = useTranslation("common")
|
||||
const lang = useGeneralSettingKey("language")
|
||||
const lang = useUISettingKey("discoverLanguage")
|
||||
|
||||
const [selectedLang, setSelectedLang] = useState<Language>(lang.startsWith("zh") ? "all" : "eng")
|
||||
const [selectedView, setSelectedView] = useState<View>("all")
|
||||
|
||||
const { data, isLoading } = useQuery({
|
||||
queryKey: ["trending", selectedLang, selectedView],
|
||||
queryKey: ["trending", lang, selectedView],
|
||||
queryFn: async () => {
|
||||
return await apiClient.trending.feeds.$get({
|
||||
query: {
|
||||
language: selectedLang === "all" ? undefined : selectedLang,
|
||||
language: lang === "all" ? undefined : lang,
|
||||
view: selectedView === "all" ? undefined : Number(selectedView),
|
||||
limit,
|
||||
},
|
||||
@@ -86,9 +85,9 @@ export function Trending({
|
||||
<div className="flex items-center">
|
||||
<span className="text-text shrink-0 text-sm font-medium">{t("words.language")}:</span>
|
||||
<ResponsiveSelect
|
||||
value={selectedLang}
|
||||
value={lang}
|
||||
onValueChange={(value) => {
|
||||
setSelectedLang(value as Language)
|
||||
setUISetting("discoverLanguage", value as Language)
|
||||
}}
|
||||
triggerClassName="h-8 rounded border-0"
|
||||
size="sm"
|
||||
|
||||
@@ -89,6 +89,9 @@ export const defaultUISettings: UISettings = {
|
||||
|
||||
showUnreadCountViewAndSubscriptionMobile: false,
|
||||
showUnreadCountBadgeMobile: false,
|
||||
|
||||
// Discover
|
||||
discoverLanguage: "all",
|
||||
}
|
||||
|
||||
export const defaultIntegrationSettings: IntegrationSettings = {
|
||||
|
||||
@@ -75,6 +75,9 @@ export interface UISettings {
|
||||
// @mobile
|
||||
showUnreadCountViewAndSubscriptionMobile: boolean
|
||||
showUnreadCountBadgeMobile: boolean
|
||||
|
||||
// Discover
|
||||
discoverLanguage: "all" | "eng" | "cmn"
|
||||
}
|
||||
|
||||
export interface IntegrationSettings {
|
||||
|
||||
Reference in New Issue
Block a user