2023-08-10 15:56:36 -07:00
|
|
|
import React from "react";
|
|
|
|
import { useToast } from "@chakra-ui/react";
|
2023-08-10 17:15:07 -07:00
|
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
2023-08-10 15:56:36 -07:00
|
|
|
import { useDebounce } from "../util";
|
|
|
|
import useCurrentUser from "../components/useCurrentUser";
|
|
|
|
import { outfitStatesAreEqual } from "./useOutfitState";
|
2023-11-02 13:50:33 -07:00
|
|
|
import { useSaveOutfitMutation } from "../loaders/outfits";
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
function useOutfitSaving(outfitState, dispatchToOutfit) {
|
|
|
|
const { isLoggedIn, id: currentUserId } = useCurrentUser();
|
2023-08-10 17:15:07 -07:00
|
|
|
const { pathname } = useLocation();
|
|
|
|
const navigate = useNavigate();
|
2023-08-10 15:56:36 -07:00
|
|
|
const toast = useToast();
|
|
|
|
|
|
|
|
// Whether this outfit is new, i.e. local-only, i.e. has _never_ been saved
|
|
|
|
// to the server.
|
|
|
|
const isNewOutfit = outfitState.id == null;
|
|
|
|
|
|
|
|
// Whether this outfit's latest local changes have been saved to the server.
|
|
|
|
// And log it to the console!
|
|
|
|
const latestVersionIsSaved =
|
|
|
|
outfitState.savedOutfitState &&
|
|
|
|
outfitStatesAreEqual(
|
|
|
|
outfitState.outfitStateWithoutExtras,
|
2023-10-24 16:45:49 -07:00
|
|
|
outfitState.savedOutfitState,
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
React.useEffect(() => {
|
|
|
|
console.debug(
|
|
|
|
"[useOutfitSaving] Latest version is saved? %s\nCurrent: %o\nSaved: %o",
|
|
|
|
latestVersionIsSaved,
|
|
|
|
outfitState.outfitStateWithoutExtras,
|
2023-10-24 16:45:49 -07:00
|
|
|
outfitState.savedOutfitState,
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
}, [
|
|
|
|
latestVersionIsSaved,
|
|
|
|
outfitState.outfitStateWithoutExtras,
|
|
|
|
outfitState.savedOutfitState,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Only logged-in users can save outfits - and they can only save new outfits,
|
|
|
|
// or outfits they created.
|
|
|
|
const canSaveOutfit =
|
|
|
|
isLoggedIn && (isNewOutfit || outfitState.creator?.id === currentUserId);
|
|
|
|
|
|
|
|
// Users can delete their own outfits too. The logic is slightly different
|
|
|
|
// than for saving, because you can save an outfit that hasn't been saved
|
|
|
|
// yet, but you can't delete it.
|
|
|
|
const canDeleteOutfit = !isNewOutfit && canSaveOutfit;
|
|
|
|
|
2023-11-02 13:50:33 -07:00
|
|
|
const saveOutfitMutation = useSaveOutfitMutation({
|
|
|
|
onSuccess: (outfit) => {
|
2023-11-02 17:12:59 -07:00
|
|
|
if (outfit.id === outfitState.id && outfit.name !== outfitState.name) {
|
2023-11-02 13:50:33 -07:00
|
|
|
dispatchToOutfit({
|
|
|
|
type: "rename",
|
|
|
|
outfitName: outfit.name,
|
2023-08-10 15:56:36 -07:00
|
|
|
});
|
2023-11-02 13:50:33 -07:00
|
|
|
}
|
2023-10-24 16:45:49 -07:00
|
|
|
},
|
2023-11-02 13:50:33 -07:00
|
|
|
});
|
|
|
|
const isSaving = saveOutfitMutation.isPending;
|
2023-11-06 12:54:23 -08:00
|
|
|
const saveError = saveOutfitMutation.error;
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
const saveOutfitFromProvidedState = React.useCallback(
|
|
|
|
(outfitState) => {
|
2023-11-02 13:50:33 -07:00
|
|
|
saveOutfitMutation
|
|
|
|
.mutateAsync({
|
2023-08-10 15:56:36 -07:00
|
|
|
id: outfitState.id,
|
|
|
|
name: outfitState.name,
|
|
|
|
speciesId: outfitState.speciesId,
|
|
|
|
colorId: outfitState.colorId,
|
|
|
|
pose: outfitState.pose,
|
|
|
|
wornItemIds: [...outfitState.wornItemIds],
|
|
|
|
closetedItemIds: [...outfitState.closetedItemIds],
|
2023-11-02 13:50:33 -07:00
|
|
|
})
|
|
|
|
.then((outfit) => {
|
2023-08-10 15:56:36 -07:00
|
|
|
// Navigate to the new saved outfit URL. Our Apollo cache should pick
|
|
|
|
// up the data from this mutation response, and combine it with the
|
|
|
|
// existing cached data, to make this smooth without any loading UI.
|
|
|
|
if (pathname !== `/outfits/[outfitId]`) {
|
2023-08-10 17:15:07 -07:00
|
|
|
navigate(`/outfits/${outfit.id}`);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
toast({
|
|
|
|
status: "error",
|
|
|
|
title: "Sorry, there was an error saving this outfit!",
|
|
|
|
description: "Maybe check your connection and try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// It's important that this callback _doesn't_ change when the outfit
|
|
|
|
// changes, so that the auto-save effect is only responding to the
|
|
|
|
// debounced state!
|
2023-11-02 18:03:15 -07:00
|
|
|
[saveOutfitMutation, pathname, navigate, toast],
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const saveOutfit = React.useCallback(
|
|
|
|
() => saveOutfitFromProvidedState(outfitState.outfitStateWithoutExtras),
|
2023-10-24 16:45:49 -07:00
|
|
|
[saveOutfitFromProvidedState, outfitState.outfitStateWithoutExtras],
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// Auto-saving! First, debounce the outfit state. Use `outfitStateWithoutExtras`,
|
|
|
|
// which only contains the basic fields, and will keep a stable object
|
|
|
|
// identity until actual changes occur. Then, save the outfit after the user
|
|
|
|
// has left it alone for long enough, so long as it's actually different
|
|
|
|
// than the saved state.
|
|
|
|
const debouncedOutfitState = useDebounce(
|
|
|
|
outfitState.outfitStateWithoutExtras,
|
|
|
|
2000,
|
|
|
|
{
|
|
|
|
// When the outfit ID changes, update the debounced state immediately!
|
|
|
|
forceReset: (debouncedOutfitState, newOutfitState) =>
|
|
|
|
debouncedOutfitState.id !== newOutfitState.id,
|
2023-10-24 16:45:49 -07:00
|
|
|
},
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
// HACK: This prevents us from auto-saving the outfit state that's still
|
|
|
|
// loading. I worry that this might not catch other loading scenarios
|
|
|
|
// though, like if the species/color/pose is in the GQL cache, but the
|
|
|
|
// items are still loading in... not sure where this would happen tho!
|
|
|
|
const debouncedOutfitStateIsSaveable =
|
|
|
|
debouncedOutfitState.speciesId &&
|
|
|
|
debouncedOutfitState.colorId &&
|
|
|
|
debouncedOutfitState.pose;
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (
|
|
|
|
!isNewOutfit &&
|
|
|
|
canSaveOutfit &&
|
2023-11-06 12:38:38 -08:00
|
|
|
!isSaving &&
|
2023-11-06 12:54:23 -08:00
|
|
|
!saveError &&
|
2023-08-10 15:56:36 -07:00
|
|
|
debouncedOutfitStateIsSaveable &&
|
|
|
|
!outfitStatesAreEqual(debouncedOutfitState, outfitState.savedOutfitState)
|
|
|
|
) {
|
|
|
|
console.info(
|
|
|
|
"[useOutfitSaving] Auto-saving outfit\nSaved: %o\nCurrent (debounced): %o",
|
|
|
|
outfitState.savedOutfitState,
|
2023-10-24 16:45:49 -07:00
|
|
|
debouncedOutfitState,
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
saveOutfitFromProvidedState(debouncedOutfitState);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
isNewOutfit,
|
|
|
|
canSaveOutfit,
|
2023-11-06 12:38:38 -08:00
|
|
|
isSaving,
|
2023-11-06 12:54:23 -08:00
|
|
|
saveError,
|
2023-08-10 15:56:36 -07:00
|
|
|
debouncedOutfitState,
|
|
|
|
debouncedOutfitStateIsSaveable,
|
|
|
|
outfitState.savedOutfitState,
|
|
|
|
saveOutfitFromProvidedState,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// When the outfit changes, clear out the error state from previous saves.
|
|
|
|
// We'll send the mutation again after the debounce, and we don't want to
|
|
|
|
// show the error UI in the meantime!
|
2023-11-06 12:54:23 -08:00
|
|
|
const resetMutation = saveOutfitMutation.reset;
|
|
|
|
React.useEffect(
|
|
|
|
() => resetMutation(),
|
|
|
|
[outfitState.outfitStateWithoutExtras, resetMutation],
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
canSaveOutfit,
|
|
|
|
canDeleteOutfit,
|
|
|
|
isNewOutfit,
|
|
|
|
isSaving,
|
|
|
|
latestVersionIsSaved,
|
|
|
|
saveError,
|
|
|
|
saveOutfit,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useOutfitSaving;
|