Skip to content

Commit 400f459

Browse files
authored
Merge pull request #43119 from nextcloud/backport/42992/stable28
[stable28] fix(files): Make the navigation reactive to view changes and show also sub routes as active
2 parents ddc22c8 + 732283f commit 400f459

File tree

14 files changed

+128
-73
lines changed

14 files changed

+128
-73
lines changed

apps/files/lib/Activity/Helper.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @author Christoph Wurst <[email protected]>
66
* @author Joas Schilling <[email protected]>
7+
* @author Ferdinand Thiessen <[email protected]>
78
*
89
* @license AGPL-3.0
910
*
@@ -23,30 +24,30 @@
2324
namespace OCA\Files\Activity;
2425

2526
use OCP\Files\Folder;
27+
use OCP\Files\IRootFolder;
28+
use OCP\Files\Node;
2629
use OCP\ITagManager;
2730

2831
class Helper {
2932
/** If a user has a lot of favorites the query might get too slow and long */
3033
public const FAVORITE_LIMIT = 50;
3134

32-
/** @var ITagManager */
33-
protected $tagManager;
34-
35-
/**
36-
* @param ITagManager $tagManager
37-
*/
38-
public function __construct(ITagManager $tagManager) {
39-
$this->tagManager = $tagManager;
35+
public function __construct(
36+
protected ITagManager $tagManager,
37+
protected IRootFolder $rootFolder,
38+
) {
4039
}
4140

4241
/**
43-
* Returns an array with the favorites
42+
* Return an array with nodes marked as favorites
4443
*
45-
* @param string $user
46-
* @return array
44+
* @param string $user User ID
45+
* @param bool $foldersOnly Only return folders (default false)
46+
* @return Node[]
47+
* @psalm-return ($foldersOnly is true ? Folder[] : Node[])
4748
* @throws \RuntimeException when too many or no favorites where found
4849
*/
49-
public function getFavoriteFilePaths($user) {
50+
public function getFavoriteNodes(string $user, bool $foldersOnly = false): array {
5051
$tags = $this->tagManager->load('files', [], false, $user);
5152
$favorites = $tags->getFavorites();
5253

@@ -57,26 +58,45 @@ public function getFavoriteFilePaths($user) {
5758
}
5859

5960
// Can not DI because the user is not known on instantiation
60-
$rootFolder = \OC::$server->getUserFolder($user);
61-
$folders = $items = [];
61+
$userFolder = $this->rootFolder->getUserFolder($user);
62+
$favoriteNodes = [];
6263
foreach ($favorites as $favorite) {
63-
$nodes = $rootFolder->getById($favorite);
64+
$nodes = $userFolder->getById($favorite);
6465
if (!empty($nodes)) {
65-
/** @var \OCP\Files\Node $node */
6666
$node = array_shift($nodes);
67-
$path = substr($node->getPath(), strlen($user . '/files/'));
68-
69-
$items[] = $path;
70-
if ($node instanceof Folder) {
71-
$folders[] = $path;
67+
if (!$foldersOnly || $node instanceof Folder) {
68+
$favoriteNodes[] = $node;
7269
}
7370
}
7471
}
7572

76-
if (empty($items)) {
73+
if (empty($favoriteNodes)) {
7774
throw new \RuntimeException('No favorites', 1);
7875
}
7976

77+
return $favoriteNodes;
78+
}
79+
80+
/**
81+
* Returns an array with the favorites
82+
*
83+
* @param string $user
84+
* @return array
85+
* @throws \RuntimeException when too many or no favorites where found
86+
*/
87+
public function getFavoriteFilePaths(string $user): array {
88+
$userFolder = $this->rootFolder->getUserFolder($user);
89+
$nodes = $this->getFavoriteNodes($user);
90+
$folders = $items = [];
91+
foreach ($nodes as $node) {
92+
$path = $userFolder->getRelativePath($node->getPath());
93+
94+
$items[] = $path;
95+
if ($node instanceof Folder) {
96+
$folders[] = $path;
97+
}
98+
}
99+
80100
return [
81101
'items' => $items,
82102
'folders' => $folders,

apps/files/lib/Controller/ViewController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,14 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
226226

227227
// Get all the user favorites to create a submenu
228228
try {
229-
$favElements = $this->activityHelper->getFavoriteFilePaths($userId);
229+
$userFolder = $this->rootFolder->getUserFolder($userId);
230+
$favElements = $this->activityHelper->getFavoriteNodes($userId, true);
231+
$favElements = array_map(fn (Folder $node) => [
232+
'fileid' => $node->getId(),
233+
'path' => $userFolder->getRelativePath($node->getPath()),
234+
], $favElements);
230235
} catch (\RuntimeException $e) {
231-
$favElements['folders'] = [];
236+
$favElements = [];
232237
}
233238

234239
// If the file doesn't exists in the folder and
@@ -260,7 +265,7 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
260265
$this->initialState->provideInitialState('storageStats', $storageInfo);
261266
$this->initialState->provideInitialState('config', $this->userConfig->getConfigs());
262267
$this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs());
263-
$this->initialState->provideInitialState('favoriteFolders', $favElements['folders'] ?? []);
268+
$this->initialState->provideInitialState('favoriteFolders', $favElements);
264269

265270
// File sorting user config
266271
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);

apps/files/src/actions/openFolderAction.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*
2121
*/
22-
import { join } from 'path'
2322
import { Permission, Node, FileType, View, FileAction, DefaultType } from '@nextcloud/files'
2423
import { translate as t } from '@nextcloud/l10n'
2524
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
@@ -49,15 +48,15 @@ export const action = new FileAction({
4948
&& (node.permissions & Permission.READ) !== 0
5049
},
5150

52-
async exec(node: Node, view: View, dir: string) {
51+
async exec(node: Node, view: View) {
5352
if (!node || node.type !== FileType.Folder) {
5453
return false
5554
}
5655

5756
window.OCP.Files.Router.goToRoute(
5857
null,
5958
{ view: view.id, fileid: node.fileid },
60-
{ dir: join(dir, node.basename) },
59+
{ dir: node.path },
6160
)
6261
return null
6362
},

apps/files/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Vue.use(PiniaVuePlugin)
3434
const pinia = createPinia()
3535

3636
// Init Navigation Service
37-
const Navigation = getNavigation()
37+
// This only works with Vue 2 - with Vue 3 this will not modify the source but return just a oberserver
38+
const Navigation = Vue.observable(getNavigation())
3839
Vue.prototype.$navigation = Navigation
3940

4041
// Init Files App Settings Service

apps/files/src/router/router.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*
2121
*/
22+
import type { RawLocation, Route } from 'vue-router'
23+
2224
import { generateUrl } from '@nextcloud/router'
2325
import queryString from 'query-string'
24-
import Router, { RawLocation, Route } from 'vue-router'
26+
import Router from 'vue-router'
2527
import Vue from 'vue'
2628
import { ErrorHandler } from 'vue-router/types/router'
2729

@@ -46,10 +48,10 @@ const router = new Router({
4648
{
4749
path: '/',
4850
// Pretending we're using the default view
49-
redirect: { name: 'filelist' },
51+
redirect: { name: 'filelist', params: { view: 'files' } },
5052
},
5153
{
52-
path: '/:view/:fileid?',
54+
path: '/:view/:fileid(\\d+)?',
5355
name: 'filelist',
5456
props: true,
5557
},

apps/files/src/views/FilesList.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ export default defineComponent({
222222
},
223223
224224
currentView(): View {
225-
return (this.$navigation.active
226-
|| this.$navigation.views.find(view => view.id === 'files')) as View
225+
return this.$navigation.active || this.$navigation.views.find((view) => view.id === (this.$route.params?.view ?? 'files'))
227226
},
228227
229228
/**

apps/files/src/views/Navigation.vue

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
:key="view.id"
2828
:allow-collapse="true"
2929
:data-cy-files-navigation-item="view.id"
30-
:exact="true"
30+
:exact="useExactRouteMatching(view)"
3131
:icon="view.iconClass"
3232
:name="view.name"
3333
:open="isExpanded(view)"
@@ -41,7 +41,7 @@
4141
<NcAppNavigationItem v-for="child in childViews[view.id]"
4242
:key="child.id"
4343
:data-cy-files-navigation-item="child.id"
44-
:exact="true"
44+
:exact-path="true"
4545
:icon="child.iconClass"
4646
:name="child.name"
4747
:to="generateToNavigation(child)">
@@ -128,7 +128,7 @@ export default {
128128
},
129129
130130
currentView(): View {
131-
return this.views.find(view => view.id === this.currentViewId)
131+
return this.views.find(view => view.id === this.currentViewId)!
132132
},
133133
134134
views(): View[] {
@@ -145,19 +145,19 @@ export default {
145145
})
146146
},
147147
148-
childViews(): View[] {
148+
childViews(): Record<string, View[]> {
149149
return this.views
150150
// filter parent views
151151
.filter(view => !!view.parent)
152152
// create a map of parents and their children
153153
.reduce((list, view) => {
154-
list[view.parent] = [...(list[view.parent] || []), view]
154+
list[view.parent!] = [...(list[view.parent!] || []), view]
155155
// Sort children by order
156-
list[view.parent].sort((a, b) => {
156+
list[view.parent!].sort((a, b) => {
157157
return a.order - b.order
158158
})
159159
return list
160-
}, {})
160+
}, {} as Record<string, View[]>)
161161
},
162162
},
163163
@@ -180,6 +180,16 @@ export default {
180180
},
181181
182182
methods: {
183+
/**
184+
* Only use exact route matching on routes with child views
185+
* Because if a view does not have children (like the files view) then multiple routes might be matched for it
186+
* Like for the 'files' view this does not work because of optional 'fileid' param so /files and /files/1234 are both in the 'files' view
187+
* @param view The view to check
188+
*/
189+
useExactRouteMatching(view: View): boolean {
190+
return this.childViews[view.id]?.length > 0
191+
},
192+
183193
showView(view: View) {
184194
// Closing any opened sidebar
185195
window?.OCA?.Files?.Sidebar?.close?.()
@@ -215,8 +225,8 @@ export default {
215225
*/
216226
generateToNavigation(view: View) {
217227
if (view.params) {
218-
const { dir, fileid } = view.params
219-
return { name: 'filelist', params: view.params, query: { dir, fileid } }
228+
const { dir } = view.params
229+
return { name: 'filelist', params: view.params, query: { dir } }
220230
}
221231
return { name: 'filelist', params: { view: view.id } }
222232
},

apps/files/src/views/favorites.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ describe('Favorites view definition', () => {
8282

8383
test('Default with favorites', () => {
8484
const favoriteFolders = [
85-
'/foo',
86-
'/bar',
87-
'/foo/bar',
85+
{ fileid: 1, path: '/foo' },
86+
{ fileid: 2, path: '/bar' },
87+
{ fileid: 3, path: '/foo/bar' },
8888
]
8989
jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
9090
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
@@ -102,11 +102,12 @@ describe('Favorites view definition', () => {
102102
const favoriteView = favoriteFoldersViews[index]
103103
expect(favoriteView).toBeDefined()
104104
expect(favoriteView?.id).toBeDefined()
105-
expect(favoriteView?.name).toBe(basename(folder))
105+
expect(favoriteView?.name).toBe(basename(folder.path))
106106
expect(favoriteView?.icon).toBe('<svg>SvgMock</svg>')
107107
expect(favoriteView?.order).toBe(index)
108108
expect(favoriteView?.params).toStrictEqual({
109-
dir: folder,
109+
dir: folder.path,
110+
fileid: folder.fileid.toString(),
110111
view: 'favorites',
111112
})
112113
expect(favoriteView?.parent).toBe('favorites')
@@ -157,7 +158,7 @@ describe('Dynamic update of favourite folders', () => {
157158
test('Remove a favorite folder remove the entry from the navigation column', async () => {
158159
jest.spyOn(eventBus, 'emit')
159160
jest.spyOn(eventBus, 'subscribe')
160-
jest.spyOn(initialState, 'loadState').mockReturnValue(['/Foo/Bar'])
161+
jest.spyOn(initialState, 'loadState').mockReturnValue([{ fileid: 42, path: '/Foo/Bar' }])
161162
jest.spyOn(favoritesService, 'getContents').mockReturnValue(Promise.resolve({ folder: {} as Folder, contents: [] }))
162163

163164
registerFavoritesView()

0 commit comments

Comments
 (0)