-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Files app] Ducks: reducer migration: folder shortcuts
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
Showing
9 changed files
with
134 additions
and
155 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
71 changes: 0 additions & 71 deletions
71
ui/file_manager/file_manager/state/actions/folder_shortcuts.ts
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
ui/file_manager/file_manager/state/ducks/folder_shortcuts.ts
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,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); |
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
69 changes: 0 additions & 69 deletions
69
ui/file_manager/file_manager/state/reducers/folder_shortcuts.ts
This file was deleted.
Oops, something went wrong.
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