Skip to content

Commit 2825dab

Browse files
authored
Merge pull request #85 from unfoldingWord/zach-sort-order-760
Compare/sort browse dialog
2 parents 6f57258 + 2ee3177 commit 2825dab

File tree

9 files changed

+1665
-1226
lines changed

9 files changed

+1665
-1226
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"babel-loader": "^8.0.6",
6868
"babel-plugin-istanbul": "6.0.0",
6969
"coveralls": "3.0.7",
70-
"cypress": "6.2.1",
70+
"cypress": "^6.8.0",
7171
"eslint": "6.6.0",
7272
"eslint-config-prettier": "6.5.0",
7373
"eslint-plugin-chai-friendly": "0.5.0",

src/components/file/helpers.js

+74
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,77 @@ export const saveFile = async ({
7474
});
7575
return response;
7676
};
77+
78+
const REGEX_TSV_BOOK_ABBREVIATION = /^\w*_(\w*)\.tsv$/i;
79+
80+
export const manifestFileComparer = ({
81+
repository, item1, item2,
82+
}) => {
83+
const item1Path = item1?.path;
84+
const item2Path = item2?.path;
85+
86+
let compare = 0;
87+
88+
if (item1Path && item2Path && repository && repository.books)
89+
{
90+
const book1Matches = item1Path.match(REGEX_TSV_BOOK_ABBREVIATION);
91+
const book2Matches = item2Path.match(REGEX_TSV_BOOK_ABBREVIATION);
92+
93+
const isTsvFiles = (book1Matches && book2Matches)?true:false;
94+
if (isTsvFiles)
95+
{
96+
const book1 = book1Matches[1];
97+
const book2 = book2Matches[1];
98+
99+
let iiBook1 = 0;
100+
let iiBook2 = 0;
101+
for (let ii=0; ii < repository.books.length; ii++)
102+
{
103+
if (repository.books[ii].toLowerCase() == book1.toLowerCase())
104+
{
105+
iiBook1 = ii;
106+
}
107+
if (repository.books[ii].toLowerCase() == book2.toLowerCase())
108+
{
109+
iiBook2 = ii;
110+
}
111+
}
112+
113+
if (iiBook1 < iiBook2)
114+
{
115+
compare = -1;
116+
}
117+
else if (iiBook2 < iiBook1)
118+
{
119+
compare = 1;
120+
}
121+
else
122+
{
123+
compare = 0;
124+
}
125+
}
126+
else // BOTH are NOT TSV file: (could be manifest file).
127+
{
128+
if (book1Matches)
129+
{
130+
// Book1 is a TSV, but book2 is a non-TSV file.
131+
return 1;
132+
}
133+
else if (book2Matches)
134+
{
135+
// Book2 is the TSV file; but book1 is NOT.
136+
return -1;
137+
}
138+
else
139+
{
140+
compare = item1Path.localeCompare(item2Path);
141+
}
142+
}
143+
}
144+
else // item1/item2 don't exist:
145+
{
146+
compare = 0;
147+
}
148+
149+
return compare;
150+
};

src/components/tree-blob/Tree.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function Tree({
2828
onBlob,
2929
depth,
3030
filepath,
31+
comparer,
3132
}) {
3233
const classes = useStyles();
3334
const [_tree, setTree] = useState(tree || []);
@@ -40,7 +41,7 @@ function Tree({
4041
const [selectedPath, setSelectedPath] = useState(_selectedPath);
4142

4243
const updateTree = async () => {
43-
const __tree = await fetchTree({ url, config });
44+
const __tree = await fetchTree({ url, config, comparer });
4445
setTree(__tree);
4546
};
4647

src/components/tree-blob/TreeObject.js

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function TreeObject({
3535
onBlob,
3636
depth,
3737
filepath,
38+
comparer,
3839
}) {
3940
const classes = useStyles();
4041
const _filepath = Path.join(filepath || '', path);
@@ -67,6 +68,7 @@ function TreeObject({
6768
onBlob={onBlob}
6869
depth={depth + 1}
6970
filepath={_filepath}
71+
comparer={comparer}
7072
/>
7173
</>
7274
);

src/components/tree-blob/TreeObject.md

+52
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,58 @@ import { TreeObject } from 'gitea-react-toolkit';
3737
</Paper>
3838
```
3939

40+
### URL + Sorting Example
41+
42+
```js
43+
import { Paper } from '@material-ui/core';
44+
import { TreeObject } from 'gitea-react-toolkit';
45+
import {manifestFileComparer} from '../file/helpers';
46+
47+
import repository from './mocks/repositoryData-twl.json';
48+
49+
const tsvManifestFileComparer = ((item1,item2) => {
50+
// Repository instance can be passed from application.
51+
return manifestFileComparer({repository, item1, item2});
52+
});
53+
54+
<Paper>
55+
<TreeObject
56+
path=""
57+
type="tree"
58+
selected
59+
pathSelected="LICENSE.md"
60+
url="https://qa.door43.org/api/v1/repos/unfoldingWord/en_twl/git/trees/master?recursive=true"
61+
onBlob={(data) => alert(JSON.stringify(data, null, 2))}
62+
comparer={tsvManifestFileComparer}
63+
/>
64+
</Paper>
65+
```
66+
67+
### URL + Custom Sorting Example
68+
69+
```js
70+
import { Paper } from '@material-ui/core';
71+
import { TreeObject } from 'gitea-react-toolkit';
72+
73+
<Paper>
74+
<TreeObject
75+
path=""
76+
type="tree"
77+
selected
78+
pathSelected="LICENSE.md"
79+
url="https://qa.door43.org/api/v1/repos/unfoldingWord/en_twl/git/trees/master?recursive=true"
80+
onBlob={(data) => alert(JSON.stringify(data, null, 2))}
81+
comparer={(item1,item2)=>{
82+
if (item1.path < item2.path)
83+
return -1;
84+
if ( item1.path > item2.path)
85+
return 1;
86+
return 0;
87+
}}
88+
/>
89+
</Paper>
90+
```
91+
4092
### Sample Data
4193

4294
```json

src/components/tree-blob/helpers.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import { get } from '../../core';
22

3-
export const fetchTree = async ({ url, config }) => {
3+
export const fetchTree = async ({ url, config, comparer }) => {
44
const _config = {
55
cache: {
66
maxAge: 1 * 2 * 1000, // 2 sec cache override
77
},
88
...config,
99
};
1010
const response = await get({ url, config: _config });
11-
const { tree } = response;
11+
let { tree } = response;
12+
13+
if (comparer)
14+
{
15+
tree = tree.sort(comparer);
16+
}
17+
1218
return tree;
1319
};
1420

0 commit comments

Comments
 (0)