Skip to content

useCdaBlob does not handle anything other than plaintext and json #179

@krowvin

Description

@krowvin

In the useCdaBlob callback it only handles text or json.

if (contentType && contentType.includes("application/json")) {
return await response.raw.json();
} else {
return await response.raw.text();
}

This means PDF/images will return a URL encoded response that will not be directly usable in a param URL.

I intended users would directly query for these resources i.e.

<img src="/cwms-data/blobs/EUFA.PLOT.PNG?office=SWT" />

However, with some tweaks useCdaBlob could be made to return all data types.

One issue I forsee is that we would need utility hooks to handle converting to raw response to an ObjectURL. Which has its own issues with lifecycle vs the caching React does.

Something like this could resolve the ObjectURL issue.

export function useObjectUrl(blob?: Blob | null) {
  const [url, setUrl] = useState<string | null>(null);

  useEffect(() => {
    if (!blob) {
      setUrl(null);
      return;
    }

    const next = URL.createObjectURL(blob);
    setUrl(next);

    return () => {
      URL.revokeObjectURL(next);
    };
  }, [blob]);

  return url;
}

Perhaps?

Side note: I'm using blobraw because the current swagger spec cwmsjs was built on returned null for the Blob type on the promise. Also needing the response object to determine the type.

See: USACE/cwms-data-api#1286

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmediumMedium Priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions