fix: mobile UX improvements - scroll restoration, OTP autofill, swipe direction, back button
- ClientLayout: implement proper manual scroll restoration using module-level _wasPopNavigation flag and sessionStorage. Fixes scroll jumping to top/bottom on Back navigation. Scroll position saved on scroll (debounced) and cleanup, restored via double-rAF on popstate navigation. - AuthModal/LoginModal: fix Android back button to push only ONE history sentinel on modal open (not on every subView/step change). Prevents stacking of extra history entries. Back in sub-views re-pushes sentinel for next back press. - AuthModal/LoginModal: remove navigator.credentials.get (WebOTP API) which was showing an intrusive permission modal. The native autoComplete=one-time-code on the first OTP input provides the suggestion bar above the keyboard instead. - AuthModal/LoginModal: add isVerifyingRef guard to prevent double toast on successful login. Both onComplete callback and form submit were calling triggerVerifyOtp/handleVerifyOtpWithCode simultaneously. - AuthModal: remove triggerVerifyOtp from handleOtpChange to eliminate the second duplicate call (only onComplete now triggers verification). - OtpInput5: remove maxLength=1 restriction from first input so browser can inject the full 5-digit OTP code. Multi-char input is already handled by handleDigitChange which distributes digits across all boxes. - ProductPage: fix swipe gesture direction in media gallery. Swipe right -> next item, swipe left -> previous item (matches RTL intuition for Persian users).
This commit is contained in:
1 parent
1d2d141514
commit
86e8101d8c
16 files changed
+10175
-9886
No files matched your search
@@ -77,6 +77,8 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
const phoneNumberRef = useRef(phoneNumber);
|
||||
const subViewRef = useRef(subView);
|
||||
const isOpenRef = useRef(isOpen);
|
||||
// Guard against double-verify (onComplete fires, then form submit also fires)
|
||||
const isVerifyingRef = useRef(false);
|
||||
const isB2BEnabled = useSettingsStore((s) => s.getBoolean('b2bRegistrationOpen', true) && s.getBoolean('b2b_enabled', true));
|
||||
|
||||
useEffect(() => {
|
||||
@@ -92,17 +94,17 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
}, [isOpen]);
|
||||
|
||||
// Handle hardware / browser back button for Android & mobile navigation
|
||||
// IMPORTANT: only depend on isOpen here — NOT subView. If we depend on subView,
|
||||
// every subView change pushes a new history entry and back button never closes the modal.
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
|
||||
// Push a state for this modal level
|
||||
const currentState = window.history.state || {};
|
||||
window.history.pushState({ ...currentState, __authModalOpen: true, __subView: subView }, "");
|
||||
// Push ONE history entry when the modal opens so back button hits this entry first
|
||||
window.history.pushState({ __authModalOpen: true }, "");
|
||||
|
||||
const handlePopState = (e: PopStateEvent) => {
|
||||
// If user hit Android back button
|
||||
const handlePopState = () => {
|
||||
if (subViewRef.current !== "main") {
|
||||
// Go back inside modal
|
||||
// Navigate back within the modal
|
||||
if (subViewRef.current === "forgot-otp") {
|
||||
setSubView("forgot-phone");
|
||||
} else if (subViewRef.current === "reset-password") {
|
||||
@@ -110,8 +112,10 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
} else {
|
||||
setSubView("main");
|
||||
}
|
||||
// Push another sentinel so the next back also hits this handler
|
||||
window.history.pushState({ __authModalOpen: true }, "");
|
||||
} else {
|
||||
// In main view: close modal without navigating the page behind
|
||||
// Already at main view — close the modal
|
||||
onClose();
|
||||
}
|
||||
};
|
||||
@@ -120,7 +124,8 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
return () => {
|
||||
window.removeEventListener("popstate", handlePopState);
|
||||
};
|
||||
}, [isOpen, subView, onClose]);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isOpen]);
|
||||
|
||||
// Reset modal state on open/close
|
||||
useEffect(() => {
|
||||
@@ -160,6 +165,9 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
const cleanCode = extractOtpFromText(codeToVerify);
|
||||
const cleanPhone = toEnglishDigits(phoneNumberRef.current).replace(/[^0-9]/g, "");
|
||||
if (cleanCode.length !== 5 || !cleanPhone) return;
|
||||
// Prevent double-call from onComplete + form submit firing simultaneously
|
||||
if (isVerifyingRef.current) return;
|
||||
isVerifyingRef.current = true;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
@@ -172,6 +180,7 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
} catch (err: unknown) {
|
||||
const errObj = err as { message?: string };
|
||||
toast.error(errObj.message || "کد تایید نامعتبر است");
|
||||
isVerifyingRef.current = false;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -179,48 +188,20 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
[fetchProfile, onClose],
|
||||
);
|
||||
|
||||
// WebOTP API SMS Auto-read
|
||||
useEffect(() => {
|
||||
if (subView !== "otp-verify" && subView !== "forgot-otp") return;
|
||||
|
||||
let isMounted = true;
|
||||
const ac = new AbortController();
|
||||
|
||||
if (typeof window !== "undefined" && "OTPCredential" in window) {
|
||||
(navigator as unknown as { credentials: { get: (opts: unknown) => Promise<{ code?: string }> } }).credentials
|
||||
?.get({
|
||||
otp: { transport: ["sms"] },
|
||||
signal: ac.signal,
|
||||
})
|
||||
.then((otp) => {
|
||||
if (!isMounted) return;
|
||||
if (otp && typeof otp.code === "string") {
|
||||
const clean = extractOtpFromText(otp.code);
|
||||
if (clean) {
|
||||
setOtpCode(clean);
|
||||
if (clean.length === 5 && subView === "otp-verify") {
|
||||
triggerVerifyOtp(clean);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
ac.abort();
|
||||
};
|
||||
}, [subView, triggerVerifyOtp]);
|
||||
// NOTE: We intentionally do NOT use navigator.credentials.get (WebOTP API) here
|
||||
// because it shows an intrusive browser permission modal that is poor UX.
|
||||
// autoComplete="one-time-code" on the first OTP input provides the native
|
||||
// suggestion bar above the keyboard on Android without any permission dialog.
|
||||
|
||||
const handleOtpChange = (val: string) => {
|
||||
const clean = extractOtpFromText(val);
|
||||
setOtpCode(clean);
|
||||
if (clean.length === 5 && subView === "otp-verify") {
|
||||
triggerVerifyOtp(clean);
|
||||
}
|
||||
// Auto-verify is handled by OtpInput5's onComplete callback only
|
||||
// to prevent double-submission
|
||||
};
|
||||
|
||||
|
||||
const handleSendOtp = async (e?: React.FormEvent) => {
|
||||
if (e) e.preventDefault();
|
||||
const cleanPhone = toEnglishDigits(phoneNumber).trim();
|
||||
@@ -915,6 +896,7 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
disabled={isLoading}
|
||||
autoFocus={true}
|
||||
/>
|
||||
|
||||
</div>
|
||||
|
||||
<button
|
||||
|
||||
@@ -28,20 +28,24 @@ export default function LoginModal({ isOpen, onClose, onLogin, petName, isAdviso
|
||||
const { fetchProfile } = useUserStore();
|
||||
|
||||
const stepRef = useRef(step);
|
||||
const isVerifyingRef = useRef(false);
|
||||
useEffect(() => {
|
||||
stepRef.current = step;
|
||||
}, [step]);
|
||||
|
||||
|
||||
// Handle hardware / Android back button
|
||||
// IMPORTANT: only depend on isOpen — NOT step, to avoid stacking extra history entries
|
||||
useEffect(() => {
|
||||
if (!isOpen) return;
|
||||
|
||||
const currentState = window.history.state || {};
|
||||
window.history.pushState({ ...currentState, __loginModalOpen: true, __loginStep: step }, "");
|
||||
window.history.pushState({ __loginModalOpen: true }, "");
|
||||
|
||||
const handlePopState = () => {
|
||||
if (stepRef.current === "otp") {
|
||||
setStep("phone");
|
||||
// Push sentinel again so next back also hits this handler
|
||||
window.history.pushState({ __loginModalOpen: true }, "");
|
||||
} else {
|
||||
onClose();
|
||||
}
|
||||
@@ -51,7 +55,9 @@ export default function LoginModal({ isOpen, onClose, onLogin, petName, isAdviso
|
||||
return () => {
|
||||
window.removeEventListener("popstate", handlePopState);
|
||||
};
|
||||
}, [isOpen, step, onClose]);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isOpen]);
|
||||
|
||||
|
||||
const handleVerifyOtpWithCode = React.useCallback(async (code: string) => {
|
||||
const cleanCode = code.trim();
|
||||
@@ -59,6 +65,9 @@ export default function LoginModal({ isOpen, onClose, onLogin, petName, isAdviso
|
||||
toast.error("کد تایید باید ۵ رقم باشد");
|
||||
return;
|
||||
}
|
||||
// Guard against double-fire from onComplete + WebOTP
|
||||
if (isVerifyingRef.current) return;
|
||||
isVerifyingRef.current = true;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
@@ -72,36 +81,16 @@ export default function LoginModal({ isOpen, onClose, onLogin, petName, isAdviso
|
||||
} catch (err: unknown) {
|
||||
const errObj = err as { message?: string };
|
||||
toast.error(errObj.message || "کد تایید اشتباه است");
|
||||
isVerifyingRef.current = false;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [phoneNumber, fetchProfile, onLogin, onClose]);
|
||||
|
||||
// WebOTP API & Auto-Submit
|
||||
useEffect(() => {
|
||||
if (step === "otp" && typeof window !== "undefined" && "OTPCredential" in window) {
|
||||
const ac = new AbortController();
|
||||
const creds = (navigator as unknown as { credentials?: { get?: (opt: unknown) => Promise<{ code?: string }> } }).credentials;
|
||||
if (creds && creds.get) {
|
||||
creds.get({
|
||||
otp: { transport: ["sms"] },
|
||||
signal: ac.signal,
|
||||
})
|
||||
.then((otp) => {
|
||||
if (otp && otp.code) {
|
||||
const clean = otp.code.replace(/[^0-9]/g, "").slice(0, 5);
|
||||
setOtpCode(clean);
|
||||
if (clean.length === 5) {
|
||||
handleVerifyOtpWithCode(clean);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
return () => ac.abort();
|
||||
}
|
||||
}, [step, handleVerifyOtpWithCode]);
|
||||
// NOTE: We intentionally do NOT use navigator.credentials.get (WebOTP API) here.
|
||||
// autoComplete="one-time-code" on the first OTP input provides the native
|
||||
// suggestion bar above the keyboard on Android without any permission dialog.
|
||||
|
||||
// Reset modal state when closed or opened
|
||||
useEffect(() => {
|
||||
|
||||
@@ -121,7 +121,9 @@ export default function OtpInput5({
|
||||
type="tel"
|
||||
inputMode="numeric"
|
||||
pattern="[0-9]*"
|
||||
maxLength={1}
|
||||
// idx === 0: no maxLength so browser OTP autofill can inject the full code.
|
||||
// The handleDigitChange handler distributes multi-char input across all boxes.
|
||||
maxLength={idx === 0 ? undefined : 1}
|
||||
autoComplete={idx === 0 ? "one-time-code" : "off"}
|
||||
value={val}
|
||||
disabled={disabled}
|
||||
|
||||
@@ -460,18 +460,18 @@ export default function ProductPage({ productSlug }: { productSlug: string }) {
|
||||
onDragEnd={(e, info) => {
|
||||
const swipeThreshold = 50;
|
||||
if (info.offset.x > swipeThreshold || info.velocity.x > 300) {
|
||||
// Swipe right -> in RTL, swipe right goes to previous item, or left/right intuitive navigation
|
||||
if (activeMediaIndex > 0) {
|
||||
const newIdx = activeMediaIndex - 1;
|
||||
// Swipe right → next item (RTL intuition: user slides right to advance)
|
||||
if (activeMediaIndex < galleryMedia.length - 1) {
|
||||
const newIdx = activeMediaIndex + 1;
|
||||
setActiveMediaIndex(newIdx);
|
||||
if (galleryMedia[newIdx]?.type === 'image') {
|
||||
setActiveImage(galleryMedia[newIdx].url);
|
||||
}
|
||||
}
|
||||
} else if (info.offset.x < -swipeThreshold || info.velocity.x < -300) {
|
||||
// Swipe left -> go to next item
|
||||
if (activeMediaIndex < galleryMedia.length - 1) {
|
||||
const newIdx = activeMediaIndex + 1;
|
||||
// Swipe left → previous item
|
||||
if (activeMediaIndex > 0) {
|
||||
const newIdx = activeMediaIndex - 1;
|
||||
setActiveMediaIndex(newIdx);
|
||||
if (galleryMedia[newIdx]?.type === 'image') {
|
||||
setActiveImage(galleryMedia[newIdx].url);
|
||||
|
||||
Reference in new issue
Block a user