-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetafiles.ts
111 lines (95 loc) · 4.7 KB
/
metafiles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { createSelector, EntityId } from '@reduxjs/toolkit';
import { PathLike } from 'fs-extra';
import { isDescendant, isEqualPaths } from '../../containers/io';
import { isDefined } from '../../containers/utils';
import { Card } from '../slices/cards';
import { FilebasedMetafile, isFilebasedMetafile, isFileMetafile, Metafile, metafileAdapter, VersionedMetafile, VirtualMetafile } from '../slices/metafiles';
import { RootState } from '../store';
import { CardType, FilesystemStatus, UUID } from '../types';
const selectors = metafileAdapter.getSelectors<RootState>(state => state.metafiles);
const selectByIds = createSelector(
selectors.selectEntities,
(_state: RootState, ids: EntityId[]) => ids,
(metafiles, ids) => ids.map(id => metafiles[id]).filter((c): c is Metafile => c !== undefined)
)
const selectByFilepath = createSelector(
selectors.selectAll,
(_state: RootState, filepath: PathLike) => filepath,
(_state: RootState, _filepath: PathLike, handlers?: CardType[]) => handlers,
(metafiles, filepath, handlers) => handlers
? metafiles.filter(m => isDefined(m.path) && isEqualPaths(m.path, filepath) && handlers.includes(m.handler)) as FilebasedMetafile[]
: metafiles.filter(m => isDefined(m.path) && isEqualPaths(m.path, filepath)) as FilebasedMetafile[]
);
const selectByFilepaths = createSelector(
selectors.selectAll,
(_state: RootState, filepaths: PathLike[]) => filepaths,
(metafiles, filepaths) => metafiles.filter(m =>
filepaths.find(f => m.path && isEqualPaths(m.path, f))) as FilebasedMetafile[]
);
const selectByRoot = createSelector(
selectors.selectAll,
(_state: RootState, root: PathLike) => root,
(metafiles, root) => metafiles.filter(m =>
isFilebasedMetafile(m) &&
isDescendant(root, m.path)
) as FilebasedMetafile[]
);
const selectByRepo = createSelector(
selectors.selectAll,
(_state: RootState, repo: UUID) => repo,
(metafiles, repo) => metafiles.filter(m => m.repo === repo) as VersionedMetafile[]
);
const selectByBranch = createSelector(
selectors.selectAll,
(_state: RootState, branch: UUID) => branch,
(_state: RootState, _branch: UUID, filepath?: PathLike) => filepath,
(metafiles, filepath, branch) => filepath
? metafiles.filter(m => m.path === filepath && m.branch === branch) as VersionedMetafile[]
: metafiles.filter(m => m.branch === branch) as VersionedMetafile[]
);
const selectByVirtual = createSelector(
selectors.selectAll,
(_state: RootState, name: string) => name,
(_state: RootState, _name: string, handler: string) => handler,
(metafiles, name, handler) => metafiles.filter(m => m.name === name && m.handler === handler) as VirtualMetafile[]
);
const selectByState = createSelector(
selectors.selectAll,
(_state: RootState, state: FilesystemStatus) => state,
(metafiles, state) => metafiles.filter(m => m.state === state) as FilebasedMetafile[]
);
const selectByCards = createSelector(
selectors.selectEntities,
(_state: RootState, cards: Card[]) => cards,
(metafiles, cards) => cards.map(card => metafiles[card.metafile])
);
const selectByConflicted = createSelector(
selectors.selectAll,
(_state: RootState, repoId: UUID) => repoId,
(metafiles, repo) => metafiles.filter(m =>
m.repo === repo && m.conflicts !== undefined && m.conflicts.length > 0 && ['Editor', 'Explorer'].includes(m.handler)) as VersionedMetafile[]
);
const selectStagedFieldsByRepo = createSelector(
selectors.selectAll,
(_state: RootState, repoId: UUID) => repoId,
(metafiles, repo) => metafiles.filter(isFileMetafile)
.filter(m => m.repo === repo && m.status && ['added', 'modified', 'deleted'].includes(m.status))
.map(m => { return { id: m.id, repo: m.repo, branch: m.branch, status: m.status } })
)
const selectStagedByBranch = createSelector(
selectors.selectAll,
(_state: RootState, branchId: UUID) => branchId,
(metafiles, branch) => metafiles.filter(isFileMetafile)
.filter(m => m.branch === branch && m.status && ['added', 'modified', 'deleted'].includes(m.status))
)
const selectUnstagedByBranch = createSelector(
selectors.selectAll,
(_state: RootState, branchId: UUID) => branchId,
(metafiles, branch) => metafiles.filter(isFileMetafile)
.filter(m => m.branch === branch && m.status && ['*absent', '*added', '*undeleted', '*modified', '*deleted'].includes(m.status))
)
const metafileSelectors = {
...selectors, selectByIds, selectByFilepath, selectByFilepaths, selectByRoot, selectByRepo, selectByBranch,
selectByVirtual, selectByState, selectByCards, selectByConflicted, selectStagedFieldsByRepo, selectStagedByBranch, selectUnstagedByBranch
};
export default metafileSelectors;