1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-10-16 06:57:26 +00:00

Fix some postcard-related things

This commit is contained in:
Steffo 2022-07-21 00:30:09 +02:00
parent 952aefb839
commit 9ed469cd36
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 25 additions and 16 deletions

View file

@ -1,11 +1,11 @@
import { ComponentPropsWithoutRef } from "react";
import { StaticImageData } from "next/image";
import { createDefinedContext } from "../../utils/definedContext";
/**
* The string to be used as the `src` of the postcard.
*/
export type PostcardSource = string
export type PostcardSource = string | StaticImageData
/**
@ -29,8 +29,8 @@ export enum PostcardVisibility {
*/
export type PostcardContextContents = {
visibility: PostcardVisibility,
previousSrc: PostcardSource,
currentSrc: PostcardSource,
previousSrc: string,
currentSrc: string,
changePostcard: (src: PostcardSource) => void,
resetPostcard: () => void,
changeVisibility: (visibility: PostcardVisibility) => void,

View file

@ -12,12 +12,7 @@ export function usePostcardImage(src: PostcardSource | StaticImageData) {
useEffect(
() => {
if (typeof src === "string") {
changePostcard(src)
}
else {
changePostcard(src.src)
}
},
[src, changePostcard]
)

View file

@ -1,9 +1,8 @@
import { default as classNames } from "classnames"
import style from "./renderer.module.css"
import { useDefinedContext } from "../../utils/definedContext"
import { PostcardContext, PostcardVisibility } from "./base"
import { LegacyRef, useEffect, useRef, useState } from "react"
import { asleep } from "../../utils/asleep"
import { PostcardContext, PostcardSource, PostcardVisibility } from "./base"
import { LegacyRef, useEffect, useRef } from "react"
export function PostcardRenderer() {

View file

@ -26,6 +26,9 @@ type UsePostcardStorageState = {
previousSrc: PostcardSource,
}
/**
* Reducer for {@link usePostcardStorage}.
*/
function reducerUsePostcardStorage(prev: UsePostcardStorageState, action: UsePostcardStorageAction): UsePostcardStorageState {
switch (action.type) {
case "change":
@ -40,6 +43,18 @@ function reducerUsePostcardStorage(prev: UsePostcardStorageState, action: UsePos
}
}
/**
* Convert a {@link PostcardSource} to a string suitable for use in `<img>` tags.
*/
function getProperSrc(obj: PostcardSource): string {
if (typeof obj === "string") {
return obj
}
else {
return obj.src
}
}
/**
* Hook holding as state the {@link PostcardContextContents}.
*/
@ -75,8 +90,8 @@ export function usePostcardStorage(defaultPostcard: PostcardSource): PostcardCon
)
return {
previousSrc,
currentSrc,
previousSrc: getProperSrc(previousSrc),
currentSrc: getProperSrc(currentSrc),
changePostcard,
resetPostcard,
changeVisibility,

View file

@ -25,6 +25,6 @@
"eventError": "Si è verificato il seguente errore nel recupero dei dettagli dell'evento:",
"debugLoading": "Questo è un esempio di caricamento full-page.",
"debugError": "Questo è un esempio di errore full-page.",
"debugPostcardText": "Questo è un esempio di transizione nello sfondo.",
"debugPostcardText": "Questo è un esempio di transizione dello sfondo.",
"debugPostcardButton": "Hmmm."
}