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 =
20 props.cwd !== null && props.relativePath !== null
21 ? resolveWorkspaceFilePath(props.cwd, props.relativePath)
22 : null;
23
24 const relativePath = props.relativePath;
25 const draftCwd = props.draftCwd ?? null;
26 const resource = useMemo<AssetResource | null>(() => {
27 if (absolutePath === null || relativePath === null) return null;
28 if (props.threadId !== null) {
29 return {
30 _tag:
31 isVideoPreviewFile(absolutePath) ||
32 isAudioPreviewFile(absolutePath) ||
33 isAbsolutePath(relativePath)
34 ? "media-file"
35 : "workspace-file",
36 threadId: props.threadId,
37 path: absolutePath,
38 };
39 }
40 if (draftCwd === null) return null;
41 return { _tag: "draft-workspace-file", cwd: draftCwd, path: relativePath };
42 }, [absolutePath, relativePath, props.threadId, draftCwd]);
43 const state = useAssetUrlState(props.environmentId, resource);
44 const refresh = useRefreshAssetUrl(props.environmentId, resource);
45 return { ...state, resource, refresh };
46}