Skip to content

Commit

Permalink
Merge pull request #2025 from quran/fix-font-issue
Browse files Browse the repository at this point in the history
Fix Quran reader when switching languages
  • Loading branch information
osamasayed authored Sep 14, 2023
2 parents 8ff73ed + cb96a67 commit 14c0dd0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
22 changes: 4 additions & 18 deletions src/components/QuranReader/ReadingView/PageContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useEffect, useMemo, useContext } from 'react';
import React, { useEffect, useMemo } from 'react';

import { useSelector as useXstateSelector } from '@xstate/react';
import { useSelector } from 'react-redux';
import useSWRImmutable from 'swr/immutable';

import { getPageNumberByPageIndex } from '../utils/page';
Expand All @@ -10,10 +8,8 @@ import Page from './Page';
import ReadingViewSkeleton from './ReadingViewSkeleton';

import { getReaderViewRequestKey, verseFetcher } from '@/components/QuranReader/api';
import { selectIsUsingDefaultWordByWordLocale } from '@/redux/slices/QuranReader/readingPreferences';
import useIsUsingDefaultSettings from '@/hooks/useIsUsingDefaultSettings';
import QuranReaderStyles from '@/redux/types/QuranReaderStyles';
import { selectIsUsingDefaultReciter } from 'src/xstate/actors/audioPlayer/selectors';
import { AudioPlayerMachineContext } from 'src/xstate/AudioPlayerMachineContext';
import { VersesResponse } from 'types/ApiResponses';
import LookupRecord from 'types/LookupRecord';
import Verse from 'types/Verse';
Expand All @@ -27,7 +23,6 @@ type Props = {
pageIndex: number;
setMushafPageToVersesMap: (data: Record<number, Verse[]>) => void;
initialData: VersesResponse;
isUsingDefaultFont: boolean;
};

const getPageVersesRange = (
Expand Down Expand Up @@ -75,7 +70,6 @@ const PageContainer: React.FC<Props> = ({
pageIndex,
setMushafPageToVersesMap,
initialData,
isUsingDefaultFont,
}: Props): JSX.Element => {
const pageNumber = useMemo(
() => getPageNumberByPageIndex(pageIndex, pagesVersesRange),
Expand All @@ -86,16 +80,8 @@ const PageContainer: React.FC<Props> = ({
[initialData.verses, pageIndex, pageNumber],
);

const audioService = useContext(AudioPlayerMachineContext);
const isUsingDefaultReciter = useXstateSelector(audioService, (state) =>
selectIsUsingDefaultReciter(state),
);
const isUsingDefaultWordByWordLocale = useSelector(selectIsUsingDefaultWordByWordLocale);
const shouldUseInitialData =
pageIndex === 0 &&
isUsingDefaultFont &&
isUsingDefaultReciter &&
isUsingDefaultWordByWordLocale;
const isUsingDefaultSettings = useIsUsingDefaultSettings();
const shouldUseInitialData = pageIndex === 0 && isUsingDefaultSettings;
const { data: verses, isValidating } = useSWRImmutable(
getReaderViewRequestKey({
pageNumber,
Expand Down
1 change: 0 additions & 1 deletion src/components/QuranReader/ReadingView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ const ReadingView = ({

return (
<PageContainer
isUsingDefaultFont={isUsingDefaultFont}
pagesVersesRange={pagesVersesRange}
quranReaderStyles={quranReaderStyles}
reciterId={reciterId}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { useContext, useEffect, useMemo } from 'react';
import { useEffect, useMemo } from 'react';

import { useSelector as useXstateSelector } from '@xstate/react';
import useTranslation from 'next-translate/useTranslation';
import { useRouter } from 'next/router';
import { useSelector } from 'react-redux';
import useSWRImmutable from 'swr/immutable';

import { getTranslationViewRequestKey, verseFetcher } from '@/components/QuranReader/api';
import { getTranslationsInitialState } from '@/redux/defaultSettings/util';
import { selectIsUsingDefaultWordByWordLocale } from '@/redux/slices/QuranReader/readingPreferences';
import { selectIsUsingDefaultFont } from '@/redux/slices/QuranReader/styles';
import { selectIsUsingDefaultTranslations } from '@/redux/slices/QuranReader/translations';
import useIsUsingDefaultSettings from '@/hooks/useIsUsingDefaultSettings';
import QuranReaderStyles from '@/redux/types/QuranReaderStyles';
import { VersesResponse } from '@/types/ApiResponses';
import { Mushaf, QuranReaderDataType } from '@/types/QuranReader';
import Verse from '@/types/Verse';
import { areArraysEqual } from '@/utils/array';
import { makeBookmarksRangeUrl } from '@/utils/auth/apiPaths';
import { isLoggedIn } from '@/utils/auth/login';
import { getPageNumberFromIndexAndPerPage } from '@/utils/number';
import { selectIsUsingDefaultReciter } from 'src/xstate/actors/audioPlayer/selectors';
import { AudioPlayerMachineContext } from 'src/xstate/AudioPlayerMachineContext';

interface QuranReaderParams {
quranReaderDataType: QuranReaderDataType;
Expand Down Expand Up @@ -65,34 +57,18 @@ const useDedupedFetchVerse = ({

const { lang } = useTranslation();

const defaultTranslations = getTranslationsInitialState(lang).selectedTranslations;
const translationParams = useMemo(
() =>
(router.query.translations as string)?.split(',')?.map((translation) => Number(translation)),
[router.query.translations],
);

const audioService = useContext(AudioPlayerMachineContext);

const isUsingDefaultReciter = useXstateSelector(audioService, (state) =>
selectIsUsingDefaultReciter(state),
);
const isUsingDefaultWordByWordLocale = useSelector(selectIsUsingDefaultWordByWordLocale);
const isUsingDefaultTranslations = useSelector(selectIsUsingDefaultTranslations);
const isUsingDefaultFont = useSelector(selectIsUsingDefaultFont);

const pageNumber = getPageNumberFromIndexAndPerPage(verseIdx, initialData.pagination.perPage);

const idxInPage = verseIdx % initialData.pagination.perPage;

const shouldUseInitialData =
pageNumber === 1 &&
isUsingDefaultFont &&
isUsingDefaultReciter &&
isUsingDefaultWordByWordLocale &&
isUsingDefaultTranslations &&
(!translationParams || areArraysEqual(defaultTranslations, translationParams));

const isUsingDefaultSettings = useIsUsingDefaultSettings({ translations: translationParams });
const shouldUseInitialData = pageNumber === 1 && isUsingDefaultSettings;
const { data: verses } = useSWRImmutable(
getTranslationViewRequestKey({
quranReaderDataType,
Expand Down
59 changes: 59 additions & 0 deletions src/hooks/useIsUsingDefaultSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useContext, useMemo } from 'react';

import { useSelector as useXstateSelector } from '@xstate/react';
import useTranslation from 'next-translate/useTranslation';
import { useSelector } from 'react-redux';

import {
getQuranReaderStylesInitialState,
getTranslationsInitialState,
} from '@/redux/defaultSettings/util';
import { selectIsUsingDefaultWordByWordLocale } from '@/redux/slices/QuranReader/readingPreferences';
import { selectQuranFont, selectQuranMushafLines } from '@/redux/slices/QuranReader/styles';
import { selectIsUsingDefaultTranslations } from '@/redux/slices/QuranReader/translations';
import { areArraysEqual } from '@/utils/array';
import { selectIsUsingDefaultReciter } from '@/xstate/actors/audioPlayer/selectors';
import { AudioPlayerMachineContext } from '@/xstate/AudioPlayerMachineContext';

const useIsUsingDefaultSettings = ({ translations }: { translations?: number[] } = {}) => {
const { lang } = useTranslation();
const audioService = useContext(AudioPlayerMachineContext);
const isUsingDefaultReciter = useXstateSelector(audioService, (state) =>
selectIsUsingDefaultReciter(state),
);
const isUsingDefaultWordByWordLocale = useSelector(selectIsUsingDefaultWordByWordLocale);
const isUsingDefaultTranslations = useSelector(selectIsUsingDefaultTranslations);

const quranFont = useSelector(selectQuranFont);
const mushafLines = useSelector(selectQuranMushafLines);

const defaultState = useMemo(() => {
return {
quranReaderStyles: getQuranReaderStylesInitialState(lang),
translations: getTranslationsInitialState(lang),
};
}, [lang]);

const isUsingDefaultFont =
defaultState.quranReaderStyles.quranFont === quranFont &&
defaultState.quranReaderStyles.mushafLines === mushafLines;

const areTranslationsEqual = useMemo(() => {
if (!translations) {
return false;
}

return areArraysEqual(defaultState.translations.selectedTranslations, translations);
}, [translations, defaultState.translations.selectedTranslations]);

const isUsingDefaultSettings =
isUsingDefaultFont && isUsingDefaultReciter && isUsingDefaultWordByWordLocale;

if (translations) {
return isUsingDefaultSettings && isUsingDefaultTranslations && areTranslationsEqual;
}

return isUsingDefaultSettings;
};

export default useIsUsingDefaultSettings;
7 changes: 5 additions & 2 deletions src/redux/slices/QuranReader/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ export const quranReaderStylesSlice = createSlice({
PreferenceGroup.QURAN_READER_STYLES
] as QuranReaderStyles;
if (remotePreferences) {
const { quranFont: defaultQuranFont } = getQuranReaderStylesInitialState(locale);
const { quranFont: defaultQuranFont, mushafLines: defaultMushafLines } =
getQuranReaderStylesInitialState(locale);
return {
...state,
...remotePreferences,
isUsingDefaultFont: defaultQuranFont === remotePreferences.quranFont,
isUsingDefaultFont:
defaultQuranFont === remotePreferences.quranFont &&
defaultMushafLines === remotePreferences.mushafLines,
};
}
return state;
Expand Down

0 comments on commit 14c0dd0

Please sign in to comment.