-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4325 from brave/ca-2971
Refactor DnD on NTP Top Sites
- Loading branch information
Showing
45 changed files
with
2,119 additions
and
1,374 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// Types | ||
import { types } from '../constants/grid_sites_types' | ||
import { action } from 'typesafe-actions' | ||
import { InitialData } from '../api/initialData' | ||
import { Dispatch } from 'redux' | ||
|
||
// API | ||
import { | ||
fetchAllBookmarkTreeNodes, | ||
updateBookmarkTreeNode | ||
} from '../api/bookmarks' | ||
|
||
export const setFirstRenderGridSitesData = (initialData: InitialData) => { | ||
return action(types.GRID_SITES_SET_FIRST_RENDER_DATA, initialData) | ||
} | ||
|
||
export const gridSitesDataUpdated = (gridSites: NewTab.Site[]) => { | ||
return action(types.GRID_SITES_DATA_UPDATED, { gridSites }) | ||
} | ||
|
||
export const toggleGridSitePinned = (pinnedSite: NewTab.Site) => { | ||
return action(types.GRID_SITES_TOGGLE_SITE_PINNED, { pinnedSite }) | ||
} | ||
|
||
export const removeGridSite = (removedSite: NewTab.Site) => { | ||
return action(types.GRID_SITES_REMOVE_SITE, { removedSite }) | ||
} | ||
|
||
export const undoRemoveGridSite = () => { | ||
return action(types.GRID_SITES_UNDO_REMOVE_SITE) | ||
} | ||
|
||
export const undoRemoveAllGridSites = () => { | ||
return action(types.GRID_SITES_UNDO_REMOVE_ALL_SITES) | ||
} | ||
|
||
export const updateGridSitesBookmarkInfo = ( | ||
sites: chrome.topSites.MostVisitedURL[] | ||
) => { | ||
return async (dispatch: Dispatch) => { | ||
const bookmarkInfo = await fetchAllBookmarkTreeNodes(sites) | ||
dispatch(action(types.GRID_SITES_UPDATE_SITE_BOOKMARK_INFO, { | ||
bookmarkInfo | ||
})) | ||
} | ||
} | ||
|
||
export const toggleGridSiteBookmarkInfo = (site: NewTab.Site) => { | ||
return async (dispatch: Dispatch) => { | ||
const bookmarkInfo = await updateBookmarkTreeNode(site) | ||
dispatch(action(types.GRID_SITES_TOGGLE_SITE_BOOKMARK_INFO, { | ||
url: site.url, | ||
bookmarkInfo | ||
})) | ||
} | ||
} | ||
|
||
export const addGridSites = (site: NewTab.Site) => { | ||
return action(types.GRID_SITES_ADD_SITES, { site }) | ||
} | ||
|
||
export const showGridSiteRemovedNotification = (shouldShow: boolean) => { | ||
return action(types.GRID_SITES_SHOW_SITE_REMOVED_NOTIFICATION, { shouldShow }) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
/** | ||
* Obtain the URLs bookmark info | ||
*/ | ||
export const fetchBookmarkTreeNode = ( | ||
url: string | ||
): Promise<chrome.bookmarks.BookmarkTreeNode> => { | ||
return new Promise(resolve => { | ||
chrome.bookmarks.search( | ||
url, | ||
(bookmarkTreeNodes) => { | ||
resolve(bookmarkTreeNodes[0]) | ||
} | ||
) | ||
}) | ||
} | ||
|
||
/** | ||
* Iterate over the sites array and obtain all URLs | ||
* bookmark info | ||
*/ | ||
export const fetchAllBookmarkTreeNodes = ( | ||
sites: chrome.topSites.MostVisitedURL[] | ||
): Promise<chrome.bookmarks.BookmarkTreeNode[]> => { | ||
return Promise | ||
.all(sites.map(site => fetchBookmarkTreeNode(site.url))) | ||
} | ||
|
||
/** | ||
* Update bookmark info based on user interaction | ||
*/ | ||
export const updateBookmarkTreeNode = (site: NewTab.Site) => { | ||
return new Promise(async resolve => { | ||
const bookmarkInfo = await fetchBookmarkTreeNode(site.url) | ||
// Toggle the bookmark state | ||
if (bookmarkInfo) { | ||
chrome.bookmarks.remove(bookmarkInfo.id) | ||
} else { | ||
chrome.bookmarks.create({ title: site.title, url: site.url }) | ||
} | ||
resolve(bookmarkInfo) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,19 +1,22 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
// Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import { bindActionCreators } from 'redux' | ||
import * as newTabActions from '../actions/new_tab_actions' | ||
import * as gridSitesActions from '../actions/grid_sites_actions' | ||
import store from '../store' | ||
|
||
/** | ||
* Get actions from the C++ back-end down to front-end components | ||
*/ | ||
let actions: typeof newTabActions | ||
let actions: typeof newTabActions & typeof gridSitesActions | ||
export default function getActions () { | ||
if (actions) { | ||
return actions | ||
} | ||
actions = bindActionCreators(newTabActions, store.dispatch.bind(store)) | ||
const allActions = Object.assign({}, newTabActions, gridSitesActions) | ||
actions = bindActionCreators(allActions, store.dispatch.bind(store)) | ||
return actions | ||
} |
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
8 changes: 3 additions & 5 deletions
8
...ts/brave_new_tab_ui/api/topSites/index.ts → components/brave_new_tab_ui/api/topSites.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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.