@@ -11,9 +11,7 @@ import RecentIcon from '#/assets/recent.svg'
11
11
import { useUser } from '#/providers/AuthProvider'
12
12
13
13
import { useEventCallback } from '#/hooks/eventCallbackHooks'
14
- import { useOffline } from '#/hooks/offlineHooks'
15
- import { useSearchParamsState } from '#/hooks/searchParamsStateHooks'
16
- import { pickBackend , useLocalBackend , useRemoteBackend } from '#/providers/BackendProvider'
14
+ import { useLocalBackend } from '#/providers/BackendProvider'
17
15
import { useLocalStorageState } from '#/providers/LocalStorageProvider'
18
16
import { useText } from '#/providers/TextProvider'
19
17
import type Backend from '#/services/Backend'
@@ -22,7 +20,6 @@ import { newDirectoryId } from '#/services/LocalBackend'
22
20
import { organizationIdToDirectoryId } from '#/services/RemoteBackend'
23
21
import { getFileName } from '#/utilities/fileInfo'
24
22
import LocalStorage from '#/utilities/LocalStorage'
25
- import type { ReactNode } from 'react'
26
23
import { createContext , useContext } from 'react'
27
24
import invariant from 'tiny-invariant'
28
25
import { z } from 'zod'
@@ -60,7 +57,7 @@ interface CategoryIdStoreState {
60
57
readonly categoryId : CategoryId | null
61
58
}
62
59
63
- const categoryIdStore = createStore < CategoryIdStoreState > ( ) (
60
+ export const categoryIdStore = createStore < CategoryIdStoreState > ( ) (
64
61
persist (
65
62
( ) : CategoryIdStoreState => ( {
66
63
categoryId : null ,
@@ -276,7 +273,6 @@ export type CategoriesResult = ReturnType<typeof useCategories>
276
273
/**
277
274
* List of all categories.
278
275
*/
279
- // eslint-disable-next-line react-refresh/only-export-components
280
276
export function useCategories ( ) {
281
277
const cloudCategories = useCloudCategoryList ( )
282
278
const localCategories = useLocalCategoryList ( )
@@ -297,7 +293,7 @@ export function useCategories() {
297
293
/**
298
294
* Context value for the categories.
299
295
*/
300
- interface CategoriesContextValue {
296
+ export interface CategoriesContextValue {
301
297
readonly cloudCategories : CloudCategoryResult
302
298
readonly localCategories : LocalCategoryResult
303
299
readonly category : Category
@@ -306,98 +302,11 @@ interface CategoriesContextValue {
306
302
readonly associatedBackend : Backend
307
303
}
308
304
309
- const CategoriesContext = createContext < CategoriesContextValue | null > ( null )
310
-
311
- /**
312
- * Props for the {@link CategoriesProvider}.
313
- */
314
- export interface CategoriesProviderProps {
315
- readonly children : ReactNode | ( ( contextValue : CategoriesContextValue ) => ReactNode )
316
- readonly onCategoryChange ?: ( previousCategory : Category | null , newCategory : Category ) => void
317
- }
318
-
319
- /**
320
- * Provider for the categories.
321
- */
322
- export function CategoriesProvider ( props : CategoriesProviderProps ) : React . JSX . Element {
323
- const { children, onCategoryChange = ( ) => { } } = props
324
-
325
- const { cloudCategories, localCategories, findCategoryById } = useCategories ( )
326
- const localBackend = useLocalBackend ( )
327
- const remoteBackend = useRemoteBackend ( )
328
- const { isOffline } = useOffline ( )
329
-
330
- const [ categoryId , privateSetCategoryId , privateResetCategoryId ] =
331
- useSearchParamsState < CategoryId > (
332
- 'driveCategory' ,
333
- ( ) => {
334
- const savedId = categoryIdStore . getState ( ) . categoryId
335
-
336
- if ( savedId != null && findCategoryById ( savedId ) != null ) {
337
- return savedId
338
- }
339
-
340
- if ( isOffline && localBackend != null ) {
341
- return 'local'
342
- }
343
-
344
- return localBackend != null ? 'local' : 'cloud'
345
- } ,
346
- // This is safe, because we enshure the type inside the function
347
- // eslint-disable-next-line no-restricted-syntax
348
- ( value ) : value is CategoryId => findCategoryById ( value as CategoryId ) != null ,
349
- )
350
-
351
- const setCategoryId = useEventCallback ( ( nextCategoryId : CategoryId ) => {
352
- const previousCategory = findCategoryById ( categoryId )
353
- privateSetCategoryId ( nextCategoryId )
354
- categoryIdStore . setState ( {
355
- categoryId : nextCategoryId ,
356
- } )
357
-
358
- // This is safe, because we know that the result will have the correct type.
359
- // eslint-disable-next-line no-restricted-syntax
360
- onCategoryChange ( previousCategory , findCategoryById ( nextCategoryId ) as Category )
361
- } )
362
-
363
- const resetCategoryId = useEventCallback ( ( replace ?: boolean ) => {
364
- privateResetCategoryId ( replace )
365
- categoryIdStore . setState ( {
366
- categoryId : null ,
367
- } )
368
- } )
369
-
370
- const category = findCategoryById ( categoryId )
371
-
372
- // This usually doesn't happen but if so,
373
- // We reset the category to the default.
374
- if ( category == null ) {
375
- resetCategoryId ( true )
376
- return < > </ >
377
- }
378
-
379
- const backend = pickBackend ( category , remoteBackend , localBackend )
380
-
381
- const contextValue = {
382
- cloudCategories,
383
- localCategories,
384
- category,
385
- setCategory : setCategoryId ,
386
- resetCategory : resetCategoryId ,
387
- associatedBackend : backend ,
388
- } satisfies CategoriesContextValue
389
-
390
- return (
391
- < CategoriesContext . Provider value = { contextValue } >
392
- { typeof children === 'function' ? children ( contextValue ) : children }
393
- </ CategoriesContext . Provider >
394
- )
395
- }
305
+ export const CategoriesContext = createContext < CategoriesContextValue | null > ( null )
396
306
397
307
/**
398
308
* Returns the current category and the associated backend.
399
309
*/
400
- // eslint-disable-next-line react-refresh/only-export-components
401
310
export function useCategory ( ) {
402
311
const { category, associatedBackend } = useCategoriesAPI ( )
403
312
@@ -407,7 +316,6 @@ export function useCategory() {
407
316
/**
408
317
* Gets the api to interact with the categories.
409
318
*/
410
- // eslint-disable-next-line react-refresh/only-export-components
411
319
export function useCategoriesAPI ( ) {
412
320
const context = useContext ( CategoriesContext )
413
321
0 commit comments