| import { useMemo } from "react"; |
| import { useUserQuery } from "@/hooks/use-user"; |
|
|
| type FileUrlOptions = |
| | { |
| type: "proxy" | "download"; |
| filePath: string; |
| filename?: string; |
| } |
| | { |
| type: "invoice"; |
| invoiceId: string; |
| isReceipt?: boolean; |
| } |
| | { |
| type: "url"; |
| url: string; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function useFileUrl(options: FileUrlOptions | null) { |
| const { data: user, isLoading } = useUserQuery(); |
|
|
| const result = useMemo(() => { |
| if (!options) { |
| return { |
| url: null as string | null, |
| isLoading, |
| hasFileKey: !!user?.fileKey, |
| }; |
| } |
|
|
| if (options.type === "url") { |
| |
| if (options.url.startsWith("/")) { |
| const [pathname, search] = options.url.split("?"); |
| const params = new URLSearchParams(search); |
| if (user?.fileKey) { |
| params.set("fk", user.fileKey); |
| } |
| const queryString = params.toString(); |
| return { |
| url: queryString ? `${pathname}?${queryString}` : pathname, |
| isLoading: false, |
| hasFileKey: !!user?.fileKey, |
| }; |
| } |
|
|
| |
| const url = new URL(options.url); |
| |
| if (user?.fileKey) { |
| url.searchParams.set("fk", user.fileKey); |
| } |
| return { |
| url: url.toString(), |
| isLoading: false, |
| hasFileKey: !!user?.fileKey, |
| }; |
| } |
|
|
| |
| if (!user?.fileKey) { |
| return { |
| url: null as string | null, |
| isLoading, |
| hasFileKey: false, |
| }; |
| } |
|
|
| if (options.type === "invoice") { |
| |
| const baseUrl = `${process.env.NEXT_PUBLIC_API_URL}/files/download/invoice`; |
| const url = new URL(baseUrl); |
| url.searchParams.set("id", options.invoiceId); |
| url.searchParams.set("fk", user.fileKey); |
| if (options.isReceipt) { |
| url.searchParams.set("type", "receipt"); |
| } |
| return { |
| url: url.toString(), |
| isLoading: false, |
| hasFileKey: true, |
| }; |
| } |
|
|
| |
| const { type, filePath, filename } = options; |
| const endpointPath = type === "download" ? `${type}/file` : type; |
| const baseUrl = `${process.env.NEXT_PUBLIC_API_URL}/files/${endpointPath}`; |
| const url = new URL(baseUrl); |
|
|
| if (type === "download") { |
| url.searchParams.set("path", filePath); |
| if (filename) { |
| url.searchParams.set("filename", filename); |
| } |
| } else { |
| url.searchParams.set("filePath", filePath); |
| } |
|
|
| url.searchParams.set("fk", user.fileKey); |
|
|
| return { |
| url: url.toString(), |
| isLoading: false, |
| hasFileKey: true, |
| }; |
| }, [options, user?.fileKey, isLoading]); |
|
|
| return result; |
| } |
|
|