From 983154487435d016ec62ec0b37e97d61c645713b Mon Sep 17 00:00:00 2001 From: Azat Ismagilov Date: Wed, 2 Aug 2023 19:51:27 +0200 Subject: [PATCH] Move PhotoInfo into it's own context and add edit button --- package-lock.json | 45 +++++++ package.json | 1 + src/App.jsx | 2 + src/Util/PhotoInfoHelper.js | 111 ++++++++++++++++++ src/Util/PhotoParser.js | 76 ------------ src/components/Body/Body.jsx | 3 +- src/components/Body/Lightbox.jsx | 29 ++--- src/components/Body/PhotoInfo.jsx | 53 --------- .../Body/PhotoInfo/PhotoInfoContext.jsx | 70 +++++++++++ .../Body/{ => PhotoInfo}/PhotoInfoDetails.jsx | 36 ++++-- .../Body/PhotoInfo/PhotoInfoPanel.jsx | 83 +++++++++++++ src/index.jsx | 5 +- 12 files changed, 354 insertions(+), 160 deletions(-) create mode 100644 src/Util/PhotoInfoHelper.js delete mode 100644 src/Util/PhotoParser.js delete mode 100644 src/components/Body/PhotoInfo.jsx create mode 100644 src/components/Body/PhotoInfo/PhotoInfoContext.jsx rename src/components/Body/{ => PhotoInfo}/PhotoInfoDetails.jsx (72%) create mode 100644 src/components/Body/PhotoInfo/PhotoInfoPanel.jsx diff --git a/package-lock.json b/package-lock.json index 314e723..e2f35f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@mui/material": "^5.11.0", "axios": "^0.27.2", "lightbox2": "^2.11.3", + "notistack": "^3.0.1", "react": "^18.0.2", "react-dom": "^18.0.2", "react-helmet": "^6.1.0", @@ -8968,6 +8969,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/goober": { + "version": "2.1.13", + "resolved": "https://registry.npmjs.org/goober/-/goober-2.1.13.tgz", + "integrity": "sha512-jFj3BQeleOoy7t93E9rZ2de+ScC4lQICLwiAQmKMg9F6roKGaLSHoCDYKkWlSafg138jejvq/mTdvmnwDQgqoQ==", + "peerDependencies": { + "csstype": "^3.0.10" + } + }, "node_modules/gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -12495,6 +12504,27 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/notistack": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/notistack/-/notistack-3.0.1.tgz", + "integrity": "sha512-ntVZXXgSQH5WYfyU+3HfcXuKaapzAJ8fBLQ/G618rn3yvSzEbnOB8ZSOwhX+dAORy/lw+GC2N061JA0+gYWTVA==", + "dependencies": { + "clsx": "^1.1.0", + "goober": "^2.0.33" + }, + "engines": { + "node": ">=12.0.0", + "npm": ">=6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/notistack" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0", + "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0" + } + }, "node_modules/npm-run-path": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-4.0.1.tgz", @@ -23914,6 +23944,12 @@ "slash": "^3.0.0" } }, + "goober": { + "version": "2.1.13", + "resolved": "https://registry.npmjs.org/goober/-/goober-2.1.13.tgz", + "integrity": "sha512-jFj3BQeleOoy7t93E9rZ2de+ScC4lQICLwiAQmKMg9F6roKGaLSHoCDYKkWlSafg138jejvq/mTdvmnwDQgqoQ==", + "requires": {} + }, "gopd": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", @@ -26470,6 +26506,15 @@ "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz", "integrity": "sha512-DlL+XwOy3NxAQ8xuC0okPgK46iuVNAK01YN7RueYBqqFeGsBjV9XmCAzAdgt+667bCl5kPh9EqKKDwnaPG1I7A==" }, + "notistack": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/notistack/-/notistack-3.0.1.tgz", + "integrity": "sha512-ntVZXXgSQH5WYfyU+3HfcXuKaapzAJ8fBLQ/G618rn3yvSzEbnOB8ZSOwhX+dAORy/lw+GC2N061JA0+gYWTVA==", + "requires": { + "clsx": "^1.1.0", + "goober": "^2.0.33" + } + }, "npm-run-path": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-4.0.1.tgz", diff --git a/package.json b/package.json index 630cfcc..3a874e3 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "@mui/material": "^5.11.0", "axios": "^0.27.2", "lightbox2": "^2.11.3", + "notistack": "^3.0.1", "react": "^18.0.2", "react-dom": "^18.0.2", "react-helmet": "^6.1.0", diff --git a/src/App.jsx b/src/App.jsx index 4d46003..cd6de00 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -2,6 +2,7 @@ import { Helmet } from "react-helmet"; import { Grid } from "@mui/material"; import CssBaseline from "@mui/material/CssBaseline"; import { ThemeProvider } from "@mui/material/styles"; +import { SnackbarProvider } from "notistack"; import { useAppContext } from "./components/AppContext"; import Body from "./components/Body/Body"; @@ -23,6 +24,7 @@ function App() { {title} + {desktop && diff --git a/src/Util/PhotoInfoHelper.js b/src/Util/PhotoInfoHelper.js new file mode 100644 index 0000000..b352f13 --- /dev/null +++ b/src/Util/PhotoInfoHelper.js @@ -0,0 +1,111 @@ +import { TAG_ALBUM, TAG_EVENT, TAG_PERSON, TAG_PHOTOGRAPHER, TAG_TEAM } from "../consts"; + +const MAX_INT = 65535; + +const convertRel = (text) => convertNum(text) / MAX_INT; + +function getPosition(encodedPosition) { + return { + left: convertRel(encodedPosition.substr(0, 4)), + top: convertRel(encodedPosition.substr(4, 4)), + right: convertRel(encodedPosition.substr(8, 4)), + bottom: convertRel(encodedPosition.substr(12, 4)), + }; +} + +function serizalizePosition(position) { + return Math.round(position.left * MAX_INT).toString(16).padStart(4, "0") + + Math.round(position.top * MAX_INT).toString(16).padStart(4, "0") + + Math.round(position.right * MAX_INT).toString(16).padStart(4, "0") + + Math.round(position.bottom * MAX_INT).toString(16).padStart(4, "0"); +} + +function parsePerson(personTag) { + const openBracket = personTag.indexOf("("); + if (openBracket === -1) { + return null; + } + const name = personTag.substr(0, openBracket); + const closingBracket = personTag.indexOf(")", openBracket); + const encodedPosition = personTag.substr(openBracket + 1, closingBracket - openBracket - 1); + if (name === "") { + return null; + } + return { + name: name, + position: getPosition(encodedPosition) + }; +} + +function serizalizePerson(person) { + return person.name + "(" + serizalizePosition(person.position) + ")"; +} + +const convertNum = (text) => parseInt("0x" + text); + +function ParsePhotoInfo(tags, description) { + let photoInfo = { + event: [], + team: [], + person: [], + album: [], + photographer: [], + }; + photoInfo.photographer.push(description?.replaceAll("Photographer: ", "")); + if (tags === null || tags === undefined) { + return photoInfo; + } + for (const tag of tags) { + if (tag === null) { + continue; + } + if (tag.startsWith(TAG_EVENT)) { + photoInfo.event.push(tag.replaceAll(TAG_EVENT + "$", "")); + continue; + } + if (tag.startsWith(TAG_TEAM)) { + photoInfo.team.push(tag.replaceAll(TAG_TEAM + "$", "")); + continue; + } + if (tag.startsWith(TAG_PERSON)) { + photoInfo.person.push({ name: tag.replaceAll(TAG_PERSON + "$", "") }); + continue; + } + if (tag.startsWith(TAG_ALBUM)) { + photoInfo.album.push(tag.replaceAll(TAG_ALBUM + "$", "")); + continue; + } + if (tag.startsWith(TAG_PHOTOGRAPHER)) { + photoInfo.photographer.push(tag.replaceAll(TAG_PHOTOGRAPHER + "$", "")); + continue; + } + const parsedPerson = parsePerson(tag); + if (parsedPerson !== null) { + photoInfo.person.push(parsedPerson); + } + } + return photoInfo; +} + +function SerializePhotoInfo(photoInfo) { + let tags = []; + for (const event of photoInfo.event) { + tags.push(TAG_EVENT + "$" + event); + } + for (const team of photoInfo.team) { + tags.push(TAG_TEAM + "$" + team); + } + for (const album of photoInfo.album) { + tags.push(TAG_ALBUM + "$" + album); + } + for (const photographer of photoInfo.photographer) { + tags.push(TAG_PHOTOGRAPHER + "$" + photographer); + } + for (const person of photoInfo.person) { + tags.push(serizalizePerson(person)); + } + return tags.map(tag => `"${tag.trim()}"`); +} + + +export { ParsePhotoInfo, SerializePhotoInfo }; diff --git a/src/Util/PhotoParser.js b/src/Util/PhotoParser.js deleted file mode 100644 index bb765a1..0000000 --- a/src/Util/PhotoParser.js +++ /dev/null @@ -1,76 +0,0 @@ -import { TAG_ALBUM, TAG_EVENT, TAG_PERSON, TAG_PHOTOGRAPHER, TAG_TEAM } from "../consts"; - -const MAX_INT = 65535; - -const convertRel = (text) => convertNum(text) / MAX_INT; - -function getPosition(encodedPosition) { - return { - left: convertRel(encodedPosition.substr(0, 4)), - top: convertRel(encodedPosition.substr(4, 4)), - right: convertRel(encodedPosition.substr(8, 4)), - bottom: convertRel(encodedPosition.substr(12, 4)), - }; -} - -function parsePerson(personTag) { - const openBracket = personTag.indexOf("("); - if (openBracket === -1) { - return null; - } - const name = personTag.substr(0, openBracket); - const closingBracket = personTag.indexOf(")", openBracket); - const encodedPosition = personTag.substr(openBracket + 1, closingBracket - openBracket - 1); - if (name === "") { - return null; - } - return { - name: name, - position: getPosition(encodedPosition) - }; -} - -const convertNum = (text) => parseInt("0x" + text); - -function ParsePhotoInfo(tags, description) { - let info = { - event: [], - team: [], - person: [], - album: [], - photographer: [], - }; - info.photographer?.push(description.replaceAll("Photographer: ", "")); - for (const tag of tags) { - if (tag === null) { - continue; - } - if (tag.startsWith(TAG_EVENT)) { - info.event.push(tag.replaceAll(TAG_EVENT + "$", "")); - continue; - } - if (tag.startsWith(TAG_TEAM)) { - info.team.push(tag.replaceAll(TAG_TEAM + "$", "")); - continue; - } - if (tag.startsWith(TAG_PERSON)) { - info.person.push({ name: tag.replaceAll(TAG_PERSON + "$", "") }); - continue; - } - if (tag.startsWith(TAG_ALBUM)) { - info.album.push(tag.replaceAll(TAG_ALBUM + "$", "")); - continue; - } - if (tag.startsWith(TAG_PHOTOGRAPHER)) { - info.photographer.push(tag.replaceAll(TAG_PHOTOGRAPHER + "$", "")); - continue; - } - const parsedPerson = parsePerson(tag); - if (parsedPerson !== null) { - info.person.push(parsedPerson); - } - } - return info; -} - -export { ParsePhotoInfo }; diff --git a/src/components/Body/Body.jsx b/src/components/Body/Body.jsx index 1c5afba..de48e7d 100644 --- a/src/components/Body/Body.jsx +++ b/src/components/Body/Body.jsx @@ -1,12 +1,13 @@ import { useEffect, useRef, useState } from "react"; import InfiniteScroll from "react-infinite-scroller"; -import { Box,Typography } from "@mui/material"; +import { Box, Typography } from "@mui/material"; import "../../consts"; import usePhotoLoader from "../../Util/PhotoLoader"; import { useAppContext } from "../AppContext"; +import { PhotoInfoProvider } from "./PhotoInfo/PhotoInfoContext"; import MyModal from "./MyModal"; import PhotoGridByYear from "./PhotoGridByYear"; diff --git a/src/components/Body/Lightbox.jsx b/src/components/Body/Lightbox.jsx index 50d2e0e..11249fb 100644 --- a/src/components/Body/Lightbox.jsx +++ b/src/components/Body/Lightbox.jsx @@ -1,14 +1,12 @@ -import { useEffect, useRef, useState } from "react"; +import { useRef, useState } from "react"; import ArrowForwardIosIcon from "@mui/icons-material/ArrowForwardIos"; import { IconButton } from "@mui/material"; import styled from "styled-components"; -import { ParsePhotoInfo } from "../../Util/PhotoParser"; -import PhotoService from "../../Util/PhotoService"; - +import { usePhotoInfo } from "./PhotoInfo/PhotoInfoContext"; +import PhotoInfoPanel from "./PhotoInfo/PhotoInfoPanel"; import Control from "./Control"; import FaceDiv from "./FaceDiv"; -import PhotoInfo from "./PhotoInfo"; import "../../styles/Body.css"; @@ -19,18 +17,7 @@ const Lightbox = ({ leftArrow, rightArrow }) => { - const [photoInfo, setPhotoInfo] = useState(null); - - useEffect(() => { - PhotoService.getPhotoInfo(photo.id) - .then(response => { - const tags = response.data.photo.tags.tag?.map(tag => tag.raw); - const description = response.data.photo.description._content; - const newPhotoInfo = ParsePhotoInfo(tags, description); - setPhotoInfo(newPhotoInfo); - } - ); - }, [photo.id]); + const { photoInfo, editMode } = usePhotoInfo(); const [face, setFace] = useState(null); const imgRef = useRef(null); @@ -63,14 +50,14 @@ const Lightbox = ({ face={face} setFace={setFace} key={person.name + "facediv" + person.position.top} />))} - - - {leftArrow &&
+ {!editMode && } + + {leftArrow && !editMode &&
} - {rightArrow &&
+ {rightArrow && !editMode &&
diff --git a/src/components/Body/PhotoInfo.jsx b/src/components/Body/PhotoInfo.jsx deleted file mode 100644 index b17a19f..0000000 --- a/src/components/Body/PhotoInfo.jsx +++ /dev/null @@ -1,53 +0,0 @@ -import { useState } from "react"; -import DownloadIcon from "@mui/icons-material/Download"; -import OpenInNewIcon from "@mui/icons-material/OpenInNew"; -import VisibilityIcon from "@mui/icons-material/Visibility"; -import VisibilityOffIcon from "@mui/icons-material/VisibilityOff"; -import { IconButton, Tooltip } from "@mui/material"; - -import { FLICKR_IMAGE_PREFIX } from "../../consts"; - -import { AlbumInfo, EventInfo, PersonInfo,PhotographerInfo, TeamInfo } from "./PhotoInfoDetails"; - -import "../../styles/PhotoInfo.css"; - -const PhotoInfo = ({ photoInfo, setFace, photo }) => { - const [hidden, setHidden] = useState(false); - - function toogleHidden() { - setHidden(!hidden); - } - - return ( -
- {!hidden && PhotographerInfo({ photoInfo })} - {!hidden && AlbumInfo({ photoInfo })} - {!hidden && EventInfo({ photoInfo })} - {!hidden && TeamInfo({ photoInfo })} - {!hidden && PersonInfo({ photoInfo, setFace })} - -
- - - {hidden - ? - : } - - - - - - - - - - - - -
-
- - ); -}; - -export default PhotoInfo; diff --git a/src/components/Body/PhotoInfo/PhotoInfoContext.jsx b/src/components/Body/PhotoInfo/PhotoInfoContext.jsx new file mode 100644 index 0000000..32ba6c3 --- /dev/null +++ b/src/components/Body/PhotoInfo/PhotoInfoContext.jsx @@ -0,0 +1,70 @@ +import { createContext, useContext, useEffect, useState } from "react"; + +import { ParsePhotoInfo } from "../../../Util/PhotoInfoHelper"; +import PhotoService from "../../../Util/PhotoService"; +import { useAppContext } from "../../AppContext"; + +const PhotoInfoContext = createContext(null); + +const PhotoInfoProvider = ({ children }) => { + const { data } = useAppContext(); + + const [photoInfo, setPhotoInfo] = useState(null); + const [editMode, setEditMode] = useState(false); + + useEffect(() => { + const fullscreenPhotoId = data.fullscreenPhotoId; + if (fullscreenPhotoId === null) { + return; + } + PhotoService.getPhotoInfo(fullscreenPhotoId) + .then(response => { + const tags = response.data?.photo?.tags?.tag?.map(tag => tag.raw); + const description = response.data?.photo?.description._content; + const newPhotoInfo = ParsePhotoInfo(tags, description); + setPhotoInfo(newPhotoInfo); + } + ); + }, [data.fullscreenPhotoId]); + + function setEvents(newEvents) { + setPhotoInfo({ ...photoInfo, events: newEvents }); + } + + function setPeople(newPeople) { + setPhotoInfo({ ...photoInfo, people: newPeople }); + } + + function setAlbum(newAlbum) { + setPhotoInfo({ ...photoInfo, album: newAlbum }); + } + + function setPhotographer(newPhotographer) { + setPhotoInfo({ ...photoInfo, photographer: newPhotographer }); + } + + return ( + + {children} + + ); +}; + +const usePhotoInfo = () => { + const context = useContext(PhotoInfoContext); + if (context === undefined || context === null) { + throw new Error("usePhotoInfo must be called within PhotoInfoProvider"); + } + return context; +}; + +export { PhotoInfoProvider, usePhotoInfo }; diff --git a/src/components/Body/PhotoInfoDetails.jsx b/src/components/Body/PhotoInfo/PhotoInfoDetails.jsx similarity index 72% rename from src/components/Body/PhotoInfoDetails.jsx rename to src/components/Body/PhotoInfo/PhotoInfoDetails.jsx index fff7aa5..3b3f20a 100644 --- a/src/components/Body/PhotoInfoDetails.jsx +++ b/src/components/Body/PhotoInfo/PhotoInfoDetails.jsx @@ -1,3 +1,9 @@ +import { AddCircleOutline } from "@mui/icons-material"; +import { IconButton } from "@mui/material"; + +import { usePhotoInfo } from "./PhotoInfoContext"; + + const ComaSeparated = (list) => { return list.map((item, index) => [ index > 0 && ", ", @@ -5,21 +11,29 @@ const ComaSeparated = (list) => { ]); }; -const PhotographerInfo = ({ photoInfo }) => { +const PhotographerInfo = () => { + const { photoInfo } = usePhotoInfo(); + if (!photoInfo || photoInfo.photographer.length === 0) { return null; } - const photographer = photoInfo.photographer; + const photographer = photoInfo.photographer.map(photographer => ( + + {photographer} + + )); return (
- Photographer: {photographer.join(", ")} + Photographer: {ComaSeparated(photographer)}
); }; -const AlbumInfo = ({ photoInfo }) => { +const AlbumInfo = () => { + const { photoInfo, editMode } = usePhotoInfo(); + if (!photoInfo || photoInfo.album.length === 0) { return null; } @@ -38,7 +52,9 @@ const AlbumInfo = ({ photoInfo }) => { ); }; -const EventInfo = ({ photoInfo }) => { +const EventInfo = () => { + const { photoInfo, editMode } = usePhotoInfo(); + if (!photoInfo || photoInfo.event.length === 0) { return null; } @@ -57,7 +73,9 @@ const EventInfo = ({ photoInfo }) => { ); }; -const TeamInfo = ({ photoInfo }) => { +const TeamInfo = () => { + const { photoInfo } = usePhotoInfo(); + if (!photoInfo || photoInfo.team.length === 0) { return null; } @@ -76,7 +94,9 @@ const TeamInfo = ({ photoInfo }) => { ); }; -const PersonInfo = ({ photoInfo, setFace }) => { +const PersonInfo = ({ setFace }) => { + const { photoInfo } = usePhotoInfo(); + if (!photoInfo || photoInfo.person.length === 0) { return null; } @@ -103,4 +123,4 @@ const PersonInfo = ({ photoInfo, setFace }) => { ); }; -export { AlbumInfo, EventInfo, PersonInfo,PhotographerInfo, TeamInfo }; +export { AlbumInfo, EventInfo, PersonInfo, PhotographerInfo, TeamInfo }; diff --git a/src/components/Body/PhotoInfo/PhotoInfoPanel.jsx b/src/components/Body/PhotoInfo/PhotoInfoPanel.jsx new file mode 100644 index 0000000..308926e --- /dev/null +++ b/src/components/Body/PhotoInfo/PhotoInfoPanel.jsx @@ -0,0 +1,83 @@ +import { useState } from "react"; +import DownloadIcon from "@mui/icons-material/Download"; +import EditIcon from "@mui/icons-material/Edit"; +import OpenInNewIcon from "@mui/icons-material/OpenInNew"; +import VisibilityIcon from "@mui/icons-material/Visibility"; +import VisibilityOffIcon from "@mui/icons-material/VisibilityOff"; +import { Button, IconButton, Tooltip } from "@mui/material"; +import { enqueueSnackbar } from "notistack"; + +import { FLICKR_IMAGE_PREFIX } from "../../../consts"; +import { SerializePhotoInfo } from "../../../Util/PhotoInfoHelper"; + +import { usePhotoInfo } from "./PhotoInfoContext"; +import { AlbumInfo, EventInfo, PersonInfo, PhotographerInfo, TeamInfo } from "./PhotoInfoDetails"; + +import "../../../styles/PhotoInfo.css"; + +const PhotoInfoPanel = ({ setFace, photo }) => { + const { editMode, setEditMode, photoInfo } = usePhotoInfo(); + + const [hidden, setHidden] = useState(false); + + const toolTipsHidden = editMode; + + function finishEditing() { + setEditMode(false); + navigator.clipboard.writeText(SerializePhotoInfo(photoInfo).join(", ")); + enqueueSnackbar("New tags copied to clipboard", { variant: "success", autoHideDuration: 2000 }); + } + + function toogleHidden() { + setHidden(!hidden); + } + + return ( +
+ {!hidden && PhotographerInfo()} + {!hidden && AlbumInfo()} + {!hidden && EventInfo()} + {!hidden && TeamInfo()} + {!hidden && PersonInfo({ setFace })} + + +
+ {toolTipsHidden && + + + } + {!toolTipsHidden && + + setEditMode(true)}> + + + } + {!toolTipsHidden && + + + {hidden + ? + : } + + } + {!toolTipsHidden && + + + + + } + {!toolTipsHidden && + + + + + } +
+
+ + ); +}; + +export default PhotoInfoPanel; diff --git a/src/index.jsx b/src/index.jsx index 02a0c6d..2e93df3 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -3,6 +3,7 @@ import ReactDOM from "react-dom/client"; import { BrowserRouter } from "react-router-dom"; import { AppContextProvider } from "./components/AppContext"; +import { PhotoInfoProvider } from "./components/Body/PhotoInfo/PhotoInfoContext"; import App from "./App"; const root = ReactDOM.createRoot(document.getElementById("root")); @@ -11,7 +12,9 @@ root.render( - + + +