Skip to content

Commit

Permalink
[Files app] Ducks: reducer migration: folder shortcuts
Browse files Browse the repository at this point in the history
Bug: b:295777015
Test: CQ passes
Change-Id: I591ec9b60f3d046614961eab7bb565623067e266
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4805484
Commit-Queue: Marcello Salomao <msalomao@google.com>
Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1187052}
  • Loading branch information
Marcello Salomao authored and Chromium LUCI CQ committed Aug 23, 2023
1 parent 8763c1a commit c81d513
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 155 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/ash/file_manager/file_manager_jstest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, ReducerAndroidApps) {
}

IN_PROC_BROWSER_TEST_F(FileManagerJsTest, ReducerFolderShortcuts) {
RunTestURL("state/reducers/folder_shortcuts_unittest.js");
RunTestURL("state/ducks/folder_shortcuts_unittest.js");
}

IN_PROC_BROWSER_TEST_F(FileManagerJsTest, ReducerCurrentDirectory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {FilteredVolumeManager} from '../../common/js/filtered_volume_manager.js'
import {metrics} from '../../common/js/metrics.js';
import {util} from '../../common/js/util.js';
import {VolumeManagerCommon} from '../../common/js/volume_manager_types.js';
import {addFolderShortcut, refreshFolderShortcut, removeFolderShortcut} from '../../state/actions/folder_shortcuts.js';
import {addFolderShortcut, refreshFolderShortcut, removeFolderShortcut} from '../../state/ducks/folder_shortcuts.js';
import {getStore} from '../../state/store.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion ui/file_manager/file_manager/state/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import {AddChildEntriesAction, ClearStaleCachedEntriesAction, UpdateMetadataAction} from './actions/all_entries.js';
import {AddAndroidAppsAction} from './ducks/android_apps.js';
import {ChangeDirectoryAction, ChangeFileTasksAction, ChangeSelectionAction, UpdateDirectoryContentAction} from './actions/current_directory.js';
import {AddFolderShortcutAction, RefreshFolderShortcutAction, RemoveFolderShortcutAction} from './actions/folder_shortcuts.js';
import {AddFolderShortcutAction, RefreshFolderShortcutAction, RemoveFolderShortcutAction} from './ducks/folder_shortcuts.js';
import {RefreshNavigationRootsAction, UpdateNavigationEntryAction} from './actions/navigation.js';
import {UpdatePreferencesAction} from './actions/preferences.js';
import {UpdateBulkPinProgressAction} from './ducks/bulk_pinning.js';
Expand Down
71 changes: 0 additions & 71 deletions ui/file_manager/file_manager/state/actions/folder_shortcuts.ts

This file was deleted.

125 changes: 125 additions & 0 deletions ui/file_manager/file_manager/state/ducks/folder_shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {util} from '../../common/js/util.js';
import {FileKey, State} from '../../externs/ts/state.js';
import {addReducer, BaseAction, Reducer, ReducersMap} from '../../lib/base_store.js';
import {Action, ActionType} from '../actions.js';
import {getEntry} from '../store.js';

/**
* Actions and reducers for folder shortcuts.
*
* Folder shortcuts represent a shortcut for the folders inside Drive.
*/

/** Map of actions to reducers for the folder shortcuts slice. */
export const folderShortcutsReducersMap: ReducersMap<State, Action> = new Map();

/** Action to refresh all folder shortcuts in the store. */
export interface RefreshFolderShortcutAction extends BaseAction {
type: ActionType.REFRESH_FOLDER_SHORTCUT;
payload: {
/** All folder shortcuts should be provided here. */
entries: DirectoryEntry[],
};
}

const refreshFolderShortcutReducer =
(currentState: State, payload: RefreshFolderShortcutAction['payload']):
State => ({
...currentState,
folderShortcuts: payload.entries.map(entry => entry.toURL()),
});

/**
* Action factory to refresh all folder shortcuts in the store, all folder
* shortcuts needs to be provided here because it will replace all existing ones
* in the store.
*/
export const refreshFolderShortcut = addReducer(
ActionType.REFRESH_FOLDER_SHORTCUT,
refreshFolderShortcutReducer as Reducer<State, Action>,
folderShortcutsReducersMap);


/** Action to add single folder shortcut in the store. */
export interface AddFolderShortcutAction extends BaseAction {
type: ActionType.ADD_FOLDER_SHORTCUT;
payload: {
entry: DirectoryEntry,
};
}

function addFolderShortcutReducer(
currentState: State, payload: AddFolderShortcutAction['payload']): State {
const {entry} = payload;
const key = entry.toURL();
const {folderShortcuts} = currentState;

for (let i = 0; i < folderShortcuts.length; i++) {
// Do nothing if the key is already existed.
if (key === folderShortcuts[i]) {
return currentState;
}

const shortcutEntry = getEntry(currentState, folderShortcuts[i]!);
// The folder shortcut array is sorted, the new item will be added just
// before the first larger item.
if (util.comparePath(shortcutEntry!, entry) > 0) {
return {
...currentState,
folderShortcuts: [
...folderShortcuts.slice(0, i),
key,
...folderShortcuts.slice(i),
],
};
}
}

// If for loop is not returned, the key is not added yet, add it at the last.
return {
...currentState,
folderShortcuts: folderShortcuts.concat(key),
};
}

/** Action factory to add single folder shortcut in the store. */
export const addFolderShortcut = addReducer(
ActionType.ADD_FOLDER_SHORTCUT,
addFolderShortcutReducer as Reducer<State, Action>,
folderShortcutsReducersMap);


/** Action to remove single folder shortcut from the store. */
export interface RemoveFolderShortcutAction extends BaseAction {
type: ActionType.REMOVE_FOLDER_SHORTCUT;
payload: {
key: FileKey,
};
}

function removeFolderShortcutReducer(
currentState: State,
payload: RemoveFolderShortcutAction['payload']): State {
const {key} = payload;
const {folderShortcuts} = currentState;
const isExisted = folderShortcuts.find(k => k === key);
// Do nothing if the key is not existed.
if (!isExisted) {
return currentState;
}

return {
...currentState,
folderShortcuts: folderShortcuts.filter(k => k !== key),
};
}

/** Action factory to remove single folder shortcut in the store. */
export const removeFolderShortcut = addReducer(
ActionType.REMOVE_FOLDER_SHORTCUT,
removeFolderShortcutReducer as Reducer<State, Action>,
folderShortcutsReducersMap);
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import '../store.js';

import {MockFileSystem} from '../../common/js/mock_entry.js';
import {State} from '../../externs/ts/state.js';
import {addFolderShortcut, refreshFolderShortcut, removeFolderShortcut} from '../actions/folder_shortcuts.js';
import {setUpFileManagerOnWindow, setupStore, waitDeepEquals} from '../for_tests.js';
import {convertEntryToFileData} from '../reducers/all_entries.js';
import {getEmptyState} from '../store.js';

import {convertEntryToFileData} from './all_entries.js';
import {addFolderShortcut, refreshFolderShortcut, removeFolderShortcut} from './folder_shortcuts.js';

export function setUp() {
setUpFileManagerOnWindow();
Expand Down
69 changes: 0 additions & 69 deletions ui/file_manager/file_manager/state/reducers/folder_shortcuts.ts

This file was deleted.

9 changes: 2 additions & 7 deletions ui/file_manager/file_manager/state/reducers/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {State} from '../../externs/ts/state.js';
import {Action, ActionType} from '../actions.js';
import {androidAppsReducersMap} from '../ducks/android_apps.js';
import {bulkPinningReducersMap} from '../ducks/bulk_pinning.js';
import {folderShortcutsReducersMap} from '../ducks/folder_shortcuts.js';
import {searchReducersMap} from '../ducks/search.js';
import {uiEntriesReducersMap} from '../ducks/ui_entries.js';
import {volumesReducersMap} from '../ducks/volumes.js';

import {addChildEntries, cacheEntries, clearCachedEntries, updateMetadata} from './all_entries.js';
import {changeDirectory, updateDirectoryContent, updateFileTasks, updateSelection} from './current_directory.js';
import {addFolderShortcut, refreshFolderShortcut, removeFolderShortcut} from './folder_shortcuts.js';
import {refreshNavigationRoots, updateNavigationEntry} from './navigation.js';
import {updatePreferences} from './preferences.js';

Expand All @@ -23,6 +23,7 @@ const rootReducersMap = new Map([
...bulkPinningReducersMap,
...uiEntriesReducersMap,
...androidAppsReducersMap,
...folderShortcutsReducersMap,
]);

/**
Expand Down Expand Up @@ -61,12 +62,6 @@ export function rootReducer(currentState: State, action: Action): State {
return refreshNavigationRoots(currentState, action);
case ActionType.UPDATE_NAVIGATION_ENTRY:
return updateNavigationEntry(currentState, action);
case ActionType.REFRESH_FOLDER_SHORTCUT:
return refreshFolderShortcut(currentState, action);
case ActionType.ADD_FOLDER_SHORTCUT:
return addFolderShortcut(currentState, action);
case ActionType.REMOVE_FOLDER_SHORTCUT:
return removeFolderShortcut(currentState, action);
case ActionType.ADD_CHILD_ENTRIES:
return addChildEntries(currentState, action);
case ActionType.UPDATE_PREFERENCES:
Expand Down
5 changes: 2 additions & 3 deletions ui/file_manager/file_names.gni
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ ts_files = [
"file_manager/state/actions.ts",
"file_manager/state/actions/all_entries.ts",
"file_manager/state/actions/current_directory.ts",
"file_manager/state/actions/folder_shortcuts.ts",
"file_manager/state/actions/navigation.ts",
"file_manager/state/actions/preferences.ts",

Expand All @@ -266,13 +265,13 @@ ts_files = [
"file_manager/state/reducers/root.ts",
"file_manager/state/reducers/all_entries.ts",
"file_manager/state/reducers/current_directory.ts",
"file_manager/state/reducers/folder_shortcuts.ts",
"file_manager/state/reducers/navigation.ts",
"file_manager/state/reducers/preferences.ts",

# Ducks.
"file_manager/state/ducks/android_apps.ts",
"file_manager/state/ducks/bulk_pinning.ts",
"file_manager/state/ducks/folder_shortcuts.ts",
"file_manager/state/ducks/search.ts",
"file_manager/state/ducks/ui_entries.ts",
"file_manager/state/ducks/volumes.ts",
Expand Down Expand Up @@ -401,13 +400,13 @@ ts_test_files = [
"file_manager/state/for_tests.ts",
"file_manager/state/reducers/all_entries_unittest.ts",
"file_manager/state/reducers/current_directory_unittest.ts",
"file_manager/state/reducers/folder_shortcuts_unittest.ts",
"file_manager/state/reducers/navigation_unittest.ts",
"file_manager/state/reducers/preferences_unittest.ts",

# Ducks:
"file_manager/state/ducks/android_apps_unittest.ts",
"file_manager/state/ducks/bulk_pinning_unittest.ts",
"file_manager/state/ducks/folder_shortcuts_unittest.ts",
"file_manager/state/ducks/search_unittest.ts",
"file_manager/state/ducks/ui_entries_unittest.ts",
"file_manager/state/ducks/volumes_unittest.ts",
Expand Down

0 comments on commit c81d513

Please sign in to comment.