Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

IRO-1307 - Address UI lag in rendering Magic Link state #126

Merged
merged 40 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8592cf1
try summa this on staging
brekk Dec 6, 2021
f882adf
more cleanups
brekk Dec 6, 2021
c6cf8fa
standardize more
brekk Dec 6, 2021
c05c733
actually add constants
brekk Dec 6, 2021
603cdf6
more live, more test, more go
brekk Dec 6, 2021
bbf39fb
try swapping usage to useRouter hook
brekk Dec 6, 2021
3526977
back to basics
brekk Dec 6, 2021
79e3527
one more
brekk Dec 6, 2021
7ecbe28
cleanups
brekk Dec 6, 2021
fe87fb5
cheat the state
brekk Dec 6, 2021
4fe5335
quicktest
brekk Dec 6, 2021
a7c815c
logic
brekk Dec 6, 2021
3ba8628
do it properly
brekk Dec 7, 2021
f01446e
ensure data flows from logout
brekk Dec 7, 2021
853f834
try this again
brekk Dec 7, 2021
a25b8f9
logouttery
brekk Dec 7, 2021
9638564
bill o'reilly
brekk Dec 7, 2021
fe24ff5
B-A-N-A-N-A-S
brekk Dec 7, 2021
0954903
tired of debugging someone else's fucked up product
brekk Dec 7, 2021
9b9ca37
clusterfuckery
brekk Dec 7, 2021
0cef9b9
SNATS -> Situation Normal All TS
brekk Dec 7, 2021
379cb60
poison pill
brekk Dec 7, 2021
2c4a42b
again again
brekk Dec 7, 2021
44f23e3
address some feedback
brekk Dec 9, 2021
75f105f
more feedback address-al
brekk Dec 9, 2021
d369a1c
more feedback address-al
brekk Dec 9, 2021
653d0c8
more feedback addressed
brekk Dec 9, 2021
d6df6cd
more mor mo m
brekk Dec 9, 2021
2fb00af
addressing feedback broke something
brekk Dec 9, 2021
39bdc2a
suture
brekk Dec 9, 2021
3d0ad4d
test this first
brekk Dec 9, 2021
f710e1c
settting status caused a unloadable page, try this
brekk Dec 9, 2021
d7d82db
back in time
brekk Dec 9, 2021
86f1e1f
back in time again
brekk Dec 9, 2021
3513ef4
logout cleanup then test
brekk Dec 9, 2021
8b784ab
try using Router again
brekk Dec 9, 2021
329e608
eschew more
brekk Dec 9, 2021
c704074
piecemeal
brekk Dec 9, 2021
596ceae
try removing from useLogin
brekk Dec 9, 2021
f4dfa39
Keep using useRouter in login
brekk Dec 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions apiClient/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
LoginEvent,
} from './types'
import { LocalError } from './errors'
import {
UNABLE_TO_LOGIN,
ENDPOINT_UNAVAILABLE,
NOT_ISOMORPHIC,
} from 'constants/errors'

// Environment variables set in Vercel config.
const SERVER_API_URL = process.env.API_URL
Expand Down Expand Up @@ -142,7 +147,7 @@ export async function getMetricsConfig(): Promise<
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export async function login(email: string): Promise<any> {
if (typeof window === 'undefined' || !magic) {
return new LocalError('Only runnable in the browser', 500)
return new LocalError('Only runnable in the browser', NOT_ISOMORPHIC)
}
try {
await magic.auth.loginWithMagicLink({
Expand All @@ -161,7 +166,7 @@ export async function login(email: string): Promise<any> {
return { statusCode: 200, loaded: true }
}
} catch (e) {
return new LocalError(e.message, 500)
return new LocalError(e.message, UNABLE_TO_LOGIN)
}
}

Expand All @@ -177,13 +182,13 @@ export async function getUserDetails(
})
return data.json()
} catch (e) {
return new LocalError(e.message, 500)
return new LocalError(e.message, ENDPOINT_UNAVAILABLE)
}
}

export async function tokenLogin(): Promise<LoginEvent | LocalError> {
if (typeof window === 'undefined' || !magic) {
return new LocalError('Only runnable in the browser', 500)
return new LocalError('Only runnable in the browser', NOT_ISOMORPHIC)
}
try {
const token = await magic.auth.loginWithCredential()
Expand All @@ -195,6 +200,6 @@ export async function tokenLogin(): Promise<LoginEvent | LocalError> {
})
return res.json()
} catch (e) {
return new LocalError(e.message, 500)
return new LocalError(e.message, ENDPOINT_UNAVAILABLE)
}
}
37 changes: 19 additions & 18 deletions components/FooterCTA/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from 'clsx'
import Router from 'next/router'
import Link from 'next/link'
import Intertubes from 'components/About/ImageIntertubes'
import { RawButton } from 'components/Button'

Expand All @@ -21,23 +21,24 @@ export function TubesCTA({ cta, goTo, buttonText }: TubesCTAProps) {
<br />
{cta}
</h3>
<RawButton
onClick={() => Router.push(goTo)}
className={clsx(
'm-auto',
'mt-8',
'mb-16',
'max-w-[240px]',
'text-lg',
'px-4',
'py-3',
'md:text-xl',
'md:py-5',
'md:px-4'
)}
>
{buttonText}
</RawButton>
<Link passHref href={goTo}>
<RawButton
className={clsx(
'm-auto',
'mt-8',
'mb-16',
'max-w-[240px]',
'text-lg',
'px-4',
'py-3',
'md:text-xl',
'md:py-5',
'md:px-4'
)}
>
{buttonText}
</RawButton>
</Link>
</div>
</>
)
Expand Down
4 changes: 4 additions & 0 deletions components/Navbar/Flyout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Logo from 'components/Logo'
import Close from 'components/icons/Close'
import { LoginContext } from 'hooks/useLogin'
import NavbarLinks from './Links'

export type NavbarFlyoutProps = {
Expand All @@ -9,6 +10,7 @@ export type NavbarFlyoutProps = {
testnetClicked: () => unknown
companyVisible: boolean
testnetVisible: boolean
loginContext: LoginContext
}

export function NavbarFlyout({
Expand All @@ -18,12 +20,14 @@ export function NavbarFlyout({
testnetClicked,
companyVisible,
testnetVisible,
loginContext,
}: NavbarFlyoutProps) {
const navbarLinksProps = {
companyClicked,
testnetClicked,
companyVisible,
testnetVisible,
loginContext,
}
return (
<div
Expand Down
6 changes: 5 additions & 1 deletion components/Navbar/Links.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Link from 'next/link'
import clsx from 'clsx'

import { LoginContext } from 'hooks/useLogin'

import SubnavButton from './SubnavButton'
import LoginButton from './LoginButton'
import Company from './Company'
Expand All @@ -16,6 +18,7 @@ type NavbarLinksProps = {
testnetClicked?: () => unknown
testnetHovered?: () => unknown
testnetVisible?: boolean
loginContext: LoginContext
}

export function NavbarLinks({
Expand All @@ -28,6 +31,7 @@ export function NavbarLinks({
testnetClicked,
testnetHovered,
testnetVisible = false,
loginContext,
}: NavbarLinksProps) {
const itemPadding = [`px-2`, `lg:px-3.5`, `3xl:px-5`]
const cc = clsx([className, ...itemPadding])
Expand Down Expand Up @@ -60,7 +64,7 @@ export function NavbarLinks({
>
{testnetVisible && <Testnet condensed={condensed} />}
</SubnavButton>
<LoginButton />
<LoginButton loginContext={loginContext} />
</>
)
}
Expand Down
81 changes: 39 additions & 42 deletions components/Navbar/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, ReactNode, MouseEventHandler } from 'react'
import Link from 'next/link'
import clsx from 'clsx'
import { LoginContext } from 'contexts/LoginContext'
import { LoginContext } from 'hooks/useLogin'

import ChevronDown from './ChevronDown'
import { ApiUserMetadata } from 'apiClient/types'
Expand Down Expand Up @@ -142,52 +142,49 @@ const UserButton = ({ id, graffiti, visible }: ApiUserMetadataUI) => (
</Link>
</div>
)
export const LoginButton = () => {
type LoginProps = {
loginContext: LoginContext
}
export const LoginButton = ({ loginContext }: LoginProps) => {
const [$visible, $setVisible] = useState<boolean>(false)
const set = (x: boolean) => () => $setVisible(x)
const on = set(true)
const off = set(false)
const { checkLoggedIn, metadata } = loginContext
const isLoggedIn = checkLoggedIn()
// eslint-disable-next-line
const meta = metadata as any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this was just copied from unwrapping, so it doesn't necessarily have to be fixed here, but this any cast should be deleted by expanding the hasGraffiti conditional down where it's used.

I don't think we actually want the hasGraffiti check here -- if someone manages to log in but has an empty string graffiti for some reason, they'll see the Login button, but when they click it, they'll be redirected to leaderboard since already logged in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is because <UserButton />'s type as defined is:

interface ApiUserMetadataUI extends ApiUserMetadata {
  visible: boolean
}

But TypeScript yells about id:

Type error: Type '{ visible: boolean; id?: number | undefined; created_at?: string | undefined; updated_at?: string | undefined; email?: string | undefined; graffiti?: string | undefined; total_points?: number | undefined; ... 6 more ...; message?: string | undefined; }' is not assignable to type 'ApiUserMetadataUI'.
  Types of property 'id' are incompatible.
    Type 'number | undefined' is not assignable to type 'number'.
      Type 'undefined' is not assignable to type 'number'.

More than happy to change if you know a good approach; but I didn't wanna change ApiUserMetadata and I figured extending it would be correct?

return (
<LoginContext.Consumer>
{({ checkLoggedIn, metadata }) => {
const isLoggedIn = checkLoggedIn()
// eslint-disable-next-line
const meta = metadata as any
const hasGraffiti = metadata && !!metadata.graffiti
return (
<HoverButton
className={clsx(
'bg-transparent',
'h-16',
'hover:bg-black',
'hover:text-white',
'px-6',
'py-3',
'relative',
'text-2xl',
'text-black',
'text-center',
'md:min-w-[8rem]',
'md:h-12',
'md:ml-4',
'md:px-3',
'md:text-sm',
'lg:min-w-[10rem]',
'lg:px-6',
'lg:text-base'
)}
onMouseOver={on}
onMouseOut={off}
>
{isLoggedIn && hasGraffiti ? (
<UserButton {...meta} visible={$visible} />
) : (
<StaticButton href="/login" />
)}
</HoverButton>
)
}}
</LoginContext.Consumer>
<HoverButton
className={clsx(
'bg-transparent',
'h-16',
'hover:bg-black',
'hover:text-white',
'px-6',
'py-3',
'relative',
'text-2xl',
'text-black',
'text-center',
'md:min-w-[8rem]',
'md:h-12',
'md:ml-4',
'md:px-3',
'md:text-sm',
'lg:min-w-[10rem]',
'lg:px-6',
'lg:text-base'
)}
onMouseOver={on}
onMouseOut={off}
>
{isLoggedIn ? (
<UserButton {...meta} visible={$visible} />
) : (
<StaticButton href="/login" />
)}
</HoverButton>
)
}
export default LoginButton
4 changes: 4 additions & 0 deletions components/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clsx from 'clsx'
import Logo from 'components/Logo'
import Menu from 'components/icons/Menu'
import { useNav, NavState } from 'hooks/useNav'
import { LoginContext } from 'hooks/useLogin'

import NavbarLinks from './Links'
import NavbarFlyout from './Flyout'
Expand All @@ -13,11 +14,13 @@ import Link from 'next/link'
type NavbarProps = {
className?: string
fill?: string
loginContext: LoginContext
}

function Navbar({
fill = 'white',
className = 'bg-black text-white',
loginContext,
}: NavbarProps) {
const {
$flyoutVisible,
Expand All @@ -40,6 +43,7 @@ function Navbar({
testnetClicked: toggleNavTestnet,
testnetHovered: enterNavTestnet,
testnetVisible,
loginContext,
}
return (
<nav
Expand Down
23 changes: 5 additions & 18 deletions components/ResponsiveToolkit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
import { ApiUserMetadata } from 'apiClient/types'
import clsx from 'clsx'
import useQuery from 'hooks/useQuery'
import { useLogin } from 'hooks/useLogin'
import styles from './ResponsiveToolkit.module.css'
import pkg from 'package.json'

Expand Down Expand Up @@ -45,15 +45,14 @@ const points = [
'90%',
]

type ResponsiveToolkitProps = {
userDetails: ReturnType<typeof useLogin>
type ToolkitProps = {
metadata: ApiUserMetadata | null
}

const ResponsiveToolkit = ({ userDetails }: ResponsiveToolkitProps) => {
const ResponsiveToolkit = ({ metadata }: ToolkitProps) => {
const [$active, $setActive] = useState(true)
const [$width, $setWidth] = useState(-1)
const [$point, $setPoint] = useState(0)
const [$graffiti, $setGraffiti] = useState('👻')
const toggle = () => $setActive(!$active)
const $toolkit = useQuery('debug')
const $customPoint = useQuery('point')
Expand All @@ -66,8 +65,6 @@ const ResponsiveToolkit = ({ userDetails }: ResponsiveToolkitProps) => {
const update = () => {
$setWidth(window.innerWidth)
const newPoints = activePoints()
// eslint-disable-next-line no-console
console.log({ points: newPoints, $customPoint })
$setPoint(newPoints)
}
if ($toolkit) {
Expand All @@ -81,16 +78,6 @@ const ResponsiveToolkit = ({ userDetails }: ResponsiveToolkitProps) => {
const horizontal = pointsPlus.filter(z => !z.startsWith('v'))
const vertical = pointsPlus.filter(z => z.startsWith('v'))

useEffect(() => {
if (userDetails && userDetails.metadata) {
// eslint-disable-next-line
const given = userDetails!.metadata.graffiti
if ($graffiti !== given) {
$setGraffiti(given)
}
}
}, [userDetails, $graffiti])

return $toolkit ? (
<>
<div className={styles.toolkit} onClick={toggle}>
Expand All @@ -99,7 +86,7 @@ const ResponsiveToolkit = ({ userDetails }: ResponsiveToolkitProps) => {
<div className={styles.debugMode}>
{pkg.name}@{pkg.version}
</div>
<div className={styles.contextual}>{$graffiti}</div>
<div className={styles.contextual}>{metadata?.graffiti || '👻'}</div>
{$active && horizontal.map(x => <Breakpoint key={x} at={x} />)}
{$active &&
vertical.map(x => <Breakpoint key={x} at={x} horizontal={false} />)}
Expand Down
18 changes: 18 additions & 0 deletions constants/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const NO_MAGIC_INSTANCE = 1000
export const NO_MAGIC_TOKEN = 1001
export const NO_MAGIC_USER = 1002

export const NOT_ISOMORPHIC = 2000

export const UNABLE_TO_LOGIN = 3000

export const ENDPOINT_UNAVAILABLE = 4000

export const ERRORS = {
NO_MAGIC_INSTANCE,
NO_MAGIC_TOKEN,
NO_MAGIC_USER,
NOT_ISOMORPHIC,
UNABLE_TO_LOGIN,
ENDPOINT_UNAVAILABLE,
}
Loading