Skip to content

Commit ed29fdd

Browse files
Fix hook error and add PWA install translations and localize InstallApp (#352)
* Fix hook error and add PWA install translations and localize InstallApp * Removed duplicated locales * Unified component * Unified component * Unified component * Add useIsPwa hook * Refactor DownloadAppDrawer to use children prop and remove InstallApp component * Fix lint warnings and minor issues --------- Co-authored-by: krokosik <[email protected]>
1 parent b7bf689 commit ed29fdd

File tree

5 files changed

+98
-127
lines changed

5 files changed

+98
-127
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useTranslation } from 'next-i18next';
2+
import { type ReactNode } from 'react';
3+
4+
import { useIsPwa } from '~/hooks/useIsPwa';
5+
import { AppDrawer } from '../ui/drawer';
6+
7+
interface DownloadAppDrawerProps {
8+
children: ReactNode;
9+
className?: string;
10+
}
11+
12+
export const DownloadAppDrawer: React.FC<DownloadAppDrawerProps> = ({ children, className }) => {
13+
const { t } = useTranslation('account_page');
14+
const isStandalone = useIsPwa();
15+
16+
if (isStandalone) {
17+
return null;
18+
}
19+
20+
return (
21+
<AppDrawer
22+
trigger={children}
23+
leftAction={t('ui.actions.close', { ns: 'common' })}
24+
title={t('ui.download_app_details.title')}
25+
className={className ?? 'h-[70vh]'}
26+
shouldCloseOnAction
27+
>
28+
<div className="flex flex-col gap-8">
29+
<p>{t('ui.download_app_details.download_as_pwa')}</p>
30+
31+
<p>
32+
{t('ui.download_app_details.using_ios')}{' '}
33+
<a
34+
className="text-cyan-500 underline"
35+
href="https://youtube.com/shorts/MQHeLOjr350"
36+
target="_blank"
37+
rel="noreferrer"
38+
>
39+
{t('ui.download_app_details.video')}
40+
</a>
41+
</p>
42+
43+
<p>
44+
{t('ui.download_app_details.using_android')}{' '}
45+
<a
46+
className="text-cyan-500 underline"
47+
href="https://youtube.com/shorts/04n7oKGzgOs"
48+
target="_blank"
49+
rel="noreferrer"
50+
>
51+
{t('ui.download_app_details.video')}
52+
</a>
53+
</p>
54+
</div>
55+
</AppDrawer>
56+
);
57+
};
58+
59+
export default DownloadAppDrawer;

src/components/InstallApp.tsx

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/hooks/useIsPwa.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useEffect, useState } from 'react';
2+
3+
export function useIsPwa(): boolean {
4+
const [isPwa, setIsPwa] = useState(false);
5+
6+
useEffect(() => {
7+
try {
8+
const isIOSStandalone =
9+
typeof navigator !== 'undefined' && 'standalone' in navigator
10+
? !!navigator.standalone
11+
: false;
12+
13+
const isDisplayModeStandalone =
14+
typeof window !== 'undefined' && typeof window.matchMedia === 'function'
15+
? window.matchMedia('(display-mode: standalone)').matches
16+
: false;
17+
18+
setIsPwa(isIOSStandalone || isDisplayModeStandalone);
19+
} catch {
20+
setIsPwa(false);
21+
}
22+
}, []);
23+
24+
return isPwa;
25+
}

src/pages/account.tsx

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { UpdateName } from '~/components/Account/UpdateName';
2121
import MainLayout from '~/components/Layout/MainLayout';
2222
import { EntityAvatar } from '~/components/ui/avatar';
2323
import { Button } from '~/components/ui/button';
24-
import { AppDrawer } from '~/components/ui/drawer';
24+
import DownloadAppDrawer from '~/components/Account/DownloadAppDrawer';
2525
import { LoadingSpinner } from '~/components/ui/spinner';
2626
import { env } from '~/env';
2727
import { type NextPageWithUser } from '~/types';
@@ -196,41 +196,7 @@ const AccountPage: NextPageWithUser<{ isCloud: boolean; feedBackPossible: boolea
196196
<ChevronRight className="h-6 w-6 text-gray-500" />
197197
</Button>
198198
</Link>
199-
<AppDrawer
200-
trigger={downloadAppButton}
201-
leftAction={t('ui.actions.close', { ns: 'common' })}
202-
title={t('ui.download_app_details.title')}
203-
className="h-[70vh]"
204-
shouldCloseOnAction
205-
>
206-
<div className="flex flex-col gap-8">
207-
<p>{t('ui.download_app_details.download_as_pwa')}</p>
208-
209-
<p>
210-
{t('ui.download_app_details.using_ios')}{' '}
211-
<a
212-
className="text-cyan-500 underline"
213-
href="https://youtube.com/shorts/MQHeLOjr350"
214-
target="_blank"
215-
rel="noreferrer"
216-
>
217-
{t('ui.download_app_details.video')}
218-
</a>
219-
</p>
220-
221-
<p>
222-
{t('ui.download_app_details.using_android')}{' '}
223-
<a
224-
className="text-cyan-500 underline"
225-
href="https://youtube.com/shorts/04n7oKGzgOs"
226-
target="_blank"
227-
rel="noreferrer"
228-
>
229-
{t('ui.download_app_details.video')}
230-
</a>
231-
</p>
232-
</div>
233-
</AppDrawer>
199+
<DownloadAppDrawer>{downloadAppButton}</DownloadAppDrawer>
234200
<Button
235201
variant="ghost"
236202
className="text-md hover:text-foreground/80 w-full justify-between px-0"

src/pages/balances.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { ArrowUpOnSquareIcon } from '@heroicons/react/24/outline';
2-
import { PlusIcon } from 'lucide-react';
2+
import { Download, PlusIcon } from 'lucide-react';
33
import { useTranslation } from 'next-i18next';
44
import Head from 'next/head';
55
import Link from 'next/link';
66
import { useCallback } from 'react';
7+
import DownloadAppDrawer from '~/components/Account/DownloadAppDrawer';
78
import { BalanceEntry } from '~/components/Expense/BalanceEntry';
8-
import InstallApp from '~/components/InstallApp';
99
import MainLayout from '~/components/Layout/MainLayout';
1010
import { NotificationModal } from '~/components/NotificationModal';
1111
import { Button } from '~/components/ui/button';
12+
import { useIsPwa } from '~/hooks/useIsPwa';
1213
import { type NextPageWithUser } from '~/types';
1314
import { api } from '~/utils/api';
1415
import { withI18nStaticProps } from '~/utils/i18n/server';
1516
import { toUIString } from '~/utils/numbers';
1617

1718
const BalancePage: NextPageWithUser = () => {
1819
const { t } = useTranslation();
20+
const isPwa = useIsPwa();
1921
const balanceQuery = api.expense.getBalances.useQuery();
2022

2123
const shareWithFriends = useCallback(() => {
@@ -117,8 +119,13 @@ const BalancePage: NextPageWithUser = () => {
117119

118120
{!balanceQuery.isPending && !balanceQuery.data?.balances.length ? (
119121
<div className="mt-[40vh] flex -translate-y-[130%] flex-col items-center justify-center gap-6">
120-
<InstallApp />
121-
122+
<DownloadAppDrawer>
123+
<Button className="w-[250px]">
124+
<Download className="mr-2 h-5 w-5 text-black" />
125+
{t('account_page:ui.download_app')}
126+
</Button>
127+
</DownloadAppDrawer>
128+
{!isPwa && <p>{t('ui.or', { ns: 'common' })}</p>}
122129
<Link href="/add">
123130
<Button className="w-[250px]">
124131
<PlusIcon className="mr-2 h-5 w-5 text-black" />
@@ -136,6 +143,6 @@ const BalancePage: NextPageWithUser = () => {
136143

137144
BalancePage.auth = true;
138145

139-
export const getStaticProps = withI18nStaticProps(['common']);
146+
export const getStaticProps = withI18nStaticProps(['common', 'account_page']);
140147

141148
export default BalancePage;

0 commit comments

Comments
 (0)