mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
feat(share): record and display share activity (#549)
* feat(share): record and display share activity * style(share): remove activity entry borders * docs(share): explain share link activity
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { client } from './client'
|
||||
|
||||
export function requestShareDownload(
|
||||
shareId: string,
|
||||
fileId: string,
|
||||
key: string,
|
||||
purpose?: 'download' | 'prepare' | 'clipboard',
|
||||
) {
|
||||
const password = localStorage.getItem(`share_pwd_${shareId}`)
|
||||
const postDownload = client.api.shares[':shareId'].files[':fileId']['download-url'].$post
|
||||
const input = { param: { shareId, fileId }, json: { key, purpose } }
|
||||
return password
|
||||
? postDownload(input, { headers: { 'x-share-password': password } })
|
||||
: postDownload(input)
|
||||
}
|
||||
@@ -419,6 +419,8 @@ describe('BreadcrumbNav component', () => {
|
||||
rootFolderId: 'rf-1',
|
||||
projectId: 'proj-1',
|
||||
isExpired: false,
|
||||
views: 0,
|
||||
lastViewedAt: null,
|
||||
createdAt: '2026-11-24T00:00:00.000Z',
|
||||
updatedAt: '2026-11-24T00:00:00.000Z',
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
@@ -206,10 +207,7 @@ export function BreadcrumbNav({
|
||||
if (!fileId) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, fileId, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: fileId },
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { toast } from 'sonner'
|
||||
import { copyToClipboard, copyToClipboardDeferred, copyImageToClipboard } from '@/ui/lib/clipboard'
|
||||
@@ -39,18 +40,7 @@ export function useCopyActions({
|
||||
if (transcode?.url) return transcode.url
|
||||
if (transcode?.key && item.id) {
|
||||
if (isPublic && shareId) {
|
||||
const password = localStorage.getItem(`share_pwd_${shareId}`) || ''
|
||||
const res = await client.api.shares[':shareId'].files[':fileId']['download-url'].$post(
|
||||
{
|
||||
param: { shareId, fileId: item.id },
|
||||
json: { key: transcode.key },
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'x-share-password': password,
|
||||
},
|
||||
},
|
||||
)
|
||||
const res = await requestShareDownload(shareId, item.id, transcode.key, 'clipboard')
|
||||
if (res.ok) {
|
||||
const { url } = await res.json()
|
||||
return url
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use client'
|
||||
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||
@@ -20,6 +21,18 @@ interface UseFileActionsProps {
|
||||
shareId?: string
|
||||
}
|
||||
|
||||
async function requestShareDownloadUrl(
|
||||
shareId: string,
|
||||
fileId: string,
|
||||
key: string,
|
||||
purpose: 'prepare' | 'download',
|
||||
) {
|
||||
const response = await requestShareDownload(shareId, fileId, key, purpose)
|
||||
if (response.ok) return (await response.json()).url
|
||||
}
|
||||
|
||||
type DownloadFile = { id: string; name: string; url: string; shareDownloadKey?: string }
|
||||
|
||||
export function useFileActions({
|
||||
teamId,
|
||||
projectId,
|
||||
@@ -35,9 +48,7 @@ export function useFileActions({
|
||||
const [itemsToDelete, setItemsToDelete] = useState<AssetInfo[]>([])
|
||||
const [isDownloadDialogOpen, setIsDownloadDialogOpen] = useState(false)
|
||||
const [isLoadingLinks, setIsLoadingLinks] = useState(false)
|
||||
const [resolvedFiles, setResolvedFiles] = useState<
|
||||
Array<{ id: string; name: string; url: string }>
|
||||
>([])
|
||||
const [resolvedFiles, setResolvedFiles] = useState<DownloadFile[]>([])
|
||||
const [skippedArchivedCount, setSkippedArchivedCount] = useState(0)
|
||||
const [archiveConfirmState, setArchiveConfirmState] = useState<{
|
||||
open: boolean
|
||||
@@ -257,8 +268,7 @@ export function useFileActions({
|
||||
setSkippedArchivedCount(0)
|
||||
try {
|
||||
if (isPublic && shareId) {
|
||||
const password = localStorage.getItem(`share_pwd_${shareId}`) || ''
|
||||
const links: Array<{ id: string; name: string; url: string }> = []
|
||||
const links: DownloadFile[] = []
|
||||
let skipped = 0
|
||||
for (const item of items) {
|
||||
if (
|
||||
@@ -271,20 +281,9 @@ export function useFileActions({
|
||||
}
|
||||
const key = item.media?.original?.key
|
||||
if (!key || !item.id) continue
|
||||
const res = await client.api.shares[':shareId'].files[':fileId']['download-url'].$post(
|
||||
{
|
||||
param: { shareId, fileId: item.id },
|
||||
json: { key },
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'x-share-password': password,
|
||||
},
|
||||
},
|
||||
)
|
||||
if (res.ok) {
|
||||
const { url } = await res.json()
|
||||
links.push({ id: item.id, name: item.name || 'download', url })
|
||||
const url = await requestShareDownloadUrl(shareId, item.id, key, 'prepare')
|
||||
if (url) {
|
||||
links.push({ id: item.id, name: item.name || 'download', url, shareDownloadKey: key })
|
||||
}
|
||||
}
|
||||
setSkippedArchivedCount(skipped)
|
||||
@@ -407,9 +406,21 @@ export function useFileActions({
|
||||
|
||||
const runQueue = async () => {
|
||||
const batchSize = 5
|
||||
let failed = 0
|
||||
for (let i = 0; i < files.length; i += batchSize) {
|
||||
const chunk = files.slice(i, i + batchSize)
|
||||
for (const file of chunk) {
|
||||
const urls = await Promise.allSettled(
|
||||
chunk.map((file) =>
|
||||
shareId && file.shareDownloadKey
|
||||
? requestShareDownloadUrl(shareId, file.id, file.shareDownloadKey, 'download')
|
||||
: file.url,
|
||||
),
|
||||
)
|
||||
for (const result of urls) {
|
||||
if (result.status !== 'fulfilled' || !result.value) {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
// Use a hidden iframe instead of an anchor with target="_blank".
|
||||
// Download URLs respond with Content-Disposition: attachment, so the
|
||||
// browser starts the download in the background: no flash of a tab
|
||||
@@ -417,7 +428,7 @@ export function useFileActions({
|
||||
// the hidden iframe instead of navigating the SPA tab away.
|
||||
const iframe = document.createElement('iframe')
|
||||
iframe.style.display = 'none'
|
||||
iframe.src = file.url
|
||||
iframe.src = result.value
|
||||
document.body.appendChild(iframe)
|
||||
// Remove the iframe once the download has had time to start.
|
||||
window.setTimeout(() => iframe.remove(), 60_000)
|
||||
@@ -426,7 +437,8 @@ export function useFileActions({
|
||||
await delay(600)
|
||||
}
|
||||
}
|
||||
toast.success('All downloads initiated successfully')
|
||||
if (failed > 0) toast.error(m.share_download_start_failed())
|
||||
else toast.success('All downloads initiated successfully')
|
||||
}
|
||||
|
||||
runQueue().catch((err) => {
|
||||
|
||||
@@ -238,6 +238,8 @@ describe('MobileFileHeader', () => {
|
||||
rootFolderId: 'rf-1',
|
||||
projectId: 'proj-1',
|
||||
isExpired: false,
|
||||
views: 0,
|
||||
lastViewedAt: null,
|
||||
createdAt: '2026-11-24T00:00:00.000Z',
|
||||
updatedAt: '2026-11-24T00:00:00.000Z',
|
||||
},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
@@ -113,10 +114,7 @@ export function MobileFileHeader({
|
||||
try {
|
||||
const res =
|
||||
isPublic && shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: activeFileId },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, activeFileId, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: activeFileId },
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ import { m } from '@/ui/paraglide/messages.js'
|
||||
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
||||
import { useEffect, useState, useMemo, useRef } from 'react'
|
||||
import { useIsMobile } from '@/ui/hooks/use-mobile'
|
||||
import { usePublicShareView } from '@/ui/hooks/use-public-share-view'
|
||||
import { TopNav } from './top-nav'
|
||||
import { FileBrowser } from './file-browser/file-browser'
|
||||
import { MobileFileBrowser } from './file-browser/mobile-file-browser'
|
||||
@@ -127,6 +128,7 @@ export function PublicShareManager({
|
||||
},
|
||||
)
|
||||
if (res.status === 401) throw new Error('Unauthorized')
|
||||
if (!res.ok) throw new Error('Failed to fetch shared asset')
|
||||
return (await res.json()) as unknown as AssetInfo
|
||||
},
|
||||
enabled: !!shareInfo && !!currentFolderId,
|
||||
@@ -258,6 +260,7 @@ export function PublicShareManager({
|
||||
},
|
||||
)
|
||||
if (res.status === 401) throw new Error('Unauthorized')
|
||||
if (!res.ok) throw new Error('Failed to fetch shared asset')
|
||||
return (await res.json()) as unknown as AssetInfo
|
||||
},
|
||||
enabled: !!viewingFileId && !!shareInfo,
|
||||
@@ -277,6 +280,7 @@ export function PublicShareManager({
|
||||
},
|
||||
)
|
||||
if (res.status === 401) throw new Error('Unauthorized')
|
||||
if (!res.ok) throw new Error('Failed to fetch shared asset')
|
||||
return (await res.json()) as unknown as AssetInfo
|
||||
},
|
||||
enabled: !!versionId && !!shareInfo,
|
||||
@@ -286,6 +290,12 @@ export function PublicShareManager({
|
||||
// or the stack itself) is what we actually display.
|
||||
const viewingFileData = versionId ? versionFileData : stackFileData
|
||||
const isViewingFileLoading = isStackFileLoading || (!!versionId && isVersionFileLoading)
|
||||
usePublicShareView({
|
||||
shareId,
|
||||
assetId: activeFileId ?? initialFolderId,
|
||||
password,
|
||||
ready: !!shareInfo && (viewingFileId ? !!viewingFileData : !initialFolderId || !!folderInfo),
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (foldersError?.message === 'Unauthorized') {
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import type { ShareActivityKind } from '@shumai/dtos'
|
||||
import { useInfiniteQuery } from '@tanstack/react-query'
|
||||
import { isToday, isYesterday } from 'date-fns'
|
||||
import { CircleUserRound } from 'lucide-react'
|
||||
import { useEffect } from 'react'
|
||||
import { useInView } from 'react-intersection-observer'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { getLocale } from '@/ui/paraglide/runtime.js'
|
||||
import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar'
|
||||
import { Button } from './ui/button'
|
||||
import { ScrollArea } from './ui/scroll-area'
|
||||
|
||||
function activityLabel(kind: ShareActivityKind) {
|
||||
switch (kind) {
|
||||
case 'share_viewed':
|
||||
return m.share_activity_opened_share_link()
|
||||
case 'asset_viewed':
|
||||
return m.share_activity_viewed_asset()
|
||||
case 'asset_downloaded':
|
||||
return m.share_activity_downloaded()
|
||||
case 'comment_created':
|
||||
return m.share_activity_commented()
|
||||
}
|
||||
}
|
||||
|
||||
function formatDay(date: Date) {
|
||||
if (isToday(date)) return m.today()
|
||||
if (isYesterday(date)) return m.yesterday()
|
||||
return date.toLocaleDateString(getLocale(), { dateStyle: 'medium' })
|
||||
}
|
||||
|
||||
export function ShareActivityList({ shareId }: { shareId: string }) {
|
||||
const query = useInfiniteQuery({
|
||||
queryKey: ['share-activities', shareId],
|
||||
queryFn: async ({ pageParam, signal }) => {
|
||||
const response = await client.api.shares[':shareId'].activities.$get(
|
||||
{ param: { shareId }, query: { first: '20', after: pageParam } },
|
||||
{ init: { signal } },
|
||||
)
|
||||
if (!response.ok) throw new Error(m.share_activity_load_failed())
|
||||
return response.json()
|
||||
},
|
||||
initialPageParam: '',
|
||||
getNextPageParam: (page) => page.pageInfo.cursor || undefined,
|
||||
})
|
||||
const { ref, inView } = useInView()
|
||||
const { fetchNextPage, hasNextPage, isFetching, isError } = query
|
||||
useEffect(() => {
|
||||
if (inView && hasNextPage && !isFetching && !isError) void fetchNextPage()
|
||||
}, [fetchNextPage, hasNextPage, inView, isError, isFetching])
|
||||
|
||||
const events = new Map(
|
||||
query.data?.pages.flatMap((page) => page.data.map((event) => [event.id, event] as const)),
|
||||
)
|
||||
const days = Map.groupBy(events.values(), (event) => new Date(event.createdAt).toDateString())
|
||||
|
||||
return (
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<div className="space-y-4 p-3">
|
||||
{query.isPending && <p className="text-sm text-muted-foreground">{m.loading()}</p>}
|
||||
{query.isSuccess && events.size === 0 && (
|
||||
<p className="text-sm text-muted-foreground">{m.no_activity_yet()}</p>
|
||||
)}
|
||||
{[...days].map(([day, entries]) => (
|
||||
<section key={day} className="space-y-2">
|
||||
<h3 className="text-xs font-medium text-muted-foreground">
|
||||
{formatDay(new Date(entries[0].createdAt))}
|
||||
</h3>
|
||||
<ul className="space-y-2">
|
||||
{entries.map((event) => (
|
||||
<li
|
||||
key={event.id}
|
||||
className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-2 px-2 py-2"
|
||||
>
|
||||
{event.user ? (
|
||||
<Avatar className="size-8">
|
||||
<AvatarImage src={event.user.image} alt="" />
|
||||
<AvatarFallback>{event.user.name.slice(0, 2).toUpperCase()}</AvatarFallback>
|
||||
</Avatar>
|
||||
) : (
|
||||
<CircleUserRound aria-hidden="true" className="size-8 text-muted-foreground" />
|
||||
)}
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-sm font-medium">
|
||||
{event.user?.name || m.someone()}
|
||||
</div>
|
||||
<div className="flex items-baseline gap-x-2 text-xs text-muted-foreground">
|
||||
{event.asset && (
|
||||
<span className="min-w-0 truncate" title={event.asset.name ?? undefined}>
|
||||
{event.asset.name ?? m.share_activity_deleted_asset()}
|
||||
</span>
|
||||
)}
|
||||
<time dateTime={event.createdAt} className="shrink-0 whitespace-nowrap">
|
||||
{new Date(event.createdAt).toLocaleTimeString(getLocale(), {
|
||||
timeStyle: 'short',
|
||||
})}
|
||||
</time>
|
||||
</div>
|
||||
</div>
|
||||
<span className="max-w-24 text-right text-xs">{activityLabel(event.kind)}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
))}
|
||||
{query.isError && (
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">{m.share_activity_load_failed()}</p>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => void (query.isFetchNextPageError ? fetchNextPage() : query.refetch())}
|
||||
>
|
||||
{m.retry()}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{hasNextPage && <div ref={ref}>{query.isFetchingNextPage && m.loading()}</div>}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
)
|
||||
}
|
||||
@@ -52,6 +52,8 @@ const sampleShare: ShareLinkInfo = {
|
||||
rootFolderId: 'folder-share-1',
|
||||
projectId: 'proj-1',
|
||||
isExpired: false,
|
||||
views: 0,
|
||||
lastViewedAt: null,
|
||||
watermarkConfigId: null,
|
||||
watermarkStatus: 'disabled',
|
||||
watermarkConfig: null,
|
||||
|
||||
@@ -9,6 +9,8 @@ import { useMutation, useInfiniteQuery, useQuery, useQueryClient } from '@tansta
|
||||
import { useEffect, useState, useMemo } from 'react'
|
||||
import { FileBrowser } from './file-browser/file-browser'
|
||||
import { ShareSettingsSidebar } from './share-settings-sidebar'
|
||||
import { ShareActivityList } from './share-activity-list'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from './ui/tabs'
|
||||
import { FolderTree } from './folder-tree'
|
||||
import { ResizeHandle } from './resize-handle'
|
||||
import { useFileSystemDnd } from './use-file-system-dnd'
|
||||
@@ -354,13 +356,30 @@ export default function ShareManager({
|
||||
}
|
||||
/>
|
||||
<div style={{ width: rightSidebarWidth }} className="flex-shrink-0 h-full">
|
||||
<ShareSettingsSidebar
|
||||
shareLink={shareLink}
|
||||
viewMode={displayStyle}
|
||||
onViewModeChange={(mode) => updateShare({ viewMode: mode })}
|
||||
defaultSortOrder={defaultSortOrder}
|
||||
onSortOrderChange={(sortOrder) => updateShare({ defaultSortOrder: sortOrder })}
|
||||
/>
|
||||
<Tabs defaultValue="settings" className="flex h-full min-h-0 flex-col px-2 py-1">
|
||||
<TabsList className="w-full shrink-0">
|
||||
<TabsTrigger value="settings" className="flex-1">
|
||||
{m.settings()}
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="activity" className="flex-1">
|
||||
{m.activity()}
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="settings" className="min-h-0 flex-1 overflow-hidden">
|
||||
<ShareSettingsSidebar
|
||||
shareLink={shareLink}
|
||||
viewMode={displayStyle}
|
||||
onViewModeChange={(mode) => updateShare({ viewMode: mode })}
|
||||
defaultSortOrder={defaultSortOrder}
|
||||
onSortOrderChange={(sortOrder) =>
|
||||
updateShare({ defaultSortOrder: sortOrder })
|
||||
}
|
||||
/>
|
||||
</TabsContent>
|
||||
<TabsContent value="activity" className="flex min-h-0 flex-1 flex-col">
|
||||
<ShareActivityList shareId={shareId} />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { Download, AlertCircle } from 'lucide-react'
|
||||
import React, { useImperativeHandle } from 'react'
|
||||
@@ -18,10 +19,7 @@ export const DefaultViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
if (!key || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, file.id, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: file.id },
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||
import { getBestTranscode } from '@/ui/lib/media'
|
||||
@@ -118,10 +119,7 @@ export const CompareImagePane = forwardRef<ComparePaneHandle, CompareImagePanePr
|
||||
if (!key || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, file.id, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: file.id },
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import { getBestTranscode } from '@/ui/lib/media'
|
||||
import { copyImageToClipboard } from '@/ui/lib/clipboard'
|
||||
import React, { useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||
@@ -106,10 +107,7 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
if (!key || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, file.id, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: file.id },
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ import React, { useCallback, useEffect, useImperativeHandle, useRef, useState }
|
||||
import { AlertCircle, Loader2 } from 'lucide-react'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import type { FileViewerProps, MediaController } from '../types'
|
||||
@@ -65,10 +66,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
if (!key || !data.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: data.id },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, data.id, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: data.id },
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
@@ -383,10 +384,7 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
if (!key || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, file.id, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: file.id },
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
@@ -452,10 +453,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
if (!dlKey || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key: dlKey },
|
||||
})
|
||||
? await requestShareDownload(shareId, file.id, dlKey)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key: dlKey, assetId: file.id },
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { requestShareDownload } from '@/ui/api/share-download'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { Play, AudioLines, Loader2, AlertCircle } from 'lucide-react'
|
||||
@@ -901,10 +902,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const handleDownload = async (key: string) => {
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: data.id! },
|
||||
json: { key },
|
||||
})
|
||||
? await requestShareDownload(shareId, data.id!, key)
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: data.id! },
|
||||
})
|
||||
|
||||
@@ -306,6 +306,8 @@ function ShareDialogHarness(): ReactElement {
|
||||
rootFolderId: 'rf-1',
|
||||
projectId: 'proj-1',
|
||||
isExpired: false,
|
||||
views: 0,
|
||||
lastViewedAt: null,
|
||||
watermarkConfigId: null,
|
||||
watermarkStatus: 'disabled',
|
||||
watermarkConfig: null,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { createContext, useContext, useEffect, useRef, type ReactNode } from 'react'
|
||||
import { client } from '@/ui/api/client'
|
||||
|
||||
type ShareVisit = {
|
||||
shareId: string
|
||||
assetId?: string
|
||||
submitted: boolean
|
||||
entered: boolean
|
||||
}
|
||||
|
||||
const ShareVisitContext = createContext<{ current: ShareVisit | null } | null>(null)
|
||||
|
||||
export function PublicShareViewProvider({ children }: { children: ReactNode }) {
|
||||
const visit = useRef<ShareVisit | null>(null)
|
||||
return <ShareVisitContext.Provider value={visit}>{children}</ShareVisitContext.Provider>
|
||||
}
|
||||
|
||||
export function usePublicShareView({
|
||||
shareId,
|
||||
assetId,
|
||||
password,
|
||||
ready,
|
||||
}: {
|
||||
shareId: string
|
||||
assetId?: string
|
||||
password: string
|
||||
ready: boolean
|
||||
}) {
|
||||
const visit = useContext(ShareVisitContext)
|
||||
useEffect(() => {
|
||||
if (!visit) return
|
||||
if (visit.current?.shareId !== shareId || visit.current.assetId !== assetId) {
|
||||
visit.current = {
|
||||
shareId,
|
||||
assetId,
|
||||
submitted: false,
|
||||
entered: visit.current?.shareId === shareId && visit.current.entered,
|
||||
}
|
||||
}
|
||||
if (!ready || visit.current.submitted) return
|
||||
|
||||
const activeVisit = visit.current
|
||||
const isShareEntry = !activeVisit.entered
|
||||
activeVisit.submitted = true
|
||||
activeVisit.entered = true
|
||||
void client.api.shares[':shareId'].views
|
||||
.$post(
|
||||
{ param: { shareId }, json: { assetId, isShareEntry } },
|
||||
{ headers: { 'x-share-password': password } },
|
||||
)
|
||||
.then((response) => {
|
||||
if (!response.ok) throw new Error(`Share view returned ${response.status}`)
|
||||
})
|
||||
.catch((error: unknown) => console.error('Failed to record share view', error))
|
||||
}, [assetId, password, ready, shareId, visit])
|
||||
}
|
||||
@@ -1528,6 +1528,17 @@
|
||||
"dependency_added": "Dependency added",
|
||||
"dependency_removed": "Dependency removed",
|
||||
"comments": "Comments",
|
||||
"share_views": "Views",
|
||||
"share_last_viewed": "Last Viewed",
|
||||
"share_never_viewed": "Never viewed",
|
||||
"share_view_activity": "View Activity",
|
||||
"share_activity_opened_share_link": "Opened Share Link",
|
||||
"share_activity_viewed_asset": "Viewed Asset",
|
||||
"share_activity_downloaded": "Downloaded",
|
||||
"share_activity_commented": "Commented",
|
||||
"share_activity_deleted_asset": "Deleted asset",
|
||||
"share_activity_load_failed": "Could not load share activity",
|
||||
"share_download_start_failed": "Some downloads could not be started",
|
||||
"activity": "Activity",
|
||||
"activity_and_runs": "Activity & Runs",
|
||||
"add_comment": "Add Comment",
|
||||
|
||||
@@ -1465,6 +1465,17 @@
|
||||
"search_tasks_to_link": "연결할 작업 검색...",
|
||||
"dependency_added": "의존 관계를 추가했습니다",
|
||||
"dependency_removed": "의존 관계를 제거했습니다",
|
||||
"share_views": "조회 수",
|
||||
"share_last_viewed": "마지막 조회",
|
||||
"share_never_viewed": "조회 기록 없음",
|
||||
"share_view_activity": "활동 보기",
|
||||
"share_activity_opened_share_link": "공유 링크 열기",
|
||||
"share_activity_viewed_asset": "에셋 보기",
|
||||
"share_activity_downloaded": "다운로드",
|
||||
"share_activity_commented": "댓글 작성",
|
||||
"share_activity_deleted_asset": "삭제된 에셋",
|
||||
"share_activity_load_failed": "공유 활동을 불러올 수 없습니다",
|
||||
"share_download_start_failed": "일부 다운로드를 시작할 수 없습니다",
|
||||
"activity": "활동",
|
||||
"activity_and_runs": "활동 및 실행",
|
||||
"add_comment": "댓글 추가",
|
||||
|
||||
@@ -1528,6 +1528,17 @@
|
||||
"dependency_added": "依赖已添加",
|
||||
"dependency_removed": "依赖已移除",
|
||||
"comments": "评论",
|
||||
"share_views": "浏览次数",
|
||||
"share_last_viewed": "最近浏览",
|
||||
"share_never_viewed": "从未浏览",
|
||||
"share_view_activity": "查看活动",
|
||||
"share_activity_opened_share_link": "打开了分享链接",
|
||||
"share_activity_viewed_asset": "浏览了素材",
|
||||
"share_activity_downloaded": "下载了素材",
|
||||
"share_activity_commented": "发表了评论",
|
||||
"share_activity_deleted_asset": "已删除的素材",
|
||||
"share_activity_load_failed": "无法加载分享活动",
|
||||
"share_download_start_failed": "部分下载无法启动",
|
||||
"activity": "活动",
|
||||
"activity_and_runs": "活动与运行",
|
||||
"add_comment": "添加评论",
|
||||
|
||||
@@ -6,6 +6,15 @@ import { m } from '@/ui/paraglide/messages.js'
|
||||
import { copyToClipboard } from '@/ui/lib/clipboard'
|
||||
import { Switch } from '@/ui/components/ui/switch'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { ShareActivityList } from '@/ui/components/share-activity-list'
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetDescription,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
} from '@/ui/components/ui/sheet'
|
||||
import { formatTimeAgo } from '@/ui/lib/time'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
@@ -24,7 +33,7 @@ import {
|
||||
useSuspenseQuery,
|
||||
} from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { Copy, ExternalLink, Calendar, Link2, Share2, Loader2 } from 'lucide-react'
|
||||
import { useInView } from 'react-intersection-observer'
|
||||
import { toast } from 'sonner'
|
||||
@@ -56,6 +65,8 @@ function SharesPage() {
|
||||
|
||||
const rootFolderId = projectInfo?.rootFolder
|
||||
const [leftSidebarWidth, setLeftSidebarWidth] = useState(240)
|
||||
const [activityShare, setActivityShare] = useState<ShareLinkInfo | null>(null)
|
||||
const activityTrigger = useRef<HTMLButtonElement | null>(null)
|
||||
|
||||
const {
|
||||
shareConfigLeftSidebarCollapsed: isLeftSidebarCollapsed,
|
||||
@@ -185,6 +196,11 @@ function SharesPage() {
|
||||
<TableHead className="font-semibold px-4 py-3 w-[150px]">
|
||||
{m.created_date()}
|
||||
</TableHead>
|
||||
<TableHead className="font-semibold px-4 py-3">{m.share_views()}</TableHead>
|
||||
<TableHead className="font-semibold px-4 py-3">
|
||||
{m.share_last_viewed()}
|
||||
</TableHead>
|
||||
<TableHead className="font-semibold px-4 py-3">{m.activity()}</TableHead>
|
||||
<TableHead className="font-semibold px-4 py-3 text-right w-[120px]">
|
||||
{m.active()}
|
||||
</TableHead>
|
||||
@@ -260,6 +276,33 @@ function SharesPage() {
|
||||
<span>{new Date(share.createdAt).toLocaleDateString()}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="px-4 py-3.5 tabular-nums">
|
||||
{share.views.toLocaleString()}
|
||||
</TableCell>
|
||||
<TableCell className="px-4 py-3.5 text-sm text-muted-foreground">
|
||||
{share.lastViewedAt ? (
|
||||
<time
|
||||
dateTime={share.lastViewedAt}
|
||||
title={new Date(share.lastViewedAt).toLocaleString()}
|
||||
>
|
||||
{formatTimeAgo(share.lastViewedAt)}
|
||||
</time>
|
||||
) : (
|
||||
m.share_never_viewed()
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="px-4 py-3.5">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={(event) => {
|
||||
activityTrigger.current = event.currentTarget
|
||||
setActivityShare(share)
|
||||
}}
|
||||
>
|
||||
{m.share_view_activity()}
|
||||
</Button>
|
||||
</TableCell>
|
||||
<TableCell className="px-4 py-3.5 text-right">
|
||||
<div className="flex justify-end">
|
||||
<Switch
|
||||
@@ -283,6 +326,21 @@ function SharesPage() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Sheet open={!!activityShare} onOpenChange={(open) => !open && setActivityShare(null)}>
|
||||
<SheetContent
|
||||
className="gap-0 sm:max-w-md"
|
||||
onCloseAutoFocus={(event) => {
|
||||
event.preventDefault()
|
||||
activityTrigger.current?.focus()
|
||||
}}
|
||||
>
|
||||
<SheetHeader className="border-b pr-12">
|
||||
<SheetTitle>{m.activity()}</SheetTitle>
|
||||
<SheetDescription className="truncate">{activityShare?.name}</SheetDescription>
|
||||
</SheetHeader>
|
||||
{activityShare && <ShareActivityList shareId={activityShare.id} />}
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { createFileRoute, Outlet } from '@tanstack/react-router'
|
||||
import { PublicShareViewProvider } from '@/ui/hooks/use-public-share-view'
|
||||
|
||||
function ShareLayout() {
|
||||
return <Outlet />
|
||||
return (
|
||||
<PublicShareViewProvider>
|
||||
<Outlet />
|
||||
</PublicShareViewProvider>
|
||||
)
|
||||
}
|
||||
|
||||
export const Route = createFileRoute('/share/$shareId')({
|
||||
|
||||
Reference in New Issue
Block a user