1919 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2020 *
2121 */
22- import { basename } from 'path'
23- import { getLanguage , translate as t } from '@nextcloud/l10n'
24- import { loadState } from '@nextcloud/initial-state'
25- import { Node , FileType , View , getNavigation } from '@nextcloud/files'
22+ import type { Folder , Node } from '@nextcloud/files'
23+
2624import { subscribe } from '@nextcloud/event-bus'
25+ import { FileType , View , getNavigation } from '@nextcloud/files'
26+ import { loadState } from '@nextcloud/initial-state'
27+ import { getLanguage , translate as t } from '@nextcloud/l10n'
28+ import { basename } from 'path'
2729import FolderSvg from '@mdi/svg/svg/folder.svg?raw'
2830import StarSvg from '@mdi/svg/svg/star.svg?raw'
2931
3032import { getContents } from '../services/Favorites'
3133import { hashCode } from '../utils/hashUtils'
3234import logger from '../logger'
3335
34- export const generateFolderView = function ( folder : string , index = 0 ) : View {
36+ // The return type of the initial state
37+ interface IFavoriteFolder {
38+ fileid : number
39+ path : string
40+ }
41+
42+ export const generateFavoriteFolderView = function ( folder : IFavoriteFolder , index = 0 ) : View {
3543 return new View ( {
36- id : generateIdFromPath ( folder ) ,
37- name : basename ( folder ) ,
44+ id : generateIdFromPath ( folder . path ) ,
45+ name : basename ( folder . path ) ,
3846
3947 icon : FolderSvg ,
4048 order : index ,
4149 params : {
42- dir : folder ,
50+ dir : folder . path ,
51+ fileid : folder . fileid . toString ( ) ,
4352 view : 'favorites' ,
4453 } ,
4554
@@ -57,8 +66,8 @@ export const generateIdFromPath = function(path: string): string {
5766
5867export default ( ) => {
5968 // Load state in function for mock testing purposes
60- const favoriteFolders = loadState < string [ ] > ( 'files' , 'favoriteFolders' , [ ] )
61- const favoriteFoldersViews = favoriteFolders . map ( ( folder , index ) => generateFolderView ( folder , index ) ) as View [ ]
69+ const favoriteFolders = loadState < IFavoriteFolder [ ] > ( 'files' , 'favoriteFolders' , [ ] )
70+ const favoriteFoldersViews = favoriteFolders . map ( ( folder , index ) => generateFavoriteFolderView ( folder , index ) ) as View [ ]
6271
6372 const Navigation = getNavigation ( )
6473 Navigation . register ( new View ( {
@@ -93,7 +102,7 @@ export default () => {
93102 return
94103 }
95104
96- addPathToFavorites ( node . path )
105+ addToFavorites ( node as Folder )
97106 } )
98107
99108 /**
@@ -118,26 +127,27 @@ export default () => {
118127 * update the order property of the existing views
119128 */
120129 const updateAndSortViews = function ( ) {
121- favoriteFolders . sort ( ( a , b ) => a . localeCompare ( b , getLanguage ( ) , { ignorePunctuation : true } ) )
130+ favoriteFolders . sort ( ( a , b ) => a . path . localeCompare ( b . path , getLanguage ( ) , { ignorePunctuation : true } ) )
122131 favoriteFolders . forEach ( ( folder , index ) => {
123- const view = favoriteFoldersViews . find ( view => view . id === generateIdFromPath ( folder ) )
132+ const view = favoriteFoldersViews . find ( ( view ) => view . id === generateIdFromPath ( folder . path ) )
124133 if ( view ) {
125134 view . order = index
126135 }
127136 } )
128137 }
129138
130139 // Add a folder to the favorites paths array and update the views
131- const addPathToFavorites = function ( path : string ) {
132- const view = generateFolderView ( path )
140+ const addToFavorites = function ( node : Folder ) {
141+ const newFavoriteFolder = { path : node . path , fileid : node . fileid ! } as IFavoriteFolder
142+ const view = generateFavoriteFolderView ( newFavoriteFolder )
133143
134144 // Skip if already exists
135- if ( favoriteFolders . find ( folder => folder === path ) ) {
145+ if ( favoriteFolders . find ( ( folder ) => folder . path === node . path ) ) {
136146 return
137147 }
138148
139149 // Update arrays
140- favoriteFolders . push ( path )
150+ favoriteFolders . push ( newFavoriteFolder )
141151 favoriteFoldersViews . push ( view )
142152
143153 // Update and sort views
@@ -148,7 +158,7 @@ export default () => {
148158 // Remove a folder from the favorites paths array and update the views
149159 const removePathFromFavorites = function ( path : string ) {
150160 const id = generateIdFromPath ( path )
151- const index = favoriteFolders . findIndex ( folder => folder === path )
161+ const index = favoriteFolders . findIndex ( ( folder ) => folder . path === path )
152162
153163 // Skip if not exists
154164 if ( index === - 1 ) {
0 commit comments