1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-22 09:13:19 +01:00

header refactor

This commit is contained in:
Matthias Kretschmann 2024-02-04 00:04:01 +00:00
parent 1d74f420be
commit 4d7d7e629d
Signed by: m
GPG Key ID: 606EEEF3C479A91F
14 changed files with 61 additions and 96 deletions

View File

@ -1,6 +1,7 @@
import { Metadata, ResolvingMetadata } from 'next' import { Metadata, ResolvingMetadata } from 'next'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
import meta from '../../../_content/meta.json' import meta from '../../../_content/meta.json'
import Header from '../../components/Header'
import Project from '../../components/Project' import Project from '../../components/Project'
import ProjectNav from '../../components/ProjectNav' import ProjectNav from '../../components/ProjectNav'
import { import {
@ -44,6 +45,7 @@ export default async function ProjectPage({ params }: Props) {
return ( return (
<> <>
<Header isSmall />
<Project project={project} /> <Project project={project} />
<ProjectNav projects={projects} currentSlug={params.slug} /> <ProjectNav projects={projects} currentSlug={params.slug} />
</> </>

View File

@ -19,6 +19,6 @@ describe('app: /layout', () => {
it('renders correctly', async () => { it('renders correctly', async () => {
render(<Layout>Hello</Layout>) render(<Layout>Hello</Layout>)
await screen.findByText(dataLocation.now.city) await screen.findByText('Hello')
}) })
}) })

View File

@ -5,7 +5,9 @@ import { GiphyFetch } from '@giphy/js-fetch-api'
export async function getLocation() { export async function getLocation() {
try { try {
const response = await fetch('https://location.kretschmann.io') const response = await fetch('https://location.kretschmann.io', {
cache: 'no-store'
})
if (!response.ok) if (!response.ok)
throw new Error('Network response for location was not ok.') throw new Error('Network response for location was not ok.')

View File

@ -3,7 +3,6 @@ import { Metadata, Viewport } from 'next'
import Script from 'next/script' import Script from 'next/script'
import meta from '../../_content/meta.json' import meta from '../../_content/meta.json'
import Footer from '../components/Footer' import Footer from '../components/Footer'
import Header from '../components/Header'
import HostnameCheck from '../components/HostnameCheck' import HostnameCheck from '../components/HostnameCheck'
import ThemeSwitch from '../components/ThemeSwitch' import ThemeSwitch from '../components/ThemeSwitch'
import { UMAMI_SCRIPT_URL, UMAMI_WEBSITE_ID } from '../lib/umami' import { UMAMI_SCRIPT_URL, UMAMI_WEBSITE_ID } from '../lib/umami'
@ -66,7 +65,6 @@ export default function RootLayout({ children }: { children: ReactNode }) {
<HostnameCheck allowedHosts={meta.allowedHosts} /> <HostnameCheck allowedHosts={meta.allowedHosts} />
<ThemeSwitch /> <ThemeSwitch />
<Header />
<main className={styles.screen}>{children}</main> <main className={styles.screen}>{children}</main>
<Footer /> <Footer />
</Providers> </Providers>

View File

@ -1,14 +1,18 @@
import Header from '../components/Header'
import Projects from '../components/Projects' import Projects from '../components/Projects'
import Repositories from '../components/Repositories' import Repositories from '../components/Repositories'
import { getAllProjects } from '../lib/content' import { getAllProjects } from '../lib/content'
import { getGithubRepos } from '../lib/github' import { getGithubRepos } from '../lib/github'
import { getLocation } from './actions'
export default async function IndexPage() { export default async function IndexPage() {
const projects = await getAllProjects(['title', 'images', 'slug']) const projects = await getAllProjects(['title', 'images', 'slug'])
const repos = await getGithubRepos() const repos = await getGithubRepos()
const location = await getLocation()
return ( return (
<> <>
<Header location={location} />
<Projects projects={projects} /> <Projects projects={projects} />
<Repositories repos={repos} /> <Repositories repos={repos} />
</> </>

View File

@ -1,12 +1,7 @@
'use client'
import { LazyMotion, domAnimation, m, useReducedMotion } from 'framer-motion'
import meta from '../../../_content/meta.json' import meta from '../../../_content/meta.json'
import { getAnimationProps, moveInBottom } from '../Transitions'
import styles from './index.module.css' import styles from './index.module.css'
export default function Availability() { export default function Availability() {
const shouldReduceMotion = useReducedMotion()
const { status, available, unavailable } = meta.availability const { status, available, unavailable } = meta.availability
const className = status const className = status
? `${styles.availability} ${styles.available}` ? `${styles.availability} ${styles.available}`
@ -14,14 +9,8 @@ export default function Availability() {
const html = status ? available : unavailable const html = status ? available : unavailable
return ( return (
<LazyMotion features={domAnimation}> <section className={className}>
<m.section <p dangerouslySetInnerHTML={{ __html: html }} />
variants={moveInBottom} </section>
className={className}
{...getAnimationProps(shouldReduceMotion)}
>
<p dangerouslySetInnerHTML={{ __html: html }} />
</m.section>
</LazyMotion>
) )
} }

View File

@ -2,6 +2,7 @@
position: relative; position: relative;
padding: calc(var(--spacer) / 2); padding: calc(var(--spacer) / 2);
padding-top: 25vh; padding-top: 25vh;
margin-bottom: calc(var(--spacer) * 2);
min-height: calc(85vh - var(--spacer)); min-height: calc(85vh - var(--spacer));
max-height: 1000px; max-height: 1000px;
text-align: center; text-align: center;

View File

@ -1,20 +1,17 @@
import { render, screen } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Header from '.' import Header from '.'
import { dataLocation } from '../../../tests/__fixtures__/location'
describe('Header', () => { describe('Header', () => {
it('renders correctly', async () => { it('renders correctly', async () => {
render(<Header />) render(<Header location={dataLocation} />)
await screen.findByText('matthias kretschmann') await screen.findByText('matthias kretschmann')
await screen.findAllByText('Lisbon') await screen.findAllByText(dataLocation.now.city)
}) })
it('renders small', async () => { it('renders small', async () => {
jest.mock('next/navigation', () => ({ render(<Header isSmall />)
usePathname: jest.fn().mockImplementation(() => '/something')
}))
render(<Header />)
expect(await screen.findByTestId('header')).toHaveClass('small') expect(await screen.findByTestId('header')).toHaveClass('small')
}) })

View File

@ -1,16 +1,17 @@
'use client'
import { usePathname } from 'next/navigation'
import Availability from '../Availability' import Availability from '../Availability'
import Location from '../Location' import Location from '../Location'
import { UseLocation } from '../Location/types'
import LogoUnit from '../LogoUnit' import LogoUnit from '../LogoUnit'
import Networks from '../Networks' import Networks from '../Networks'
import styles from './index.module.css' import styles from './index.module.css'
export default function Header() { export default function Header({
const pathname = usePathname() location,
const isSmall = pathname !== '/' isSmall
}: {
location?: UseLocation
isSmall?: boolean
}) {
return ( return (
<header <header
className={`${styles.header} ${isSmall ? styles.small : ''}`} className={`${styles.header} ${isSmall ? styles.small : ''}`}
@ -19,7 +20,7 @@ export default function Header() {
<LogoUnit small={isSmall} /> <LogoUnit small={isSmall} />
{!isSmall ? <Networks label="Networks" /> : null} {!isSmall ? <Networks label="Networks" /> : null}
<div className={styles.meta}> <div className={styles.meta}>
{!isSmall ? <Location /> : null} {location ? <Location location={location} /> : null}
{!isSmall ? <Availability /> : null} {!isSmall ? <Availability /> : null}
</div> </div>
</header> </header>

View File

@ -1,67 +1,42 @@
'use client'
import { useEffect, useState } from 'react'
import RelativeTime from '@yaireo/relative-time' import RelativeTime from '@yaireo/relative-time'
import { LazyMotion, domAnimation, m, useReducedMotion } from 'framer-motion'
import { getLocation } from '../../app/actions'
import { getAnimationProps, moveInTop } from '../Transitions'
import { Flag } from './Flag' import { Flag } from './Flag'
import styles from './index.module.css' import styles from './index.module.css'
import { UseLocation } from './types' import { UseLocation } from './types'
export default function Location() { export default function Location({ location }: { location: UseLocation }) {
const shouldReduceMotion = useReducedMotion()
const [location, setLocation] = useState<UseLocation>()
const isDifferentCountry = location?.now?.country !== location?.next?.country const isDifferentCountry = location?.now?.country !== location?.next?.country
const relativeTime = new RelativeTime({ locale: 'en' }) const relativeTime = new RelativeTime({ locale: 'en' })
useEffect(() => {
async function fetchData() {
const location = await getLocation()
if (!location) return
setLocation(location)
}
fetchData()
}, [])
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
{location?.now?.city ? ( {location?.now?.city ? (
<LazyMotion features={domAnimation}> <section aria-label="Location" className={styles.location}>
<m.section <Flag
aria-label="Location" country={{
variants={moveInTop} code: location.now.country_code,
className={styles.location} name: location.now.country
{...getAnimationProps(shouldReduceMotion)} }}
> />
<Flag {location.now.city} <span>Now</span>
country={{ <div className={styles.next}>
code: location.now.country_code, {location?.next?.city && (
name: location.now.country <>
}} {isDifferentCountry && (
/> <Flag
{location.now.city} <span>Now</span> country={{
<div className={styles.next}> code: location.next.country_code,
{location?.next?.city && ( name: location.next.country
<> }}
{isDifferentCountry && ( />
<Flag )}
country={{ {location.next.city}{' '}
code: location.next.country_code, <span>
name: location.next.country {relativeTime.from(new Date(location.next.date_start))}
}} </span>
/> </>
)} )}
{location.next.city}{' '} </div>
<span> </section>
{relativeTime.from(new Date(location.next.date_start))}
</span>
</>
)}
</div>
</m.section>
</LazyMotion>
) : null} ) : null}
</div> </div>
) )

View File

@ -9,5 +9,4 @@ export type Location = {
export type UseLocation = { export type UseLocation = {
now: Location now: Location
next: Location next: Location
previous: Location
} }

View File

@ -1,5 +1,5 @@
.screen { .screen {
margin: calc(var(--spacer) * 2) auto 0 auto; margin: 0 auto;
padding-left: 5vw; padding-left: 5vw;
padding-right: 5vw; padding-right: 5vw;
} }

View File

@ -3,11 +3,14 @@ export const dataLocation = {
city: 'Lisbon', city: 'Lisbon',
country: 'Portugal', country: 'Portugal',
country_code: 'PT', country_code: 'PT',
date_start: '2021-10-01' date_start: '2021-10-01',
date_end: '2021-11-01'
}, },
next: { next: {
city: 'Barcelona', city: 'Barcelona',
country: 'Spain', country: 'Spain',
date_start: '2021-10-04' country_code: 'ES',
date_start: '2021-12-04',
date_end: '2021-12-09'
} }
} }

View File

@ -16,12 +16,6 @@ jest.mock('../src/app/actions', () => ({
.mockImplementation(() => giphy.data.images.original.mp4) .mockImplementation(() => giphy.data.images.original.mp4)
})) }))
// jest.mock('@giphy/js-fetch-api', () => ({
// GiphyFetch: jest.fn().mockImplementation(() => ({
// random: jest.fn().mockImplementation(() => Promise.resolve(giphy))
// }))
// }))
const unmockedFetch = global.fetch const unmockedFetch = global.fetch
const unmockedEnv = process.env const unmockedEnv = process.env