Improve app loading state

This commit is contained in:
Prayag Prajapati committed 2023-08-28 16:13:04 +05:30
1 parent 7df0f3f9d3
commit f65e32cd49
8 files changed
+139 -32

No files matched your search

+16
View File
@@ -71,6 +71,7 @@ import { delUser, getUser } from "./utils/storage/user.js";
import axios from "axios"; import axios from "axios";
import { useBackdropStore } from "./utils/store/backdrop.js"; import { useBackdropStore } from "./utils/store/backdrop.js";
import { useAppLoadingStore } from "./utils/store/appLoading.js";
const jellyfin = new Jellyfin({ const jellyfin = new Jellyfin({
clientInfo: { clientInfo: {
@@ -131,6 +132,21 @@ function App() {
const [serverReachable, setServerReachable] = useState(true); const [serverReachable, setServerReachable] = useState(true);
const [setIsAppLoading, setIsAppSuccess, setIsAppError] =
useAppLoadingStore((state) => [
state.setIsLoading,
state.setIsSuccess,
state.setIsError,
]);
useEffect(() => {
const appLoadingStateTimer = setInterval(() => {
setIsAppSuccess(false);
setIsAppError(false);
}, 8000);
return () => clearInterval(appLoadingStateTimer);
}, [setIsAppSuccess, setIsAppError]);
const { enqueueSnackbar } = useSnackbar(); const { enqueueSnackbar } = useSnackbar();
const createApi = async () => { const createApi = async () => {
+99 -14
View File
@@ -40,6 +40,7 @@ import { MdiCogSync } from "../icons/mdiCogSync";
import { useAppLoadingStore } from "../../utils/store/appLoading"; import { useAppLoadingStore } from "../../utils/store/appLoading";
import { MdiCheck } from "../icons/mdiCheck"; import { MdiCheck } from "../icons/mdiCheck";
import { MdiAlertDecagram } from "../icons/mdiAlertDecagram"; import { MdiAlertDecagram } from "../icons/mdiAlertDecagram";
import { green, red } from "@mui/material/colors";
export const AppBar = () => { export const AppBar = () => {
const navigate = useNavigate(); const navigate = useNavigate();
@@ -61,7 +62,7 @@ export const AppBar = () => {
const trigger = useScrollTrigger({ const trigger = useScrollTrigger({
disableHysteresis: true, disableHysteresis: true,
threshold: 0, threshold: 20,
}); });
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
@@ -82,7 +83,6 @@ export const AppBar = () => {
const [syncLoading, syncSuccess, syncError] = useAppLoadingStore( const [syncLoading, syncSuccess, syncError] = useAppLoadingStore(
(state) => [state.isLoading, state.isSuccess, state.isError], (state) => [state.isLoading, state.isSuccess, state.isError],
); );
console.log([syncLoading, syncSuccess, syncError]);
useEffect(() => { useEffect(() => {
if ( if (
@@ -162,9 +162,10 @@ export const AppBar = () => {
</IconButton> </IconButton>
</Box> </Box>
<Box sx={{ display: "flex", gap: 2 }}> <Box sx={{ display: "flex", gap: 2 }}>
<AnimatePresence> <AnimatePresence mode="wait">
{syncLoading && ( {syncLoading ? (
<Box <Box
key={`laoding`}
component={motion.div} component={motion.div}
position="relative" position="relative"
display="inline-flex" display="inline-flex"
@@ -172,10 +173,9 @@ export const AppBar = () => {
animate={{ opacity: 1 }} animate={{ opacity: 1 }}
exit={{ exit={{
opacity: 0, opacity: 0,
transition: { }}
delay: 10, transition={{
duration: 5, duration: 0.2,
},
}} }}
> >
<CircularProgress <CircularProgress
@@ -197,17 +197,102 @@ export const AppBar = () => {
justifyContent: "center", justifyContent: "center",
}} }}
> >
{syncSuccess ? (
<MdiCheck />
) : syncError ? (
<MdiAlertDecagram />
) : (
<MdiCogSync /> <MdiCogSync />
)}
</IconButton> </IconButton>
</Box> </Box>
) : syncSuccess ? (
<Box
key={`success`}
component={motion.div}
position="relative"
display="inline-flex"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{
opacity: 0,
transition: {
delay: 0.4,
},
}}
transition={{
duration: 0.2,
}}
sx={{
background: green[500],
borderRadius: "1000px",
}}
>
<CircularProgress
sx={{
opacity: syncLoading
? 1
: 0,
}}
/>
<IconButton
sx={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<MdiCheck />
</IconButton>
</Box>
) : syncError ? (
<Box
key={`error`}
component={motion.div}
position="relative"
display="inline-flex"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{
opacity: 0,
transition: {
delay: 0.4,
},
}}
transition={{
duration: 0.2,
}}
sx={{
background: red[900],
borderRadius: "1000px",
}}
>
<CircularProgress
sx={{
opacity: syncLoading
? 1
: 0,
}}
/>
<IconButton
sx={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<MdiAlertDecagram />
</IconButton>
</Box>
) : (
<></>
)} )}
</AnimatePresence> </AnimatePresence>
<IconButton> <IconButton>
<MdiHeartOutline /> <MdiHeartOutline />
</IconButton> </IconButton>
+4
View File
@@ -15,3 +15,7 @@
} }
} }
} }
.appLoading {
opacity: 0;
}
+4 -4
View File
@@ -75,8 +75,6 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
return { currentLatestMedia }; return { currentLatestMedia };
}, },
onError: (error, a, context) => { onError: (error, a, context) => {
setIsLoading(false);
setIsError(true);
enqueueSnackbar( enqueueSnackbar(
`An error occured while updating "${itemName}"`, `An error occured while updating "${itemName}"`,
{ {
@@ -85,12 +83,14 @@ const LikeButton = ({ itemId, isFavorite, queryKey, userId, itemName }) => {
); );
queryClient.setQueryData(queryKey, context.previousData); queryClient.setQueryData(queryKey, context.previousData);
console.error(error); console.error(error);
setIsError(true);
setIsLoading(false);
}, },
onSuccess: () => { onSuccess: () => {
setIsLoading(false);
setIsSuccess(true);
console.log("Successfully updated", itemId); console.log("Successfully updated", itemId);
queryClient.invalidateQueries(queryKey); queryClient.invalidateQueries(queryKey);
setIsSuccess(true);
setIsLoading(false);
}, },
}); });
return ( return (
+4 -4
View File
@@ -73,8 +73,6 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
return { previousData }; return { previousData };
}, },
onError: (error, a, context) => { onError: (error, a, context) => {
setIsLoading(false);
setIsError(true);
enqueueSnackbar( enqueueSnackbar(
`An error occured while updating "${itemName}"`, `An error occured while updating "${itemName}"`,
{ {
@@ -83,12 +81,14 @@ const MarkPlayedButton = ({ itemId, isPlayed, queryKey, userId, itemName }) => {
); );
queryClient.setQueryData(queryKey, context.previousData); queryClient.setQueryData(queryKey, context.previousData);
console.error(error); console.error(error);
setIsError(true);
setIsLoading(false);
}, },
onSuccess: () => { onSuccess: () => {
setIsLoading(false);
setIsSuccess(true);
console.log("Successfully updated", itemId); console.log("Successfully updated", itemId);
queryClient.invalidateQueries(queryKey); queryClient.invalidateQueries(queryKey);
setIsSuccess(true);
setIsLoading(false);
}, },
}); });
return ( return (
+10 -7
View File
@@ -83,13 +83,16 @@ const CarouselSlide = ({ item }) => {
punch={1} punch={1}
/> />
)} )}
<motion.img <img
className="hero-carousel-background-image" className="hero-carousel-background-image"
src={ src={
!!item.ParentBackdropItemId !!item.ParentBackdropItemId
? `${window.api.basePath}/Items/${item.ParentBackdropItemId}/Images/Backdrop` ? `${window.api.basePath}/Items/${item.ParentBackdropItemId}/Images/Backdrop`
: `${window.api.basePath}/Items/${item.Id}/Images/Backdrop` : `${window.api.basePath}/Items/${item.Id}/Images/Backdrop`
} }
style={{
opacity: 0,
}}
onLoad={(e) => onLoad={(e) =>
(e.target.style.opacity = 1) (e.target.style.opacity = 1)
} }
@@ -107,8 +110,8 @@ const CarouselSlide = ({ item }) => {
initial={{ initial={{
transform: transform:
animationDirection == "right" animationDirection == "right"
? "translateX(40px)" ? "translateX(20%)"
: "translateX(-40px)", : "translateX(-20%)",
opacity: 0, opacity: 0,
}} }}
animate={{ animate={{
@@ -118,8 +121,8 @@ const CarouselSlide = ({ item }) => {
exit={{ exit={{
transform: transform:
animationDirection == "right" animationDirection == "right"
? "translateX(-40px)" ? "translateX(-20%)"
: "translateX(40px)", : "translateX(20%)",
opacity: 0, opacity: 0,
}} }}
@@ -140,7 +143,7 @@ const CarouselSlide = ({ item }) => {
{!item.ImageTags.Logo ? ( {!item.ImageTags.Logo ? (
item.Name item.Name
) : ( ) : (
<motion.img <img
className="hero-carousel-text-logo" className="hero-carousel-text-logo"
src={ src={
window.api.basePath + window.api.basePath +
@@ -151,7 +154,7 @@ const CarouselSlide = ({ item }) => {
} }
style={{ style={{
opacity: 0, opacity: 0,
transition: "opacity 250ms", transition: "opacity 0.2s",
}} }}
onLoad={(e) => onLoad={(e) =>
(e.target.style.opacity = 1) (e.target.style.opacity = 1)
+1 -2
View File
@@ -63,11 +63,10 @@
right: 0; right: 0;
bottom: 0; bottom: 0;
background-position: center; background-position: center;
opacity: 0;
object-fit: cover; object-fit: cover;
width: 100%; width: 100%;
height: 100%; height: 100%;
transition: opacity $transition-time-fast; transition: opacity 0.5s;
} }
&-icon { &-icon {
position: absolute; position: absolute;
+1 -1
View File
@@ -905,7 +905,7 @@ const LibraryView = () => {
</Stack> </Stack>
</Toolbar> </Toolbar>
</AppBar> </AppBar>
{items.isLoading || items.isFetching ? ( {items.isLoading ? (
<Box <Box
sx={{ sx={{
position: "absolute", position: "absolute",