mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat(native): a capability handshake between the app binary and the server's UI
The UI ships with the server and the binary through the stores, so the two drift. TrekShell.getCapabilities now reports the binary's shell API, version and native features by name (iOS and Android), and the UI turns each native feature on only where the binary has it; a binary from before the handshake counts as shell API 1 with none. The other way round, the server names the oldest binary its UI works with (minNativeShellApi), and an app below it is asked to update with a link to its store.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { act, fireEvent, render, screen } from '../../../tests/helpers/render';
|
||||
import { _resetNativeCapabilities, checkAppUpdate, loadNativeCapabilities } from '../../native/capabilities';
|
||||
import { isNativeApp } from '../../native/platform';
|
||||
import { TrekShell } from '../../native/trekShell';
|
||||
import NativeUpdateNotice from './NativeUpdateNotice';
|
||||
|
||||
vi.mock('../../native/platform', () => ({ isNativeApp: vi.fn(), isNativeIos: vi.fn(() => false) }));
|
||||
vi.mock('../../native/trekShell', () => ({
|
||||
TrekShell: { getCapabilities: vi.fn() },
|
||||
isUnimplemented: () => false,
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
_resetNativeCapabilities();
|
||||
vi.mocked(isNativeApp).mockReturnValue(true);
|
||||
vi.mocked(TrekShell.getCapabilities).mockResolvedValue({
|
||||
shellApi: 2,
|
||||
platform: 'android',
|
||||
appVersion: '5.2.0',
|
||||
build: '3',
|
||||
features: [],
|
||||
} as never);
|
||||
});
|
||||
|
||||
describe('NativeUpdateNotice', () => {
|
||||
it('stays away while the binary is new enough', async () => {
|
||||
render(<NativeUpdateNotice />);
|
||||
await act(async () => {
|
||||
await checkAppUpdate(async () => ({ minNativeShellApi: 2 }));
|
||||
});
|
||||
expect(screen.queryByRole('status')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('leads to the store when the server needs a newer binary, and can be dismissed', async () => {
|
||||
render(<NativeUpdateNotice />);
|
||||
await act(async () => {
|
||||
await loadNativeCapabilities();
|
||||
await checkAppUpdate(async () => ({ minNativeShellApi: 3 }));
|
||||
});
|
||||
const link = screen.getByRole('link');
|
||||
expect(link).toHaveAttribute('href', 'https://play.google.com/store/apps/details?id=com.liketrek.trek');
|
||||
fireEvent.click(screen.getByRole('button'));
|
||||
expect(screen.queryByRole('status')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* NativeUpdateNotice: this server's UI needs a newer app binary than the one
|
||||
* installed (native/capabilities.ts, minNativeShellApi). The UI updates with
|
||||
* the server, the binary only through the store, so the two can drift; the
|
||||
* notice says so and leads to the store instead of leaving native calls to
|
||||
* fail one by one. Rendered once in App, inside the app only.
|
||||
*/
|
||||
import { Download, X } from 'lucide-react';
|
||||
import React, { useState } from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { useNativeCapabilities } from '../../native/capabilities';
|
||||
|
||||
const STORE_URL = {
|
||||
ios: 'https://apps.apple.com/app/id6831237775',
|
||||
android: 'https://play.google.com/store/apps/details?id=com.liketrek.trek',
|
||||
} as const;
|
||||
|
||||
export default function NativeUpdateNotice(): React.ReactElement | null {
|
||||
const { t } = useTranslation();
|
||||
const { caps, updateRequired } = useNativeCapabilities();
|
||||
const [dismissed, setDismissed] = useState(false);
|
||||
if (!updateRequired || dismissed || caps.platform === 'web') return null;
|
||||
const store = caps.platform === 'android' ? STORE_URL.android : STORE_URL.ios;
|
||||
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
style={{ bottom: 'calc(var(--bottom-nav-h) + 56px)' }}
|
||||
className="fixed inset-x-4 z-[9999] flex items-center gap-3 rounded-2xl border border-edge bg-surface-elevated px-4 py-3 text-content shadow-lg"
|
||||
>
|
||||
<Download size={18} className="shrink-0 text-content-muted" aria-hidden="true" />
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="text-body font-semibold">{t('native.update.title')}</div>
|
||||
<div className="text-caption text-content-secondary">{t('native.update.body')}</div>
|
||||
</div>
|
||||
<a
|
||||
href={store}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="shrink-0 rounded-full bg-accent px-3 py-1.5 text-caption font-semibold text-accent-text"
|
||||
>
|
||||
{t('native.update.open')}
|
||||
</a>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDismissed(true)}
|
||||
aria-label={t('common.close')}
|
||||
className="shrink-0 rounded-full p-1 text-content-muted"
|
||||
>
|
||||
<X size={14} aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user