mancala/apps/mobile/src/screens/HomeScreen.tsx

66 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-03-24 14:23:08 +03:00
import * as React from 'react';
2024-03-31 16:39:39 +03:00
import { View, Button, Pressable, Text } from 'react-native';
2024-03-24 14:23:08 +03:00
import { useTranslation } from 'react-i18next';
import { HomeScreenProps } from '../types';
2024-03-31 16:39:39 +03:00
import CircularPanel from '../components/CircularPanel';
import { SvgXml } from 'react-native-svg';
import helpSvg from '../svg/help';
import { getColorByBrightness } from "@mancala/core";
2024-03-24 14:23:08 +03:00
export function HomeScreen({ navigation, route }: HomeScreenProps) {
const { context } = route.params;
2024-03-24 14:23:08 +03:00
const { t } = useTranslation();
const [lastGameId, setLastGameId] = React.useState<string | undefined>(undefined);
2024-03-24 14:23:08 +03:00
const textColorOnAppBar = getColorByBrightness(
context.themeManager.theme.appBarBgColor,
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
);
const updateHeaderButton = () => {
navigation.setOptions({
headerRight: () => (
<Pressable onPress={() => {
navigation.push("Help", { context })
}}>
<SvgXml xml={helpSvg.replaceAll("customColor", textColorOnAppBar)} width="24" height="24" />
</Pressable>
),
});
}
React.useEffect(() => {
updateHeaderButton();
const savedGameId = context.storage.getString('lastGameId');
if (savedGameId) {
setLastGameId(savedGameId);
}
}, []);
2024-03-24 14:23:08 +03:00
return (
2024-03-31 16:39:39 +03:00
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: context.themeManager.theme?.background }}>
<Pressable onPress={() => navigation.replace('Loby', { context })}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('NewGame')}</Text>} />
2024-03-31 16:39:39 +03:00
</Pressable>
{lastGameId && (
<>
<View style={{ height: 20 }} />
<Pressable onPress={async () => {
try {
const userKey = await context.userKeyStore.getUserKey();
navigation.replace('Game', { context, gameId: lastGameId, userKey });
} catch (error) {
context.storage.deleteString('lastGameId');
setLastGameId(undefined);
}
}}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('RejoinGame')}</Text>} />
</Pressable>
</>
)}
2024-03-24 14:23:08 +03:00
</View>
);
}