Open History to see the last commit
1import type { AssetResource, EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { useMemo } from "react";
3
4import { useAssetUrlState, useRefreshAssetUrl } from "../../state/assets";
5import {
6 isAbsolutePath,
7 isAudioPreviewFile,
8 isVideoPreviewFile,
9 resolveWorkspaceFilePath,
10} from "./filePath";
11
12export function useWorkspaceFileAssetUrlState(props: {
13 readonly cwd: string | null;
14 readonly environmentId: EnvironmentId | null;
15 readonly relativePath: string | null;
16 readonly threadId: ThreadId | null;
17 readonly draftCwd?: string | null;
18}) {
19 const absolutePath = useMemo(
20 () =>
21 props.cwd !== null && props.relativePath !== null
22 ? resolveWorkspaceFilePath(props.cwd, props.relativePath)
23 : null,
24 [props.cwd, props.relativePath],
25 );
26
27 const relativePath = props.relativePath;
28 const draftCwd = props.draftCwd ?? null;
29 const resource = useMemo<AssetResource | null>(() => {
30 if (absolutePath === null || relativePath === null) return null;
31 if (props.threadId !== null) {
32 return {
33 _tag:
34 isVideoPreviewFile(absolutePath) ||
35 isAudioPreviewFile(absolutePath) ||
36 isAbsolutePath(relativePath)
37 ? "media-file"
38 : "workspace-file",
39 threadId: props.threadId,
40 path: absolutePath,
41 };
42 }
43 if (draftCwd === null) return null;
44 return { _tag: "draft-workspace-file", cwd: draftCwd, path: relativePath };
45 }, [absolutePath, relativePath, props.threadId, draftCwd]);
46 const state = useAssetUrlState(props.environmentId, resource);
47 const refresh = useRefreshAssetUrl(props.environmentId, resource);
48 return { ...state, resource, refresh };
49}