-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: folder state managment and sync * fix: looping noop * fix: folder sorting and filtering Co-authored-by: Giuliano Caregnato <[email protected]>
- v10.0.0
- v9.0.1
- v9.0.0
- v8.0.3
- v8.0.2
- v8.0.1
- v8.0.0
- v7.0.1
- v7.0.0
- v6.0.0
- v5.2.0
- v5.1.0
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.0.1
- v4.0.0
- v3.4.3
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-beta.0
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.1
- v1.1.0
- v1.0.0
- v0.4.46
- v0.4.45
- v0.4.44
- v0.4.43
- v0.4.42
- v0.4.41
- v0.4.40
- v0.4.39
- v0.4.38
- v0.4.37
- v0.4.36
- v0.4.35
- v0.4.34
- v0.4.33
- v0.4.32
- v0.4.31
- v0.4.30
- v0.4.29
- v0.4.28
- v0.4.27
- v0.4.26
- v0.4.25
- v0.4.24
- v0.4.23
- v0.4.22
- v0.4.21
- v0.4.20
- v0.4.19
- v0.4.18
- v0.4.17
- v0.4.16
- v0.4.15
- v0.4.14
- v0.4.13
- v0.4.12
- v0.4.11
- v0.4.10
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
Showing
19 changed files
with
815 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { ComponentType, useMemo } from 'react'; | ||
import { | ||
Folders, | ||
Searches, | ||
SearchFolder, | ||
Folder, | ||
AccordionFolder, | ||
FolderView | ||
} from '../../../types'; | ||
import { useFolderStore } from './store'; | ||
import { filterNodes, folderViewFilter, mapNodes, sortFolders } from './utils'; | ||
|
||
// FOLDERS | ||
export const useFolder = (id: string): Folder | undefined => useFolderStore((s) => s.folders?.[id]); | ||
export const getFolder = (id: string): Folder | undefined => | ||
useFolderStore.getState()?.folders?.[id]; | ||
export const useFolders = (): Folders => useFolderStore((s) => s.folders); | ||
export const getFolders = (): Folders => useFolderStore.getState().folders; | ||
|
||
// ROOTS | ||
export const useRoot = (id: string): Folder | undefined => useFolderStore((s) => s.roots?.[id]); | ||
export const getRoot = (id: string): Folder | undefined => useFolderStore.getState().roots?.[id]; | ||
export const useRoots = (): Folders => useFolderStore((s) => s.roots); | ||
export const getRoots = (): Folders => useFolderStore.getState().roots; | ||
|
||
// ROOTS BY VIEW | ||
export const useRootByUser = (userId: string): Folder | SearchFolder | Record<string, never> => | ||
useFolderStore((s) => (s.roots ? s.roots[userId] : {})); | ||
export const getRootByUser = (userId: string): Folder | SearchFolder | Record<string, never> => { | ||
const folders = useFolderStore.getState(); | ||
return folders.roots ? folders.roots[userId] : {}; | ||
}; | ||
|
||
// SEARCHES | ||
|
||
export const useSearch = (id: string): SearchFolder | undefined => | ||
useFolderStore((s) => s.searches?.[id]); | ||
export const getSearch = (id: string): SearchFolder | undefined => | ||
useFolderStore.getState().searches[id]; | ||
export const useSearches = (): Searches => useFolderStore((s) => s.searches); | ||
export const getSearches = (): Searches => useFolderStore.getState().searches; | ||
|
||
// Accordion-ize | ||
|
||
export const useFoldersByView = (view: FolderView): Array<Folder> => { | ||
const roots = useRoots(); | ||
const filtered = useMemo( | ||
() => (roots ? filterNodes<Folder>(Object.values(roots), folderViewFilter(view)) : []), | ||
[roots, view] | ||
); | ||
return filtered; | ||
}; | ||
|
||
export const useFoldersAccordionByView = ( | ||
view: FolderView, | ||
customComponent: ComponentType<{ folder: Folder }> | ||
): Array<AccordionFolder> => { | ||
const roots = useRoots(); | ||
const mapped = useMemo( | ||
() => | ||
roots | ||
? mapNodes<Folder, AccordionFolder>(Object.values(roots), { | ||
mapFunction: (f) => ({ | ||
id: f.id, | ||
label: f.name, | ||
customComponent, | ||
items: [], | ||
folder: f | ||
}), | ||
filterFunction: folderViewFilter(view), | ||
recursionKey: 'items', | ||
sortFunction: sortFolders | ||
}) | ||
: [], | ||
[customComponent, roots, view] | ||
); | ||
return mapped; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export * from './store'; | ||
export * from './hooks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import create, { StoreApi, UseBoundStore } from 'zustand'; | ||
import { FolderState } from '../../../types'; | ||
import { folderWorker } from '../../workers'; | ||
|
||
export const useFolderStore = create<FolderState>(() => ({ | ||
folders: {}, | ||
roots: {}, | ||
searches: {} | ||
})) as UseBoundStore<FolderState, StoreApi<FolderState>>; | ||
|
||
folderWorker.onmessage = ({ data }): void => { | ||
useFolderStore.setState(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import { sortBy } from 'lodash'; | ||
import { Folder, FolderView, LinkFolder, TreeNode } from '../../../types'; | ||
import { FOLDERS, ROOT_NAME } from '../../constants'; | ||
|
||
const hasId = (f: Folder, id: string): boolean => f.id.split(':').includes(id); | ||
const getOriginalId = (f: Folder): string => { | ||
const parts = f.id.split(':'); | ||
return parts[1] ?? parts[0]; | ||
}; | ||
export const sortFolders = (f: Folder): string => { | ||
const id = getOriginalId(f); | ||
if (id === FOLDERS.TRASH) { | ||
return '~'; | ||
} | ||
// should work fine | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return id < 17 ? id : f.name.toLowerCase(); | ||
}; | ||
|
||
export const isTrash = (f: Folder): boolean => hasId(f, FOLDERS.TRASH); | ||
|
||
export const isRoot = (f: Folder): boolean => | ||
f.id === FOLDERS.USER_ROOT || (f as LinkFolder).oname === ROOT_NAME; | ||
|
||
export const folderViewFilter = | ||
(v: FolderView) => | ||
(f: Folder, r?: boolean): boolean => | ||
f.view === v || isTrash(f) || (isRoot(f) && !r); | ||
|
||
export const filterNodes = <T>( | ||
children: TreeNode<T>[], | ||
f: (i: TreeNode<T>) => boolean | ||
): TreeNode<T>[] => | ||
children.filter(f).map((i) => ({ ...i, children: filterNodes<TreeNode<T>>(i.children, f) })); | ||
|
||
type MapNodesOptions<T, U> = { | ||
mapFunction: (i: TreeNode<T>) => U; | ||
filterFunction: (i: TreeNode<T>, inRecursion?: boolean) => boolean; | ||
recursionKey: keyof U; | ||
sortFunction: (i: TreeNode<T>) => number | string; | ||
}; | ||
export const mapNodes = <T, U>( | ||
children: TreeNode<T>[], | ||
{ mapFunction, filterFunction, recursionKey, sortFunction }: MapNodesOptions<T, U>, | ||
inRecursion?: boolean | ||
): U[] => | ||
sortBy(children, sortFunction).reduce((acc, folder) => { | ||
if (filterFunction(folder, inRecursion)) { | ||
acc.push({ | ||
...mapFunction(folder), | ||
[recursionKey]: mapNodes<TreeNode<T>, U>( | ||
folder.children, | ||
{ | ||
mapFunction, | ||
filterFunction, | ||
recursionKey, | ||
sortFunction | ||
}, | ||
true | ||
) | ||
}); | ||
} | ||
return acc; | ||
}, [] as U[]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.