From 27e704fe4f35d44fbcb30a6a88ae3360cafc4f10 Mon Sep 17 00:00:00 2001 From: poli0981 <127664709+poli0981@users.noreply.github.com> Date: Tue, 16 Jun 2026 16:23:07 +0700 Subject: [PATCH] fix(android): show legal gate on first launch + clean file-load toast (#98) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Android sideload APK never showed the consent gate and the file-load toast surfaced opaque content-URI document ids: 1. Legal gate: `consentStore` skipped the gate for ALL Tauri builds, justified by the desktop installer's license page — which the sideload APK doesn't have, so Android users never saw or accepted the terms. Add a build-time `__IS_ANDROID_BUILD__` define (`TAURI_ENV_PLATFORM === 'android'`) and gate only on desktop Tauri (`__IS_TAURI_BUILD__ && !__IS_ANDROID_BUILD__`); Android now shows the gate like the web build. Acceptance persists in the Android WebView's localStorage, so it asks only once. 2. Legal links: add an `openExternal()` helper that routes through `@tauri-apps/plugin-shell` inside Tauri (web keeps `window.open`), and wire the gate's License/Terms/Privacy/Disclaimer links to it — plain `target="_blank"` is swallowed by the Android WebView. 3. File-load toast: when the picked name has no usable extension (opaque Android content URIs like `msf:1000000123`), fall back to a count-based "N file(s) loaded" via new `toast.files_loaded` keys; keep the clean "Loaded " when a real filename is available. Desktop/web are unchanged. Also reword `consent.intro` "in your browser" -> "on your device" (accurate on the native app). typecheck / lint / 215 tests / web build all green; verified on the Android emulator (gate shows once, links open the system browser, toast). Co-authored-by: Claude Opus 4.8 (1M context) --- src/components/common/ConsentGate.tsx | 18 ++++++++++++++---- src/components/converter/InputPanel.tsx | 9 ++++++++- src/i18n/locales/en.json | 4 +++- src/i18n/locales/vi.json | 4 +++- src/lib/tauri.ts | 14 ++++++++++++++ src/stores/consentStore.ts | 7 ++++--- src/vite-env.d.ts | 1 + vite.config.ts | 4 ++++ vitest.config.ts | 1 + 9 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/components/common/ConsentGate.tsx b/src/components/common/ConsentGate.tsx index 93d9511..3330e21 100644 --- a/src/components/common/ConsentGate.tsx +++ b/src/components/common/ConsentGate.tsx @@ -9,6 +9,7 @@ import { DialogTitle, } from '@/components/ui/dialog'; import { useConsentStore } from '@/stores/consentStore'; +import { isTauri, openExternal } from '@/lib/tauri'; const REPO_URL = 'https://github.com/poli0981/jsonPrism'; @@ -20,10 +21,11 @@ const LEGAL_LINKS = [ ] as const; /** - * First-launch legal gate (web only). Blocks the app behind a non-dismissible - * modal until the user accepts. On desktop the store seeds `accepted = true` - * (the installer's license page already gated acceptance), so this renders its - * children straight through. + * First-launch legal gate (web + Android). Blocks the app behind a + * non-dismissible modal until the user accepts. On desktop the store seeds + * `accepted = true` (the installer's license page already gated acceptance), so + * this renders its children straight through; the Android sideload APK has no + * such installer step, so it shows the gate like the web build. */ export function ConsentGate({ children }: { children: ReactNode }) { const accepted = useConsentStore((s) => s.accepted); @@ -55,6 +57,14 @@ export function ConsentGate({ children }: { children: ReactNode }) { href={l.href} target="_blank" rel="noopener noreferrer" + onClick={(e) => { + // Tauri webviews (notably Android) ignore target="_blank"; + // hand the URL to the system browser instead. + if (isTauri()) { + e.preventDefault(); + void openExternal(l.href); + } + }} className="text-primary inline-flex items-center gap-1 underline-offset-4 hover:underline" > {t(`consent.links.${l.key}`)} diff --git a/src/components/converter/InputPanel.tsx b/src/components/converter/InputPanel.tsx index 6e6733d..3cfea32 100644 --- a/src/components/converter/InputPanel.tsx +++ b/src/components/converter/InputPanel.tsx @@ -116,7 +116,14 @@ export function InputPanel({ } const text = await file.text(); onChange(text); - toast.success(t('toast.file_loaded', { name: file.name })); + // Android content URIs sometimes yield an opaque document id with no + // real filename (e.g. `msf:1000000123`). When there's no usable + // extension to show a clean name, fall back to a count-based message. + toast.success( + pickedExt + ? t('toast.file_loaded', { name: file.name }) + : t('toast.files_loaded', { count: 1 }), + ); } catch (err) { toast.error(t('toast.file_failed', { message: (err as Error).message })); } diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index a04dccf..c1ed572 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -174,6 +174,8 @@ "copy_failed": "Couldn't copy to clipboard", "downloaded": "Downloaded {{filename}}", "file_loaded": "Loaded {{name}}", + "files_loaded": "{{count}} file loaded", + "files_loaded_other": "{{count}} files loaded", "file_failed": "Failed to load file: {{message}}", "options_reset": "Options reset to defaults" }, @@ -331,7 +333,7 @@ }, "consent": { "title": "Before you start", - "intro": "JSONPrism runs entirely in your browser — your data never leaves your device. Please review and accept the terms:", + "intro": "JSONPrism runs entirely on your device — your data never leaves it. Please review and accept the terms:", "agree": "I have read and agree to the License, Terms (EULA), Privacy Policy, and Disclaimer.", "continue": "Continue", "links": { diff --git a/src/i18n/locales/vi.json b/src/i18n/locales/vi.json index 3fcf226..066dec6 100644 --- a/src/i18n/locales/vi.json +++ b/src/i18n/locales/vi.json @@ -174,6 +174,8 @@ "copy_failed": "Không thể chép vào clipboard", "downloaded": "Đã tải {{filename}}", "file_loaded": "Đã nạp {{name}}", + "files_loaded": "Đã nạp {{count}} file", + "files_loaded_other": "Đã nạp {{count}} file", "file_failed": "Không nạp được file: {{message}}", "options_reset": "Đã khôi phục tuỳ chọn mặc định" }, @@ -331,7 +333,7 @@ }, "consent": { "title": "Trước khi bắt đầu", - "intro": "JSONPrism chạy hoàn toàn trong trình duyệt của bạn — dữ liệu không bao giờ rời khỏi thiết bị. Vui lòng đọc và chấp nhận các điều khoản:", + "intro": "JSONPrism chạy hoàn toàn trên thiết bị của bạn — dữ liệu không bao giờ rời khỏi máy. Vui lòng đọc và chấp nhận các điều khoản:", "agree": "Tôi đã đọc và đồng ý với Giấy phép, Điều khoản (EULA), Chính sách bảo mật và Tuyên bố miễn trừ trách nhiệm.", "continue": "Tiếp tục", "links": { diff --git a/src/lib/tauri.ts b/src/lib/tauri.ts index e6d09e6..fbcba0a 100644 --- a/src/lib/tauri.ts +++ b/src/lib/tauri.ts @@ -152,6 +152,20 @@ export async function fileFromNativePath(path: string): Promise { return new File([text], name, { type, lastModified }); } +/** + * Open an external URL. In Tauri the webview won't honour `target="_blank"` + * (Android in particular swallows it), so route through the shell plugin to + * hand the URL to the system browser. On the web, open a new tab as usual. + */ +export async function openExternal(url: string): Promise { + if (!isTauri()) { + window.open(url, '_blank', 'noopener,noreferrer'); + return; + } + const { open } = await import('@tauri-apps/plugin-shell'); + await open(url); +} + /** * Diagnostic info from the Rust side (OS, arch, app version). * Not currently shown in the UI but useful for bug reports. diff --git a/src/stores/consentStore.ts b/src/stores/consentStore.ts index 63fbe79..831854d 100644 --- a/src/stores/consentStore.ts +++ b/src/stores/consentStore.ts @@ -7,9 +7,10 @@ interface ConsentState { } function initialAccepted(): boolean { - // Desktop builds gate acceptance through the installer's license page, so the - // in-app web gate is a no-op there. - if (__IS_TAURI_BUILD__) return true; + // Desktop installers gate acceptance through their license page, so the in-app + // gate is a no-op there. The Android sideload APK has no installer license + // step, so it must show the gate just like the web build. + if (__IS_TAURI_BUILD__ && !__IS_ANDROID_BUILD__) return true; return isConsentValid(loadConsent()); } diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index dad8e88..b9a451e 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -2,5 +2,6 @@ declare const __APP_VERSION__: string; declare const __IS_TAURI_BUILD__: boolean; +declare const __IS_ANDROID_BUILD__: boolean; declare const __APP_COMMIT__: string; declare const __APP_BUILD_DATE__: string; diff --git a/vite.config.ts b/vite.config.ts index 388123d..078deab 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,9 @@ import pkg from './package.json'; // Tauri exposes env vars during dev/build; presence indicates a Tauri context. const isTauriContext = !!process.env.TAURI_ENV_PLATFORM; +// `android` during `tauri android build`. The Android sideload APK has no +// installer license page, so it must show the in-app consent gate (web does). +const isAndroidBuild = process.env.TAURI_ENV_PLATFORM === 'android'; const tauriHost = process.env.TAURI_DEV_HOST; // Resolve commit SHA + build date once per Vite config load. The About page @@ -32,6 +35,7 @@ export default defineConfig(({ mode }) => ({ define: { __APP_VERSION__: JSON.stringify(pkg.version), __IS_TAURI_BUILD__: JSON.stringify(isTauriContext), + __IS_ANDROID_BUILD__: JSON.stringify(isAndroidBuild), __APP_COMMIT__: JSON.stringify(appCommit), __APP_BUILD_DATE__: JSON.stringify(appBuildDate), }, diff --git a/vitest.config.ts b/vitest.config.ts index 0a921fd..4b5698b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -8,6 +8,7 @@ export default defineConfig({ define: { __APP_VERSION__: JSON.stringify(pkg.version), __IS_TAURI_BUILD__: 'false', + __IS_ANDROID_BUILD__: 'false', __APP_COMMIT__: '"test"', __APP_BUILD_DATE__: '"1970-01-01"', },