2024-09-20 19:40:44 +02:00
|
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { GB, TB } from "../../utils/constants";
|
|
|
|
|
import { Promises } from "../../utils/promises";
|
|
|
|
|
import { WebStorage } from "../../utils/web-storage";
|
2024-09-24 17:52:34 +02:00
|
|
|
import { AvailabilityState } from "./types";
|
2024-09-20 19:40:44 +02:00
|
|
|
import { Dispatch, useState } from "react";
|
|
|
|
|
import {
|
|
|
|
|
StepperAction,
|
|
|
|
|
StepperState,
|
|
|
|
|
} from "@codex-storage/marketplace-ui-components";
|
|
|
|
|
import * as Sentry from "@sentry/browser";
|
|
|
|
|
import { SafeValue } from "@codex-storage/sdk-js/async";
|
|
|
|
|
import { Times } from "../../utils/times";
|
2024-09-23 18:41:13 +02:00
|
|
|
import { CodexSdk } from "../../sdk/codex";
|
2024-09-20 19:40:44 +02:00
|
|
|
|
|
|
|
|
export function useAvailabilityMutation(
|
|
|
|
|
dispatch: Dispatch<StepperAction>,
|
|
|
|
|
state: StepperState
|
|
|
|
|
) {
|
|
|
|
|
const queryClient = useQueryClient();
|
2024-09-23 18:41:13 +02:00
|
|
|
const [error, setError] = useState<Error | null>(null);
|
2024-09-20 19:40:44 +02:00
|
|
|
|
|
|
|
|
const { mutateAsync } = useMutation({
|
|
|
|
|
mutationKey: ["debug"],
|
|
|
|
|
mutationFn: ({
|
|
|
|
|
totalSize,
|
|
|
|
|
totalSizeUnit,
|
|
|
|
|
duration,
|
|
|
|
|
durationUnit = "days",
|
|
|
|
|
...input
|
2024-09-24 17:52:34 +02:00
|
|
|
}: AvailabilityState) => {
|
2024-09-20 19:40:44 +02:00
|
|
|
const unit = totalSizeUnit === "gb" ? GB : TB;
|
|
|
|
|
const marketplace = CodexSdk.marketplace;
|
2024-09-24 11:35:00 +02:00
|
|
|
const time = Times.toSeconds(duration, durationUnit);
|
2024-09-20 19:40:44 +02:00
|
|
|
|
|
|
|
|
const fn: (
|
2024-09-24 17:52:34 +02:00
|
|
|
input: Omit<AvailabilityState, "totalSizeUnit" | "durationUnit">
|
2024-09-20 19:40:44 +02:00
|
|
|
) => Promise<SafeValue<unknown>> = input.id
|
|
|
|
|
? (input) =>
|
|
|
|
|
marketplace.updateAvailability({ ...input, id: input.id || "" })
|
|
|
|
|
: (input) => marketplace.createAvailability(input);
|
|
|
|
|
|
|
|
|
|
return fn({
|
|
|
|
|
...input,
|
|
|
|
|
duration: time,
|
|
|
|
|
totalSize: Math.trunc(totalSize * unit),
|
|
|
|
|
}).then((s) => Promises.rejectOnError(s));
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["availabilities"] });
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["space"] });
|
|
|
|
|
|
|
|
|
|
WebStorage.delete("availability");
|
2024-09-23 17:04:08 +02:00
|
|
|
WebStorage.delete("availability-step");
|
2024-09-20 19:40:44 +02:00
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
|
type: "next",
|
|
|
|
|
step: state.step,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
if (import.meta.env.PROD) {
|
|
|
|
|
Sentry.captureException(error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 18:41:13 +02:00
|
|
|
setError(error);
|
|
|
|
|
|
2024-09-24 10:32:57 +02:00
|
|
|
WebStorage.set("availability-step", state.step - 1);
|
|
|
|
|
|
2024-09-23 18:41:13 +02:00
|
|
|
dispatch({
|
|
|
|
|
type: "next",
|
|
|
|
|
step: state.step,
|
2024-09-24 10:32:57 +02:00
|
|
|
});
|
2024-09-20 19:40:44 +02:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-23 18:41:13 +02:00
|
|
|
return { mutateAsync, error };
|
2024-09-20 19:40:44 +02:00
|
|
|
}
|