diff --git a/src/components/left/main/LeftMainHeader.tsx b/src/components/left/main/LeftMainHeader.tsx index d9c9aa219..97eb792d9 100644 --- a/src/components/left/main/LeftMainHeader.tsx +++ b/src/components/left/main/LeftMainHeader.tsx @@ -11,10 +11,11 @@ import { } from '../../../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../../util/environment'; import buildClassName from '../../../util/buildClassName'; -import { isChatArchived } from '../../../modules/helpers'; import { formatDateToString } from '../../../util/dateFormat'; -import { selectTheme } from '../../../modules/selectors'; import switchTheme from '../../../util/switchTheme'; +import { setPermanentWebVersion } from '../../../util/permanentWebVersion'; +import { selectTheme } from '../../../modules/selectors'; +import { isChatArchived } from '../../../modules/helpers'; import useLang from '../../../hooks/useLang'; import { disableHistoryBack } from '../../../hooks/useHistoryBack'; @@ -54,7 +55,6 @@ const ANIMATION_LEVEL_OPTIONS = [0, 1, 2]; const PRODUCTION_HOSTNAME = 'web.telegram.org'; const LEGACY_VERSION_URL = 'https://web.telegram.org/?legacy=1'; const WEBK_VERSION_URL = 'https://web.telegram.org/k/'; -const PERMANENT_VERSION_KEY = 'kz_version'; const LeftMainHeader: FC = ({ content, @@ -158,7 +158,7 @@ const LeftMainHeader: FC = ({ }, [animationLevel, setSettingOption]); const handleSwitchToWebK = () => { - localStorage.setItem(PERMANENT_VERSION_KEY, JSON.stringify('K')); + setPermanentWebVersion('K'); disableHistoryBack(); }; diff --git a/src/modules/actions/ui/initial.ts b/src/modules/actions/ui/initial.ts index 7b82d1fd9..55e7c30b5 100644 --- a/src/modules/actions/ui/initial.ts +++ b/src/modules/actions/ui/initial.ts @@ -8,6 +8,7 @@ import { setLanguage } from '../../../util/langProvider'; import switchTheme from '../../../util/switchTheme'; import { selectTheme } from '../../selectors'; import { startWebsync } from '../../../util/websync'; +import { ensurePermanentWebVersion } from '../../../util/permanentWebVersion'; const HISTORY_ANIMATION_DURATION = 450; @@ -28,7 +29,10 @@ addReducer('init', (global) => { document.body.classList.add('initial'); document.body.classList.add(`animation-level-${animationLevel}`); document.body.classList.add(IS_TOUCH_ENV ? 'is-touch-env' : 'is-pointer-env'); + switchTheme(theme, animationLevel === ANIMATION_LEVEL_MAX); + + ensurePermanentWebVersion(); startWebsync(); if (IS_IOS) { diff --git a/src/util/permanentWebVersion.ts b/src/util/permanentWebVersion.ts new file mode 100644 index 000000000..ecf2c90cb --- /dev/null +++ b/src/util/permanentWebVersion.ts @@ -0,0 +1,27 @@ +const PERMANENT_VERSION_KEY = 'kz_version'; +const AVAILABLE_VERSIONS = ['Z', 'K'] as const; +const DEFAULT_VERSION = 'Z'; + +export function setPermanentWebVersion(version: typeof AVAILABLE_VERSIONS[number]) { + localStorage.setItem(PERMANENT_VERSION_KEY, JSON.stringify(version)); +} + +export function ensurePermanentWebVersion() { + if (!hasPermanentWebVersion()) { + setPermanentWebVersion(DEFAULT_VERSION); + } +} + +function hasPermanentWebVersion() { + const json = localStorage.getItem(PERMANENT_VERSION_KEY); + if (!json) { + return false; + } + + try { + const version = JSON.parse(json); + return AVAILABLE_VERSIONS.includes(version); + } catch (err) { + return false; + } +}