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"; import { createDefinedContext } from "../../utils/definedContext";
/** /**
* The string to be used as the `src` of the postcard. * 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 = { export type PostcardContextContents = {
visibility: PostcardVisibility, visibility: PostcardVisibility,
previousSrc: PostcardSource, previousSrc: string,
currentSrc: PostcardSource, currentSrc: string,
changePostcard: (src: PostcardSource) => void, changePostcard: (src: PostcardSource) => void,
resetPostcard: () => void, resetPostcard: () => void,
changeVisibility: (visibility: PostcardVisibility) => void, changeVisibility: (visibility: PostcardVisibility) => void,

View file

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

View file

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

View file

@ -26,6 +26,9 @@ type UsePostcardStorageState = {
previousSrc: PostcardSource, previousSrc: PostcardSource,
} }
/**
* Reducer for {@link usePostcardStorage}.
*/
function reducerUsePostcardStorage(prev: UsePostcardStorageState, action: UsePostcardStorageAction): UsePostcardStorageState { function reducerUsePostcardStorage(prev: UsePostcardStorageState, action: UsePostcardStorageAction): UsePostcardStorageState {
switch (action.type) { switch (action.type) {
case "change": 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}. * Hook holding as state the {@link PostcardContextContents}.
*/ */
@ -75,8 +90,8 @@ export function usePostcardStorage(defaultPostcard: PostcardSource): PostcardCon
) )
return { return {
previousSrc, previousSrc: getProperSrc(previousSrc),
currentSrc, currentSrc: getProperSrc(currentSrc),
changePostcard, changePostcard,
resetPostcard, resetPostcard,
changeVisibility, changeVisibility,

View file

@ -25,6 +25,6 @@
"eventError": "Si è verificato il seguente errore nel recupero dei dettagli dell'evento:", "eventError": "Si è verificato il seguente errore nel recupero dei dettagli dell'evento:",
"debugLoading": "Questo è un esempio di caricamento full-page.", "debugLoading": "Questo è un esempio di caricamento full-page.",
"debugError": "Questo è un esempio di errore 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." "debugPostcardButton": "Hmmm."
} }