mirror of
https://github.com/Steffo99/festa.git
synced 2025-01-02 20:14:20 +00:00
Fix some postcard-related things
This commit is contained in:
parent
952aefb839
commit
9ed469cd36
5 changed files with 25 additions and 16 deletions
|
@ -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,
|
||||||
|
|
|
@ -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]
|
||||||
)
|
)
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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."
|
||||||
}
|
}
|
Loading…
Reference in a new issue