Initial design based off original website, some things still to do
This commit is contained in:
101
src/lib/utils.ts
Normal file
101
src/lib/utils.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import type { ImageMetadata } from "astro";
|
||||
|
||||
import type { CollectionEntry } from "astro:content";
|
||||
type Project = CollectionEntry<"projects">;
|
||||
|
||||
const allProjectOtherImages = import.meta.glob<{ default: ImageMetadata }>(
|
||||
"/src/assets/img/projects/**/*",
|
||||
{ eager: true }
|
||||
);
|
||||
|
||||
const allProjectHeros = import.meta.glob<{ default: ImageMetadata }>(
|
||||
"/src/assets/img/project-heros/*",
|
||||
{ eager: true }
|
||||
);
|
||||
|
||||
export function getProjectHero(project: Project): ImageMetadata | undefined {
|
||||
for (const [path, mod] of Object.entries(allProjectHeros)) {
|
||||
if (path.includes(project.data.images.hero.src)) {
|
||||
return mod.default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getProjectOtherImages(project: Project): ImageMetadata[] {
|
||||
const images: ImageMetadata[] = [];
|
||||
|
||||
if (!project.data.images.other) {
|
||||
return images;
|
||||
}
|
||||
|
||||
for (const [path, mod] of Object.entries(allProjectOtherImages)) {
|
||||
if (path.includes(project.data.images.other)) {
|
||||
images.push(mod.default);
|
||||
}
|
||||
}
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
export function getAllProjectImages(project: Project): ImageMetadata[] {
|
||||
const images: ImageMetadata[] = [];
|
||||
|
||||
const heroImage = getProjectHero(project);
|
||||
|
||||
if (heroImage) {
|
||||
images.push(heroImage);
|
||||
}
|
||||
|
||||
if (!project.data.images.other) {
|
||||
return images;
|
||||
}
|
||||
const otherImages = getProjectOtherImages(project);
|
||||
|
||||
for (const otherImage of otherImages) {
|
||||
images.push(otherImage);
|
||||
}
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
export function shuffleArray<T>(array: Array<T>): Array<T> {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
export async function convertEagerImagesImportGlobToArray(
|
||||
images: Record<
|
||||
string,
|
||||
() => Promise<{
|
||||
default: ImageMetadata;
|
||||
eager: true;
|
||||
}>
|
||||
>
|
||||
) {
|
||||
return await Promise.all(
|
||||
Object.values(images).map(async (image) => (await image()).default)
|
||||
);
|
||||
}
|
||||
|
||||
export async function convertImagesImportGlobToArray(
|
||||
images: Record<
|
||||
string,
|
||||
() => Promise<{
|
||||
default: ImageMetadata;
|
||||
}>
|
||||
>
|
||||
) {
|
||||
const arrayOfPromises = await Promise.all(
|
||||
Object.values(images).map(async (image) => image())
|
||||
);
|
||||
|
||||
const returnedImages: ImageMetadata[] = [];
|
||||
arrayOfPromises.forEach((image) => {
|
||||
returnedImages.push(image.default);
|
||||
});
|
||||
|
||||
return returnedImages;
|
||||
}
|
Reference in New Issue
Block a user