You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nx-guitar-school/apps/website/libs/utils.ts

51 lines
2.5 KiB

import delve from 'dlv';
import {ImageLoader, ImageLoaderProps} from "next/image";
import {environment} from "../environments/environment";
export type ImageFormatType = "default" | "thumbnail" | "medium" | "small";
export const contentfulImageLoader: ImageLoader = ({ src, width }: ImageLoaderProps) => {
return `${src}?w=${width}`
}
export const getCategoryUrl = (item): string => {
const categorySlug = !delve(item, 'attributes', null) ? delve(item, 'category.data.attributes.slug', '') : delve(item, 'attributes.category.data.attributes.slug', '');
return '/blog/' + categorySlug;
}
export const getPostUrl = (item): string => {
const categorySlug = !delve(item, 'attributes', null) ? delve(item, 'category.data.attributes.slug', '') : delve(item, 'attributes.category.data.attributes.slug', '');
const postSlug = !delve(item, 'attributes', null) ? delve(item, 'slug', '') : delve(item, 'attributes.slug', '');
return '/blog/' + categorySlug + '/' + postSlug;
}
export const getStrapiImage = (item, format: ImageFormatType = 'default') => {
const image = !delve(item, 'attributes', null) ? delve(item, 'image.data.attributes', {}) : delve(item, 'attributes.image.data.attributes', {});
switch (format) {
case "default":
return environment.strapiUrl + delve(image, "url", "/images/default.png");
case "medium":
return environment.strapiUrl + delve(image, "formats.medium.url", "/images/default.png");
case "small":
return environment.strapiUrl + delve(image, "formats.small.url", "/images/default.png");
case "thumbnail":
return environment.strapiUrl + delve(image, "formats.thumbnail.url", "/images/default.png");
default:
return environment.strapiUrl + delve(image, "url", "/images/default.png");
}
}
export const getStrapiImageSize = (item, format: ImageFormatType = 'default'): [number, number] => {
const image = delve(item, 'attributes.image.data.attributes', {});
switch (format) {
case "default":
return [delve(image, "width", 100), delve(image, "height", 100)];
case "medium":
return [delve(image, "formats.medium.width", 100), delve(image, "formats.medium.height", 100)];
case "small":
return [delve(image, "formats.small.width", 100), delve(image, "formats.small.height", 100)];
case "thumbnail":
return [delve(image, "formats.thumbnail.width", 100), delve(image, "formats.thumbnail.height", 100)];
default:
return [delve(image, "width", 100), delve(image, "height", 100)];
}
}