- added feature to cache sounds for cross devices - should work in theory
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useFileUpload } from "./useFileUpload";
|
||||
import { getSoundFileURL } from "../utils/utils";
|
||||
import type { SoundUploadValue } from "../types/types";
|
||||
import { resolveSoundSource } from "../utils/soundResolver";
|
||||
|
||||
export function useCachedSoundSrc(selected: string | undefined, fallbackUrl: string) {
|
||||
export function useCachedSoundSrc(
|
||||
selected: string | undefined,
|
||||
soundOptions: SoundUploadValue[] | undefined,
|
||||
fallbackUrl: string
|
||||
) {
|
||||
const isUploaded = !!selected && (selected.endsWith(".mp3") || selected.endsWith(".wav"));
|
||||
const fileName = isUploaded ? selected : undefined;
|
||||
|
||||
const resolved = resolveSoundSource(selected, soundOptions);
|
||||
|
||||
const { query } = useFileUpload({
|
||||
queryKey: fileName ? [fileName] : undefined,
|
||||
queryKey: resolved?.type === "uploaded" ? [resolved?.url] : undefined,
|
||||
});
|
||||
|
||||
const [objectUrl, setObjectUrl] = useState<string>();
|
||||
@@ -15,6 +22,7 @@ export function useCachedSoundSrc(selected: string | undefined, fallbackUrl: str
|
||||
|
||||
useEffect(() => {
|
||||
const blob = query?.data;
|
||||
|
||||
if (blob instanceof Blob) {
|
||||
if (objRef.current) URL.revokeObjectURL(objRef.current);
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
Reference in New Issue
Block a user