Files
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

112 lines
3.5 KiB
TypeScript

import { toast } from '@sim/emcn'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import {
type AuthenticatePublicFileResponse,
authenticatePublicFileContract,
getFileShareContract,
requestPublicFileOtpContract,
type ShareRecord,
type UpsertFileShareBody,
upsertFileShareContract,
type VerifyPublicFileOtpResponse,
verifyPublicFileOtpContract,
} from '@/lib/api/contracts/public-shares'
import { workspaceFilesKeys } from '@/hooks/queries/workspace-files'
export const FILE_SHARE_STALE_TIME = 30 * 1000
/**
* Query key factories for public shares
*/
export const shareKeys = {
all: ['publicShares'] as const,
details: () => [...shareKeys.all, 'detail'] as const,
detail: (workspaceId: string, fileId: string) =>
[...shareKeys.details(), workspaceId, fileId] as const,
}
async function fetchFileShare(
workspaceId: string,
fileId: string,
signal?: AbortSignal
): Promise<ShareRecord | null> {
const data = await requestJson(getFileShareContract, {
params: { id: workspaceId, fileId },
signal,
})
return data.share
}
export function useFileShare(workspaceId: string, fileId: string, options?: { enabled?: boolean }) {
return useQuery({
queryKey: shareKeys.detail(workspaceId, fileId),
queryFn: ({ signal }) => fetchFileShare(workspaceId, fileId, signal),
enabled: Boolean(workspaceId) && Boolean(fileId) && (options?.enabled ?? true),
staleTime: FILE_SHARE_STALE_TIME,
})
}
interface UpsertFileShareVariables extends UpsertFileShareBody {
workspaceId: string
fileId: string
}
export function useUpsertFileShare() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ workspaceId, fileId, ...body }: UpsertFileShareVariables) =>
requestJson(upsertFileShareContract, {
params: { id: workspaceId, fileId },
body,
}),
onSuccess: (data, { workspaceId, fileId }) => {
queryClient.setQueryData(shareKeys.detail(workspaceId, fileId), data.share)
queryClient.invalidateQueries({ queryKey: workspaceFilesKeys.workspaceLists(workspaceId) })
},
onError: (error) => {
toast.error(error.message)
},
})
}
/**
* Exchanges a share password for a `file_auth_{shareId}` cookie on the public
* file page. On success the page should `router.refresh()` to re-render the
* now-authorized viewer.
*/
export function usePublicFileAuth(token: string) {
return useMutation<AuthenticatePublicFileResponse, Error, { password: string }>({
mutationFn: ({ password }) =>
requestJson(authenticatePublicFileContract, {
params: { token },
body: { password },
}),
})
}
/** Requests a verification code for an email-gated share (initial send + resend). */
export function usePublicFileOtpRequest(token: string) {
return useMutation<{ message: string }, Error, { email: string }>({
mutationFn: ({ email }) =>
requestJson(requestPublicFileOtpContract, {
params: { token },
body: { email },
}),
})
}
/**
* Verifies the OTP for an email-gated share. On success the server sets the
* `file_auth_{shareId}` cookie; the page should then `router.refresh()`.
*/
export function usePublicFileOtpVerify(token: string) {
return useMutation<VerifyPublicFileOtpResponse, Error, { email: string; otp: string }>({
mutationFn: ({ email, otp }) =>
requestJson(verifyPublicFileOtpContract, {
params: { token },
body: { email, otp },
}),
})
}