Skip to content

Commit

Permalink
refactor(renterd): split out uploads manager context
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jan 16, 2025
1 parent e7b9586 commit 110e062
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { useObjectStats } from '@siafoundation/renterd-react'
import { useFilesManager } from '../../../contexts/filesManager'
import { useFilesDirectory } from '../../../contexts/filesDirectory'
import { useFilesFlat } from '../../../contexts/filesFlat'
import { useUploadsManager } from '../../../contexts/uploadsManager'

export function FilesStatsMenuCount() {
const { isViewingABucket, uploadsList, activeExplorerMode } =
useFilesManager()
const { isViewingABucket, activeExplorerMode } = useFilesManager()
const { uploadsList } = useUploadsManager()
const { datasetPageTotal: directoryPageTotal } = useFilesDirectory()
const { datasetPageTotal: flatPageTotal } = useFilesFlat()
const datasetPageTotal =
Expand Down
4 changes: 3 additions & 1 deletion apps/renterd/components/FilesDirectory/FilesActionsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { useFilesManager } from '../../contexts/filesManager'
import { FilesViewDropdownMenu } from '../Files/FilesViewDropdownMenu'
import { useDialog } from '../../contexts/dialog'
import { useCanUpload } from '../Files/useCanUpload'
import { useUploadsManager } from '../../contexts/uploadsManager'
// esm compat
const { useDropzone } = reactDropzone

export function FilesActionsMenu() {
const { openDialog } = useDialog()
const { uploadFiles, isViewingBuckets } = useFilesManager()
const { isViewingBuckets } = useFilesManager()
const { uploadFiles } = useUploadsManager()

const canUpload = useCanUpload()

Expand Down
3 changes: 2 additions & 1 deletion apps/renterd/components/FilesDirectory/FilesExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { EmptyState } from './EmptyState'
import { useCanUpload } from '../Files/useCanUpload'
import { useFilesManager } from '../../contexts/filesManager'
import { pluralize } from '@siafoundation/units'
import { useUploadsManager } from '../../contexts/uploadsManager'

export function FilesExplorer() {
const {
uploadFiles,
sortField,
sortDirection,
sortableColumns,
toggleSort,
isViewingBuckets,
visibleColumns,
} = useFilesManager()
const { uploadFiles } = useUploadsManager()
const {
datasetPage,
datasetPageTotal,
Expand Down
29 changes: 16 additions & 13 deletions apps/renterd/config/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FilesManagerProvider } from '../contexts/filesManager'
import { FilesDirectoryProvider } from '../contexts/filesDirectory'
import { UploadsProvider } from '../contexts/uploads'
import { AlertsProvider } from '../contexts/alerts'
import { UploadsManagerProvider } from '../contexts/uploadsManager'

type Props = {
children: React.ReactNode
Expand All @@ -25,20 +26,22 @@ export function Providers({ children }: Props) {
<ContractsProvider>
<HostsProvider>
<FilesManagerProvider>
<UploadsProvider>
<FilesDirectoryProvider>
<FilesFlatProvider>
<KeysProvider>
<AlertsProvider>
{/* this is here so that dialogs can use all the other providers,
<UploadsManagerProvider>
<UploadsProvider>
<FilesDirectoryProvider>
<FilesFlatProvider>
<KeysProvider>
<AlertsProvider>
{/* this is here so that dialogs can use all the other providers,
and the other providers can trigger dialogs */}
<Dialogs />
{children}
</AlertsProvider>
</KeysProvider>
</FilesFlatProvider>
</FilesDirectoryProvider>
</UploadsProvider>
<Dialogs />
{children}
</AlertsProvider>
</KeysProvider>
</FilesFlatProvider>
</FilesDirectoryProvider>
</UploadsProvider>
</UploadsManagerProvider>
</FilesManagerProvider>
</HostsProvider>
</ContractsProvider>
Expand Down
3 changes: 2 additions & 1 deletion apps/renterd/contexts/filesManager/dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { useFilesManager } from '.'
import { useEffect } from 'react'
import { Maybe } from '@siafoundation/types'
import { useUploadsManager } from '../uploadsManager'

type Props = {
id: string
Expand All @@ -27,13 +28,13 @@ export function useDataset({ id, objects }: Props) {
activeBucket,
activeBucketName,
fileNamePrefixFilter,
uploadsList,
sortDirection,
sortField,
activeDirectoryPath,
buckets,
setActiveDirectory,
} = useFilesManager()
const { uploadsList } = useUploadsManager()
const { dataset: allContracts } = useContracts()
const response = useSWR<Maybe<ObjectData[]>>(
objects.isValidating || buckets.isValidating
Expand Down
8 changes: 0 additions & 8 deletions apps/renterd/contexts/filesManager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getKeyFromPath,
pathSegmentsToPath,
} from '../../lib/paths'
import { useUploads } from './uploads'
import { useBuckets } from '@siafoundation/renterd-react'
import { routes } from '../../config/routes'
import useLocalStorageState from 'use-local-storage-state'
Expand Down Expand Up @@ -93,10 +92,6 @@ function useFilesManagerMain() {
[router, activeDirectory]
)

const { uploadFiles, uploadsMap, uploadsList } = useUploads({
activeDirectoryPath,
})

const isViewingBuckets = activeDirectory.length === 0
const isViewingRootOfABucket = activeDirectory.length === 1
const isViewingABucket = activeDirectory.length > 0
Expand Down Expand Up @@ -220,9 +215,6 @@ function useFilesManagerMain() {
setActiveDirectoryAndFileNamePrefix,
activeDirectoryPath,
navigateToModeSpecificFiltering,
uploadFiles,
uploadsMap,
uploadsList,
configurableColumns,
visibleColumnIds,
visibleColumns,
Expand Down
15 changes: 0 additions & 15 deletions apps/renterd/contexts/filesManager/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bucket } from '@siafoundation/renterd-types'
import { FullPath } from '../../lib/paths'
import { MultiSelect, TableColumn } from '@siafoundation/design-system'
import { MultipartUpload } from '../../lib/multipartUpload'
import { MouseEvent } from 'react'

export type ObjectType = 'bucket' | 'directory' | 'file'
Expand Down Expand Up @@ -81,17 +80,3 @@ export const sortOptions: { id: SortField; label: string; category: string }[] =
]

export type ExplorerMode = 'directory' | 'flat'

export type UploadStatus = 'queued' | 'uploading' | 'processing'

export type ObjectUploadData = ObjectData & {
multipartId?: string
multipartUpload?: MultipartUpload
uploadStatus: UploadStatus
uploadAbort?: () => Promise<void>
uploadFile?: File
remote?: boolean
createdAt: string
}

export type UploadsMap = Record<string, ObjectUploadData>
2 changes: 1 addition & 1 deletion apps/renterd/contexts/uploads/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TableColumn } from '@siafoundation/design-system'
import { ObjectUploadData } from '../filesManager/types'
import { ObjectUploadData } from '../uploadsManager/types'

export type TableColumnId = 'actions' | 'path' | 'status' | 'size' | 'createdAt'

Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/uploads/useLocalUploads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
import { useMemo } from 'react'
import { columnsDefaultVisible, defaultSortField, sortOptions } from './types'
import { columns } from './columns'
import { useFilesManager } from '../filesManager'
import { ObjectUploadData } from '../filesManager/types'
import { ObjectUploadData } from '../uploadsManager/types'
import { Maybe } from '@siafoundation/types'
import { useUploadsManager } from '../uploadsManager'

const defaultLimit = 500

export function useLocalUploads() {
const { uploadsList } = useFilesManager()
const { uploadsList } = useUploadsManager()
const { limit, offset } = usePaginationOffset(defaultLimit)

const datasetPage = useMemo<Maybe<ObjectUploadData[]>>(() => {
Expand Down
7 changes: 4 additions & 3 deletions apps/renterd/contexts/uploads/useRemoteUploads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import { columnsDefaultVisible, defaultSortField, sortOptions } from './types'
import { columns } from './columns'
import { join, getFilename } from '../../lib/paths'
import { useFilesManager } from '../filesManager'
import { ObjectUploadData } from '../filesManager/types'
import { ObjectUploadData } from '../uploadsManager/types'
import { MultipartUploadListUploadsPayload } from '@siafoundation/renterd-types'
import { maybeFromNullishArrayResponse } from '@siafoundation/react-core'
import { Maybe, Nullable } from '@siafoundation/types'
import { getUploadId } from '../filesManager/uploads'
import { getUploadId, useUploadsManager } from '../uploadsManager'

const defaultLimit = 500

export function useRemoteUploads() {
const { uploadsMap, activeBucket } = useFilesManager()
const { activeBucket } = useFilesManager()
const { uploadsMap } = useUploadsManager()
const { limit, marker } = usePaginationMarker(defaultLimit)
const markers = useMarkersFromParam(marker)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { triggerErrorToast } from '@siafoundation/design-system'
import {
throttle,
Expand All @@ -14,7 +16,14 @@ import {
} from '@siafoundation/renterd-react'
import { Bucket, busObjectsRoute } from '@siafoundation/renterd-types'
import { MiBToBytes, minutesInMilliseconds } from '@siafoundation/units'
import { useCallback, useEffect, useMemo, useRef } from 'react'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
} from 'react'
import { MultipartUpload } from '../../lib/multipartUpload'
import {
FullPath,
Expand All @@ -24,18 +33,16 @@ import {
} from '../../lib/paths'
import { ObjectUploadData, UploadsMap } from './types'
import { useWarnActiveUploadsOnClose } from './useWarnActiveUploadsOnClose'
import { useFilesManager } from '../filesManager'

const maxConcurrentUploads = 5
const maxConcurrentPartsPerUpload = 5
const getMultipartUploadPartSize = (minShards: number) =>
MiBToBytes(4).times(minShards)
const checkAndStartUploadsInterval = 500

type Props = {
activeDirectoryPath: string
}

export function useUploads({ activeDirectoryPath }: Props) {
function useUploadsManagerMain() {
const { activeDirectoryPath } = useFilesManager()
const buckets = useBuckets()
const mutate = useMutate()
const workerUploadPart = useMultipartUploadPart()
Expand Down Expand Up @@ -348,6 +355,24 @@ export function useUploads({ activeDirectoryPath }: Props) {
}
}

export type UploadsManagerState = ReturnType<typeof useUploadsManagerMain>

const UploadsManagerContext = createContext({} as UploadsManagerState)
export const useUploadsManager = () => useContext(UploadsManagerContext)

type Props = {
children: React.ReactNode
}

export function UploadsManagerProvider({ children }: Props) {
const state = useUploadsManagerMain()
return (
<UploadsManagerContext.Provider value={state}>
{children}
</UploadsManagerContext.Provider>
)
}

export function getUploadId(path: FullPath) {
return path
}
16 changes: 16 additions & 0 deletions apps/renterd/contexts/uploadsManager/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ObjectData } from '../filesManager/types'
import { MultipartUpload } from '../../lib/multipartUpload'

export type UploadStatus = 'queued' | 'uploading' | 'processing'

export type ObjectUploadData = ObjectData & {
multipartId?: string
multipartUpload?: MultipartUpload
uploadStatus: UploadStatus
uploadAbort?: () => Promise<void>
uploadFile?: File
remote?: boolean
createdAt: string
}

export type UploadsMap = Record<string, ObjectUploadData>

0 comments on commit 110e062

Please sign in to comment.