diff --git a/pages/collective.tsx b/pages/collective.tsx index 9dec53c3..0b7c8346 100644 --- a/pages/collective.tsx +++ b/pages/collective.tsx @@ -1,14 +1,15 @@ import Head from 'next/head' import { App } from 'src/components/App' import { Meta } from 'src/components/Meta' -import { CollectiveImage } from 'src/components/CollectiveImage' import { meta } from 'src/lib/meta' -import { IMeta, IPage, ImageDefinition } from 'src/types' +import { IMeta, IPage, ImageSize } from 'src/types' import { createApiUrl, requestGet } from 'src/next-lib' import { NextPage, NextPageContext } from 'next' import { CardGrid } from 'src/components/CardGrid' import { Card } from 'src/components/Card' import { Block } from 'src/components/Block' +import { Image } from 'src/components/Image' +import { imageSrcSet } from 'src/lib/image' const persons = [ '/teacher/mg-timasheva', @@ -38,7 +39,6 @@ type CardData = { type Props = { title: string - image: string meta: IMeta data: CardData[] } @@ -49,6 +49,9 @@ const Page: NextPage = props => { return } + const src = 'https://art.shlisselburg.org/static/img/collective.jpg' + const srcSet = imageSrcSet(src, [ImageSize.big, ImageSize.large, ImageSize.medium, ImageSize.small]) + return ( = props => { - {!props.image ? null : ( - - )} + = props => { export const unstable_getStaticProps = async (ctx: NextPageContext) => { const title = 'Преподаватели Шлиссельбургской ДХШ' - const imageFile = 'Images/HudozkaCollective2017.jpg' - const resImage = await requestGet(createApiUrl(`/api/image?file=${imageFile}`), null) - const image = resImage?.artifacts.large - const pages = await Promise.all(persons .map(page => requestGet(createApiUrl(`/api/page?page=${page}`), null)) ) @@ -116,16 +118,15 @@ export const unstable_getStaticProps = async (ctx: NextPageContext) => { title: x.title, caption: x.description, href: x.url, - imageSrc: image.src, imageAlt: title, - imageSrcSet: image.set.join(' ') + imageSrc: image.src, + imageSrcSet: imageSrcSet(image.src, [ImageSize.medium, ImageSize.small]) } }) return { props: { data, - image, title, meta: meta({ title, diff --git a/src/components/CollectiveImage.jsx b/src/components/CollectiveImage.jsx deleted file mode 100644 index c6668b58..00000000 --- a/src/components/CollectiveImage.jsx +++ /dev/null @@ -1,22 +0,0 @@ -import React from 'react' -import { Image } from './Image' - -export const CollectiveImage = (props) => ( -
- {props.data.alt} -
-) diff --git a/src/components/Image/index.tsx b/src/components/Image/index.tsx index 68d75eea..267c01f9 100644 --- a/src/components/Image/index.tsx +++ b/src/components/Image/index.tsx @@ -1,40 +1,23 @@ -import * as React from 'react' -import cx from 'classnames' +import './styles.css' +import { memo } from 'react' -export interface IImageProps { +export type ImageProps = { style?: React.CSSProperties imgStyle?: React.CSSProperties src: string - alt: string - opa: boolean - set: Array<{ - url: string - density: number - }> + alt?: string + srcSet?: string } -export const Image: React.FC = React.memo(props => { - const srcSet = props.set - .map(({ url, density }) => `${url} ${density}x`) - .join(' ') - +export const Image: React.FC = memo(props => { return ( - - - + {props.alt} - + ) }) diff --git a/src/components/Image/styles.css b/src/components/Image/styles.css new file mode 100644 index 00000000..5feaa95f --- /dev/null +++ b/src/components/Image/styles.css @@ -0,0 +1,7 @@ +.image { + display: block; +} + +.image img { + display: block; +} diff --git a/src/lib/image.ts b/src/lib/image.ts new file mode 100644 index 00000000..b838226b --- /dev/null +++ b/src/lib/image.ts @@ -0,0 +1,25 @@ +import { ImageSize } from 'src/types' + +export type ResizeOptions = { + width: number + height: number +} + +export function resizeImage(src: string, options: ResizeOptions): string { + return `https://images.weserv.nl/?url=${src}&w=${options.width}&h=${options.height}&n=-1` +} + +export function imageSrcSet(src: string, sizes: ImageSize[]): string { + const set = sizes + .map(x => ({ + value: x, + href: resizeImage(src, { + width: x, + height: x, + }), + })) + + return set + .map(x => `${x.href} ${x.value}w`) + .join(', ') +} diff --git a/src/types.ts b/src/types.ts index 5304d8ba..cebc8677 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,6 +61,14 @@ export interface ITag { href: string } +export enum ImageSize { + big = 3000, + large = 1500, + medium = 1000, + small = 500, + thumbnail = 200, +} + export type ImageArtifact = { width: number height: number