diff --git a/src/components/atoms/Links/UrlLink.jsx b/src/components/atoms/Links/UrlLink.jsx index f5a0fa9..7132562 100644 --- a/src/components/atoms/Links/UrlLink.jsx +++ b/src/components/atoms/Links/UrlLink.jsx @@ -13,14 +13,16 @@ export const UrlLink = (props) => { const {text,url,onClick,onOpened,logger = new EventLogger('Incyclist')} = props; const onOpen = (e) => { - e.preventDefault(); if (logger) logger.logEvent({message:'external link clicked',url,eventSource:'user'}) - if (onClick && typeof(onClick)==='function') + if (onClick && typeof(onClick)==='function') { + e.preventDefault(); return onClick(url) + } if ( hasFeature('shell.openExternal')) { + e.preventDefault(); api.shell.openExternal(url) if (onOpened) onOpened() @@ -28,6 +30,6 @@ export const UrlLink = (props) => { } return - { onOpen(e) } } href='#' target='_blank'>{text||url} + { onOpen(e) } } href={url} target='_blank' rel='noopener noreferrer'>{text||url} } diff --git a/src/components/atoms/Links/UrlLink.unit.test.jsx b/src/components/atoms/Links/UrlLink.unit.test.jsx new file mode 100644 index 0000000..b8071d3 --- /dev/null +++ b/src/components/atoms/Links/UrlLink.unit.test.jsx @@ -0,0 +1,60 @@ +import React from 'react' +import { beforeEach, describe, expect, test, vi } from 'vitest' +import { fireEvent, render, screen } from '@testing-library/react' + +const { hasFeature, openExternal } = vi.hoisted(() => ({ + hasFeature: vi.fn(), openExternal: vi.fn(), +})) +vi.mock('../../../utils/electron/integration', () => ({ + hasFeature, api: { shell: { openExternal } }, +})) + +import { UrlLink } from './UrlLink' + +const url = 'https://incyclist.com/faq/#street-view-simulate' +const logger = { logEvent: vi.fn() } + +beforeEach(() => { + vi.clearAllMocks() + hasFeature.mockReturnValue(false) +}) + +describe('UrlLink navigation', () => { + test('provides the actual URL for native browser navigation', () => { + render() + const link = screen.getByRole('link') + const click = new MouseEvent('click', { bubbles: true, cancelable: true }) + // Prevent jsdom navigation after observing whether the component cancelled it. + const wasPrevented = vi.fn() + document.addEventListener('click', event => { + wasPrevented(event.defaultPrevented) + event.preventDefault() + }, { once: true }) + fireEvent(link, click) + + expect(link).toHaveAttribute('href', url) + expect(link).toHaveAttribute('rel', 'noopener noreferrer') + expect(wasPrevented).toHaveBeenCalledWith(false) + expect(openExternal).not.toHaveBeenCalled() + }) + + test('uses Electron shell when available and reports the opened link', () => { + hasFeature.mockReturnValue(true) + const onOpened = vi.fn() + render() + + expect(fireEvent.click(screen.getByRole('link'))).toBe(false) + expect(openExternal).toHaveBeenCalledWith(url) + expect(onOpened).toHaveBeenCalledTimes(1) + expect(logger.logEvent).toHaveBeenCalledWith({ message: 'external link clicked', url, eventSource: 'user' }) + }) + + test('preserves custom click handling', () => { + const onClick = vi.fn() + render() + + expect(fireEvent.click(screen.getByRole('link'))).toBe(false) + expect(onClick).toHaveBeenCalledWith(url) + expect(openExternal).not.toHaveBeenCalled() + }) +}) diff --git a/src/components/modules/Settings/Ride/MapsApiKeyInfo.jsx b/src/components/modules/Settings/Ride/MapsApiKeyInfo.jsx new file mode 100644 index 0000000..68748d8 --- /dev/null +++ b/src/components/modules/Settings/Ride/MapsApiKeyInfo.jsx @@ -0,0 +1,103 @@ +import React, { useEffect, useLayoutEffect, useState } from 'react' +import styled from 'styled-components' +import { autoUpdate, flip, FloatingFocusManager, FloatingPortal, offset, shift, + useClick, useDismiss, useFloating, useInteractions, useRole } from '@floating-ui/react' +import { Button, Pill, UrlLink } from '../../../atoms' + +const supportsPopover = typeof HTMLElement !== 'undefined' + && 'popover' in HTMLElement.prototype && typeof HTMLElement.prototype.showPopover === 'function' + +const InfoButton = styled(Button)` + background: none; + border: none; + padding: 0; + margin: 0 0 1vh 0.8ch; + align-self: center; + cursor: pointer; + + &:focus-visible { + outline: 2px solid ${props => props.theme.url.color}; + outline-offset: 2px; + } + color: ${props => props.theme.colors.infoText}; +` + +const InfoPopup = styled.div` + inset: auto; + margin: 0; + box-sizing: border-box; + width: 360px; + max-width: calc(100vw - 16px); + max-height: calc(100dvh - 16px); + overflow: auto; + padding: 8px 12px; + background: ${props => props.theme.dialog.background}; + color: ${props => props.theme.dialog.text}; + border: ${props => props.theme.dialog.border}; + border-radius: ${props => props.theme.dialog.borderRadius}; + font-size: 14px; + line-height: 1.5; + text-align: left; + z-index: 1000; + + ul { margin: 0 0 8px; padding-left: 20px; } + li + li { margin-top: 8px; } +` + +export const MapsApiKeyInfo = () => { + const [isOpen, setIsOpen] = useState(false) + const { refs, elements, floatingStyles, context } = useFloating({ + open: isOpen, + onOpenChange: setIsOpen, + placement: 'bottom-end', + strategy: 'fixed', + middleware: [offset(8), flip({ padding: 8 }), shift({ padding: 8 })], + whileElementsMounted: autoUpdate, + }) + const click = useClick(context) + // The settings dialog handles Escape on keyup, so consume that same event first. + const dismiss = useDismiss(context, { escapeKey: false }) + const role = useRole(context, { role: 'dialog' }) + const { getReferenceProps, getFloatingProps } = useInteractions([click, dismiss, role]) + + useEffect(() => { + if (!isOpen) + return + const onEscape = event => { + if (event.key !== 'Escape') + return + event.stopPropagation() + setIsOpen(false) + refs.domReference.current?.focus() + } + document.addEventListener('keyup', onEscape, true) + return () => document.removeEventListener('keyup', onEscape, true) + }, [isOpen, refs]) + + useLayoutEffect(() => { + // Native top-layer rendering where supported; the portal also supports older browsers. + if (isOpen) + elements.floating?.showPopover?.() + }, [isOpen, elements.floating]) + + return <> + + + + {isOpen ? + + +
    +
  • Your own API key is required to simulate GPX rides with Street View or Satellite View.
  • +
  • Your own API key is required to enable side views (left and right).
  • +
  • Using your own API key reduces the risk of Incyclist running out of its shared free tier allowance.
  • +
+ +
+
+
: null} + +} diff --git a/src/components/modules/Settings/Ride/MapsApiKeyInfo.unit.test.jsx b/src/components/modules/Settings/Ride/MapsApiKeyInfo.unit.test.jsx new file mode 100644 index 0000000..8a28d7a --- /dev/null +++ b/src/components/modules/Settings/Ride/MapsApiKeyInfo.unit.test.jsx @@ -0,0 +1,82 @@ +import React from 'react' +import { describe, test, expect, vi } from 'vitest' +import { act, fireEvent, render, screen } from '@testing-library/react' +import { EventLogger } from 'gd-eventlog' +import { AppThemeProvider } from '../../../../theme' +import { MapsApiKeyInfo } from './MapsApiKeyInfo' + +const renderInfo = () => render() +const openInfo = async () => { + await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Info about Maps API Key' }))) + return screen.getByRole('dialog', { name: 'About Maps API Key' }) +} + +describe('Maps API key info pill', () => { + test('logs the info control click through the shared Button atom', async () => { + const logEvent = vi.spyOn(EventLogger.prototype, 'logEvent') + try { + renderInfo() + await openInfo() + + expect(logEvent).toHaveBeenCalledWith(expect.objectContaining({ + message: 'button clicked', button: 'maps-api-key-info', eventSource: 'user', + })) + } + finally { + logEvent.mockRestore() + } + }) + + test('opens on click and explains all three reasons with the FAQ deeplink', async () => { + renderInfo() + expect(screen.queryByRole('dialog')).toBeNull() + + const popup = await openInfo() + + expect(popup).toHaveTextContent('simulate GPX rides with Street View or Satellite View') + expect(popup).toHaveTextContent('enable side views (left and right)') + expect(popup).toHaveTextContent('running out of its shared free tier allowance') + expect(screen.getByRole('link', { name: /Learn more/ })).toHaveAttribute('href', + 'https://incyclist.com/faq/#street-view-simulate') + expect(screen.getByRole('button', { name: 'Info about Maps API Key' })).toHaveAttribute('aria-expanded', 'true') + }) + + test('keeps the link available when moving focus from the pill into the popup', async () => { + renderInfo() + await openInfo() + + const link = screen.getByRole('link') + fireEvent.blur(screen.getByRole('button', { name: 'Info about Maps API Key' }), { relatedTarget: link }) + fireEvent.focus(link) + + expect(screen.getByRole('dialog')).toBeInTheDocument() + }) + + test('Escape closes the help, restores focus and does not reach the settings handler', async () => { + renderInfo() + await openInfo() + const parentEscape = vi.fn() + window.addEventListener('keyup', parentEscape) + try { + fireEvent.keyUp(screen.getByRole('link'), { key: 'Escape', keyCode: 27 }) + + expect(screen.queryByRole('dialog')).toBeNull() + expect(screen.getByRole('button', { name: 'Info about Maps API Key' })).toHaveFocus() + expect(parentEscape).not.toHaveBeenCalled() + } + finally { + window.removeEventListener('keyup', parentEscape) + } + }) + + test('closes on a second click or when clicking outside', async () => { + renderInfo() + await openInfo() + fireEvent.click(screen.getByRole('button', { name: 'Info about Maps API Key' })) + expect(screen.queryByRole('dialog')).toBeNull() + + await openInfo() + fireEvent.pointerDown(document.body) + expect(screen.queryByRole('dialog')).toBeNull() + }) +}) diff --git a/src/components/modules/Settings/Ride/component.jsx b/src/components/modules/Settings/Ride/component.jsx index 63669ec..5517b2d 100644 --- a/src/components/modules/Settings/Ride/component.jsx +++ b/src/components/modules/Settings/Ride/component.jsx @@ -8,6 +8,7 @@ import { EditNumber,Button, Row, CheckBox, EditText, Column,Text, LinkButton} fr import { DynamicCoachList,DynamicCoachEdit } from '../../Coaches'; import { RIDE_MODES, RIDEVIEW } from './consts'; import { isValidGoogleMapsKeyFormat, maskGoogleMapsKey } from './validations'; +import { MapsApiKeyInfo } from './MapsApiKeyInfo'; export const ContentArea = styled.div` position:relative; @@ -313,6 +314,7 @@ export const RideSettingsView = ( props)=> { + diff --git a/src/components/modules/Settings/Ride/stories.jsx b/src/components/modules/Settings/Ride/stories.jsx index f928298..288ce2e 100644 --- a/src/components/modules/Settings/Ride/stories.jsx +++ b/src/components/modules/Settings/Ride/stories.jsx @@ -98,3 +98,11 @@ MapsKeySavedPendingRestart.args = { mapsApiKey: 'AIzaSyB0123456789012345678901234567890', mapsKeyPendingRestart: true, } + +export const MapsApiKeyHelp = Template.bind({}); +MapsApiKeyHelp.args = { + rideView: RIDEVIEW.STREETVIEW, +} +MapsApiKeyHelp.play = async ({ canvas, userEvent }) => { + await userEvent.click(canvas.getByRole('button', { name: 'Info about Maps API Key' })) +}