diff --git a/.changeset/user-preferences-addition.md b/.changeset/user-preferences-addition.md new file mode 100644 index 000000000..0711891c7 --- /dev/null +++ b/.changeset/user-preferences-addition.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: small addition to user preferences diff --git a/packages/app/pages/_app.tsx b/packages/app/pages/_app.tsx index 54c437f8b..838b6089d 100644 --- a/packages/app/pages/_app.tsx +++ b/packages/app/pages/_app.tsx @@ -13,6 +13,7 @@ import { import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { usePageTitle } from '@/brand'; +import { CrtOverlay } from '@/components/CrtOverlay'; import { MANTINE_FONT_MAP } from '@/config/fonts'; import { ibmPlexMono, inter, roboto, robotoMono } from '@/fonts'; import { fetchServerVersion, installHdxDebug } from '@/hdxDebug'; @@ -22,6 +23,7 @@ import { SystemColorSchemeScript, useContentFontSize, useResolvedColorScheme, + useUserPreferences, } from '@/useUserPreferences'; import '@mantine/core/styles.css'; @@ -79,6 +81,7 @@ function AppContent({ pageProps: AppProps['pageProps']; }) { const resolvedColorScheme = useResolvedColorScheme(); + const { userPreferences } = useUserPreferences(); const { base: contentFontSize, compact: compactContentFontSize } = useContentFontSize(); @@ -107,6 +110,7 @@ function AppContent({ {getLayout()} + {userPreferences.crtMode && } ); } diff --git a/packages/app/src/UserPreferencesModal.tsx b/packages/app/src/UserPreferencesModal.tsx index 6bc00950d..273693814 100644 --- a/packages/app/src/UserPreferencesModal.tsx +++ b/packages/app/src/UserPreferencesModal.tsx @@ -1,9 +1,19 @@ import * as React from 'react'; import { Badge } from '@clickhouse/click-ui'; -import { Divider, Group, Modal, Select, Stack, Text } from '@mantine/core'; +import { + Divider, + Group, + Modal, + Select, + Stack, + Switch, + Text, +} from '@mantine/core'; +import { CrtUnlockReveal } from './components/CrtUnlockReveal'; import { TimeZoneSelect, TimeZoneShortcuts } from './components/TimeZoneSelect'; import { isContentFontSize, OPTIONS_CONTENT_FONT_SIZE } from './config/fonts'; +import { useKonamiCode } from './hooks/useKonamiCode'; import { DEFAULT_ROW_CLICK_ACTION, RowClickAction, @@ -56,6 +66,36 @@ export const UserPreferencesModal = ({ }) => { const { userPreferences, setUserPreference } = useUserPreferences(); const { contentFontSize } = useContentFontSize(); + const [crtUnlocked, setCrtUnlocked] = React.useState(false); + // AppNav keeps this component mounted while closed, so gate on `opened`. + useKonamiCode( + React.useCallback(() => opened && setCrtUnlocked(true), [opened]), + ); + // Stays true once CRT mode has been on while the modal is open, so turning + // the switch off does not remove it mid-click. + const [crtVisible, setCrtVisible] = React.useState(false); + React.useEffect(() => { + if (!opened) { + setCrtUnlocked(false); + setCrtVisible(false); + } else if (userPreferences.crtMode) { + setCrtVisible(true); + } + }, [opened, userPreferences.crtMode]); + + const crtSetting = ( + + setUserPreference({ crtMode: e.currentTarget.checked })} + /> + + ); return ( + + {crtUnlocked ? ( + {crtSetting} + ) : ( + crtVisible && crtSetting + )} ); diff --git a/packages/app/src/__tests__/UserPreferencesModal.test.tsx b/packages/app/src/__tests__/UserPreferencesModal.test.tsx index ddd9213b7..abc33a817 100644 --- a/packages/app/src/__tests__/UserPreferencesModal.test.tsx +++ b/packages/app/src/__tests__/UserPreferencesModal.test.tsx @@ -1,3 +1,4 @@ +import * as React from 'react'; import { Provider } from 'jotai'; import { screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; @@ -99,4 +100,58 @@ describe('UserPreferencesModal', () => { expect(readStoredPreferences().rowClickAction).toBe('expand'); }); }); + + describe('CRT mode', () => { + const KONAMI = + '{ArrowUp}{ArrowUp}{ArrowDown}{ArrowDown}{ArrowLeft}{ArrowRight}{ArrowLeft}{ArrowRight}ba'; + + it('hides the toggle until the Konami code is typed', async () => { + renderModal(); + expect(screen.queryByTestId('crt-mode-switch')).toBeNull(); + + await userEvent.keyboard(KONAMI); + expect(screen.getByTestId('crt-unlock-scene')).toBeInTheDocument(); + await userEvent.click(screen.getByTestId('crt-mode-switch')); + + expect(readStoredPreferences().crtMode).toBe(true); + }); + + it('ignores the Konami code while the modal is closed', async () => { + window.localStorage.clear(); + const ClosedThenOpened = () => { + const [opened, setOpened] = React.useState(false); + return ( + <> + + {}} /> + + ); + }; + renderWithMantine( + + + , + ); + await userEvent.keyboard(KONAMI); + await userEvent.click(screen.getByText('open')); + await screen.findByTestId('time-zone-select'); + + expect(screen.queryByTestId('crt-mode-switch')).toBeNull(); + }); + + it('shows the toggle while CRT mode is on so it can be turned off', () => { + renderModal({ crtMode: true }); + + expect(screen.getByTestId('crt-mode-switch')).toBeChecked(); + }); + + it('keeps the toggle after CRT mode is turned off', async () => { + renderModal({ crtMode: true }); + + await userEvent.click(screen.getByRole('switch', { name: 'CRT mode' })); + + expect(readStoredPreferences().crtMode).toBe(false); + expect(screen.getByTestId('crt-mode-switch')).not.toBeChecked(); + }); + }); }); diff --git a/packages/app/src/components/CrtOverlay.module.scss b/packages/app/src/components/CrtOverlay.module.scss new file mode 100644 index 000000000..21d064ca3 --- /dev/null +++ b/packages/app/src/components/CrtOverlay.module.scss @@ -0,0 +1,99 @@ +// Above modals and notifications; it never takes pointer events. +.overlay { + position: fixed; + inset: 0; + z-index: 2147483647; + pointer-events: none; + + // Rounded screen corners plus edge shading read as a curved tube. + border-radius: 3vmin / 4vmin; + box-shadow: + 0 0 0 10vmin #000, + inset 0 0 10vmin rgb(0 0 0 / 55%); + background: radial-gradient( + ellipse at center, + rgb(255 255 255 / 3%) 0%, + transparent 55%, + rgb(0 0 0 / 35%) 100% + ); + + &::before, + &::after { + content: ''; + position: absolute; + inset: 0; + } + + // The light half keeps the lines visible on the dark theme. + &::before { + background: repeating-linear-gradient( + to bottom, + rgb(0 0 0 / 30%) 0, + rgb(0 0 0 / 30%) 2px, + rgb(255 255 255 / 3%) 2px, + rgb(255 255 255 / 3%) 4px + ); + } + + // RGB phosphor mask; this layer also carries the flicker. + &::after { + background: repeating-linear-gradient( + to right, + rgb(255 0 0 / 5%) 0, + rgb(255 0 0 / 5%) 1px, + rgb(0 255 0 / 4%) 1px, + rgb(0 255 0 / 4%) 2px, + rgb(0 0 255 / 5%) 2px, + rgb(0 0 255 / 5%) 3px + ); + will-change: opacity; + animation: flicker 0.15s steps(1) infinite; + + @media (prefers-reduced-motion: reduce) { + animation: none; + } + } +} + +.roll { + position: absolute; + inset: 0 0 auto; + height: 15vh; + background: linear-gradient( + to bottom, + transparent, + rgb(255 255 255 / 7%), + transparent + ); + transform: translateY(-15vh); + will-change: transform; + animation: roll 8s linear infinite; + + @media (prefers-reduced-motion: reduce) { + display: none; + } +} + +@keyframes roll { + to { + transform: translateY(100vh); + } +} + +@keyframes flicker { + 0% { + opacity: 0.7; + } + + 50% { + opacity: 1; + } +} + +// Chromatic aberration and phosphor glow on all text. `.root` goes on . +.root body { + text-shadow: + 0.6px 0 rgb(255 0 80 / 35%), + -0.6px 0 rgb(0 200 255 / 35%), + 0 0 2px color-mix(in srgb, currentcolor 60%, transparent); +} diff --git a/packages/app/src/components/CrtOverlay.tsx b/packages/app/src/components/CrtOverlay.tsx new file mode 100644 index 000000000..d1cca7eb2 --- /dev/null +++ b/packages/app/src/components/CrtOverlay.tsx @@ -0,0 +1,18 @@ +import { useEffect } from 'react'; + +import styles from './CrtOverlay.module.scss'; + +/** Full-viewport CRT effect. Mount only while the preference is on. */ +export function CrtOverlay() { + useEffect(() => { + // A module class, not a global selector: the production build rejects those. + document.documentElement.classList.add(styles.root); + return () => document.documentElement.classList.remove(styles.root); + }, []); + + return ( +
+
+
+ ); +} diff --git a/packages/app/src/components/CrtUnlockReveal.module.scss b/packages/app/src/components/CrtUnlockReveal.module.scss new file mode 100644 index 000000000..2f446a71b --- /dev/null +++ b/packages/app/src/components/CrtUnlockReveal.module.scss @@ -0,0 +1,196 @@ +$phosphor: #3f6; + +// When the scene collapses; the setting powers on as it finishes. +$collapse-at: 3.1s; + +// Scene and content share one grid cell so the scene sits on top of the setting. +.root { + display: grid; + + > * { + grid-area: 1 / 1; + } +} + +.content { + align-self: center; + animation: power-on 0.5s ($collapse-at + 0.3s) both ease-out; +} + +.flash { + position: fixed; + inset: 0; + z-index: 2147483646; + pointer-events: none; + background: #fff; + opacity: 0; + animation: flash 0.6s ease-out; +} + +.scene { + position: relative; + z-index: 1; + overflow: hidden; + padding: 0.75rem 1rem; + border-radius: 6px; + background: #000; + color: $phosphor; + font-family: var(--mantine-font-family-monospace); + font-size: 0.85rem; + line-height: 1.6; + text-shadow: 0 0 6px rgba($phosphor, 0.8); + box-shadow: inset 0 0 2rem rgba($phosphor, 0.25); + animation: + shake 0.4s 0.05s, + collapse 0.35s $collapse-at forwards ease-in; + + &::after { + content: ''; + position: absolute; + inset: 0; + pointer-events: none; + background: repeating-linear-gradient( + to bottom, + rgb(0 0 0 / 35%) 0, + rgb(0 0 0 / 35%) 1px, + transparent 1px, + transparent 3px + ); + } +} + +// Width counts the "> " prompt as two extra characters. +.line { + display: inline-block; + width: 0; + overflow: hidden; + white-space: nowrap; + vertical-align: bottom; + animation: type 0.5s var(--delay) steps(calc(var(--chars) + 2)) forwards; +} + +.cursor::after { + content: '_'; + animation: blink 0.5s steps(1) infinite; +} + +.bar { + width: 22ch; + height: 0.9rem; + margin: 0.3rem 0; + border: 1px solid currentcolor; + box-shadow: 0 0 6px rgba($phosphor, 0.6); +} + +.barFill { + width: 0; + height: 100%; + background: repeating-linear-gradient( + to right, + currentcolor 0, + currentcolor 0.8ch, + transparent 0.8ch, + transparent 1ch + ); + animation: fill 1.1s 1.3s steps(22) forwards; +} + +@media (prefers-reduced-motion: reduce) { + .scene, + .flash { + display: none; + } + + .content { + animation: none; + } +} + +@keyframes flash { + 0% { + opacity: 0.85; + } + + 100% { + opacity: 0; + } +} + +@keyframes shake { + 0%, + 100% { + transform: translate(0, 0); + } + + 20% { + transform: translate(-6px, 2px); + } + + 40% { + transform: translate(5px, -3px); + } + + 60% { + transform: translate(-4px, 1px); + } + + 80% { + transform: translate(3px, -1px); + } +} + +@keyframes type { + to { + width: calc((var(--chars) + 2) * 1ch); + } +} + +@keyframes fill { + to { + width: 100%; + } +} + +@keyframes blink { + 50% { + opacity: 0; + } +} + +// A CRT switching off: squash to a bright line, then to a dot. +@keyframes collapse { + 0% { + transform: none; + filter: none; + } + + 60% { + transform: scale(1, 0.02); + filter: brightness(3); + } + + 100% { + transform: scale(0, 0.02); + filter: brightness(5); + } +} + +@keyframes power-on { + 0% { + opacity: 0; + transform: scale(0, 0.02); + filter: brightness(4); + } + + 40% { + opacity: 1; + transform: scale(1, 0.02); + filter: brightness(3); + } + + 100% { + opacity: 1; + transform: none; + filter: none; + } +} diff --git a/packages/app/src/components/CrtUnlockReveal.tsx b/packages/app/src/components/CrtUnlockReveal.tsx new file mode 100644 index 000000000..69f4385f3 --- /dev/null +++ b/packages/app/src/components/CrtUnlockReveal.tsx @@ -0,0 +1,78 @@ +import * as React from 'react'; +import { Portal } from '@mantine/core'; + +import styles from './CrtUnlockReveal.module.scss'; + +const LINES = [ + { text: 'Cheat code accepted', delay: 0.3 }, + { text: 'Loading crt_mode.sys', delay: 0.8 }, + { text: 'Warming up the tube...', delay: 2.4 }, +]; + +/** Plays a retro boot scene over `children`, then powers them on underneath. */ +export function CrtUnlockReveal({ children }: { children: React.ReactNode }) { + const [playing, setPlaying] = React.useState(true); + + return ( +
+
{children}
+ {playing && ( + <> + {/* The modal content has a transform, which would clip a fixed child to the modal. */} + +
+ +
{ + // Child animations bubble up here; only the final collapse ends the scene. + if ( + e.target === e.currentTarget && + e.animationName.includes('collapse') + ) { + setPlaying(false); + } + }} + > + {LINES.slice(0, 2).map(line => ( + + ))} +
+
+
+ +
+ + )} +
+ ); +} + +function Line({ + text, + delay, + cursor, +}: { + text: string; + delay: number; + cursor?: boolean; +}) { + return ( +
+ + {'> '} + {text} + +
+ ); +} diff --git a/packages/app/src/components/__tests__/CrtOverlay.test.tsx b/packages/app/src/components/__tests__/CrtOverlay.test.tsx new file mode 100644 index 000000000..298e63f58 --- /dev/null +++ b/packages/app/src/components/__tests__/CrtOverlay.test.tsx @@ -0,0 +1,13 @@ +import { render } from '@testing-library/react'; + +import { CrtOverlay } from '@/components/CrtOverlay'; + +describe('CrtOverlay', () => { + it('adds the root class to while mounted and removes it after', () => { + const { unmount } = render(); + expect(document.documentElement).toHaveClass('root'); + + unmount(); + expect(document.documentElement).not.toHaveClass('root'); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useKonamiCode.test.ts b/packages/app/src/hooks/__tests__/useKonamiCode.test.ts new file mode 100644 index 000000000..8ea6f3a53 --- /dev/null +++ b/packages/app/src/hooks/__tests__/useKonamiCode.test.ts @@ -0,0 +1,65 @@ +import { act, renderHook } from '@testing-library/react'; + +import { useKonamiCode } from '@/hooks/useKonamiCode'; + +const CODE = [ + 'ArrowUp', + 'ArrowUp', + 'ArrowDown', + 'ArrowDown', + 'ArrowLeft', + 'ArrowRight', + 'ArrowLeft', + 'ArrowRight', + 'b', + 'a', +]; + +const press = (keys: string[]) => + act(() => { + keys.forEach(key => + window.dispatchEvent(new KeyboardEvent('keydown', { key })), + ); + }); + +describe('useKonamiCode', () => { + it('fires once per full sequence', () => { + const onMatch = jest.fn(); + renderHook(() => useKonamiCode(onMatch)); + + press(CODE.slice(0, -1)); + expect(onMatch).not.toHaveBeenCalled(); + + press(['a']); + expect(onMatch).toHaveBeenCalledTimes(1); + }); + + it('matches after stray keys and with caps lock on', () => { + const onMatch = jest.fn(); + renderHook(() => useKonamiCode(onMatch)); + + press(['ArrowUp', 'x', 'ArrowUp', ...CODE.slice(0, -2), 'B', 'A']); + expect(onMatch).toHaveBeenCalledTimes(1); + }); + + it('ignores keydown events with no key', () => { + const onError = jest.fn(); + window.addEventListener('error', onError); + renderHook(() => useKonamiCode(jest.fn())); + + act(() => { + window.dispatchEvent(new Event('keydown')); + }); + window.removeEventListener('error', onError); + expect(onError).not.toHaveBeenCalled(); + }); + + it('stops listening on unmount', () => { + const onMatch = jest.fn(); + const { unmount } = renderHook(() => useKonamiCode(onMatch)); + + unmount(); + press(CODE); + expect(onMatch).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/hooks/useKonamiCode.ts b/packages/app/src/hooks/useKonamiCode.ts new file mode 100644 index 000000000..8d135b5b4 --- /dev/null +++ b/packages/app/src/hooks/useKonamiCode.ts @@ -0,0 +1,33 @@ +import { useEffect } from 'react'; + +const KONAMI_CODE = [ + 'ArrowUp', + 'ArrowUp', + 'ArrowDown', + 'ArrowDown', + 'ArrowLeft', + 'ArrowRight', + 'ArrowLeft', + 'ArrowRight', + 'b', + 'a', +].join(); + +/** Calls `onMatch` each time the Konami code is typed. Pass a stable callback. */ +export function useKonamiCode(onMatch: () => void) { + useEffect(() => { + const recent: string[] = []; + const onKeyDown = (e: KeyboardEvent) => { + // Browser autofill dispatches keydown events with no `key`. + if (!e.key) return; + recent.push(e.key.length === 1 ? e.key.toLowerCase() : e.key); + if (recent.length > 10) recent.shift(); + if (recent.join() === KONAMI_CODE) { + recent.length = 0; + onMatch(); + } + }; + window.addEventListener('keydown', onKeyDown); + return () => window.removeEventListener('keydown', onKeyDown); + }, [onMatch]); +} diff --git a/packages/app/src/useUserPreferences.tsx b/packages/app/src/useUserPreferences.tsx index fec956733..d2961da14 100644 --- a/packages/app/src/useUserPreferences.tsx +++ b/packages/app/src/useUserPreferences.tsx @@ -45,6 +45,8 @@ export type UserPreferences = { * fall back to `DEFAULT_ROW_CLICK_ACTION`. */ rowClickAction?: RowClickAction; + /** CRT screen effect. The toggle stays hidden until the Konami code is typed. */ + crtMode?: boolean; }; // Legacy type for migration