- added feature to cache sounds for cross devices - should work in theory

This commit is contained in:
2025-10-29 15:04:40 +00:00
parent cf72a1e1d3
commit a8abed2246
10 changed files with 56 additions and 26 deletions

View File

@@ -23,7 +23,9 @@ const uploadFile = async (file: File) => {
const getUploadFiles = async ({ queryKey }: { queryKey: string[] }) => {
const [, fileName] = queryKey;
const url = `${camBase}/Mobile/${fileName}`;
// console.log(`${camBase}/Mobile/${fileName}`);
const url = fileName;
return getOrCacheBlob(url);
};
@@ -38,7 +40,7 @@ export const useFileUpload = ({ queryKey }: UseFileUploadProps) => {
mutationFn: (file: File) => uploadFile(file),
mutationKey: ["uploadFile"],
onError: (err) => toast.error(err ? err.message : ""),
onSuccess: (msg) => toast.success(msg),
onSuccess: async (msg) => toast.success(msg),
});
return { query: queryKey ? query : undefined, mutation };

View File

@@ -25,9 +25,9 @@ export function useSightingFeed(url: string | undefined) {
const [sessionStarted, setSessionStarted] = useState(false);
const [selectedSighting, setSelectedSighting] = useState<SightingType | null>(null);
const { src: soundSrc } = useCachedSoundSrc(state?.sightingSound, switchSound);
const { src: soundSrcHotlist } = useCachedSoundSrc(state?.hotlistSound, notification);
const { src: soundSrcNped } = useCachedSoundSrc(state?.NPEDsound, popup);
const { src: soundSrc } = useCachedSoundSrc(state?.sightingSound, state?.soundOptions, switchSound);
const { src: soundSrcHotlist } = useCachedSoundSrc(state?.hotlistSound, state?.soundOptions, notification);
const { src: soundSrcNped } = useCachedSoundSrc(state?.NPEDsound, state?.soundOptions, popup);
const { play: hotlistsound } = useSound(soundSrcHotlist, { volume: state.hotlistSoundVolume });
const { play: npedSound } = useSound(soundSrcNped, { volume: state.NPEDsoundVolume });

View File

@@ -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);