Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve dapps bookmark #2141

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: improve dapps bookmark
  • Loading branch information
kwoktung committed Dec 5, 2022
commit 556a243adc6c3aa38d08ca4ce8699349612d2386
88 changes: 35 additions & 53 deletions packages/kit/src/background/services/ServiceDiscover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
// setTagDapps,
setHomeData,
} from '../../store/reducers/discover';
import { WebTab, setWebTabData } from '../../store/reducers/webTabs';
import { setWebTabData } from '../../store/reducers/webTabs';
import { MatchDAppItemType } from '../../views/Discover/Explorer/explorerUtils';
import { DAppItemType } from '../../views/Discover/type';
import { backgroundClass, backgroundMethod } from '../decorators';
Expand Down Expand Up @@ -128,75 +128,57 @@ class ServicDiscover extends ServiceBase {
}

@backgroundMethod()
async addFavorite(key: string) {
const { dispatch } = this.backgroundApi;
dispatch(addFavorite(key));
}

@backgroundMethod()
async removeFavorite(key: string) {
const { dispatch } = this.backgroundApi;
dispatch(removeFavorite(key));
}

@backgroundMethod()
async setDappHistory(dappId: string) {
const { dispatch } = this.backgroundApi;
dispatch(setDappHistory(dappId));
}

@backgroundMethod()
async removeDappHistory(dappId: string) {
const { dispatch } = this.backgroundApi;
dispatch(removeDappHistory(dappId));
async addFavorite(url: string) {
const { dispatch, appSelector } = this.backgroundApi;
dispatch(addFavorite(url));
kwoktung marked this conversation as resolved.
Show resolved Hide resolved
const tabs = appSelector((s) => s.webTabs.tabs);
const list = tabs.filter((tab) => tab.url === url);
if (list.length) {
const actions = list.map((tab) =>
setWebTabData({ ...tab, isBookmarked: true }),
);
dispatch(...actions);
}
}

@backgroundMethod()
async toggleBookmark(tab: WebTab) {
if (tab.isBookmarked) {
this.removeBookmark(tab);
} else {
this.addBookmark(tab);
async removeFavorite(url: string) {
const { dispatch, appSelector } = this.backgroundApi;
dispatch(removeFavorite(url));
const tabs = appSelector((s) => s.webTabs.tabs);
const list = tabs.filter((tab) => tab.url === url);
if (list.length) {
const actions = list.map((tab) =>
setWebTabData({ ...tab, isBookmarked: false }),
);
dispatch(...actions);
}
}

@backgroundMethod()
async addBookmark(tab: WebTab) {
const { dispatch } = this.backgroundApi;
if (tab.isBookmarked) {
async toggleFavorite(url?: string) {
if (!url) {
return;
}
const newTab: WebTab = { ...tab, isBookmarked: !tab.isBookmarked };
dispatch(setWebTabData(newTab));
if (newTab.url) {
this.addFavorite(newTab.url);
const { appSelector } = this.backgroundApi;
const dappFavorites = appSelector((s) => s.discover.dappFavorites);
if (!dappFavorites || !dappFavorites.includes(url)) {
this.addFavorite(url);
} else {
this.removeFavorite(url);
}
}

@backgroundMethod()
async removeBookmark(tab: WebTab) {
async setDappHistory(dappId: string) {
const { dispatch } = this.backgroundApi;
if (!tab.isBookmarked) {
return;
}
const newTab: WebTab = { ...tab, isBookmarked: !tab.isBookmarked };
dispatch(setWebTabData(newTab));
if (newTab.url) {
this.removeFavorite(newTab.url);
}
dispatch(setDappHistory(dappId));
}

@backgroundMethod()
async updateBookmark({ tabId, url }: { tabId?: string; url?: string }) {
if (!tabId || !url) {
return;
}
const { appSelector, dispatch } = this.backgroundApi;
const dappFavorites = appSelector((s) => s.discover.dappFavorites);
if (dappFavorites) {
const isBookmarked = dappFavorites.includes(url);
dispatch(setWebTabData({ id: tabId, isBookmarked }));
}
async removeDappHistory(dappId: string) {
const { dispatch } = this.backgroundApi;
dispatch(removeDappHistory(dappId));
}

@backgroundMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ const ControllerBarDesktop: FC = () => {
<IconButton
onPress={() =>
currentTab &&
backgroundApiProxy.serviceDiscover.toggleBookmark(
currentTab,
backgroundApiProxy.serviceDiscover.toggleFavorite(
currentTab.url,
)
}
type="plain"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Animated, {

import { Box, Pressable, useSafeAreaInsets } from '@onekeyhq/components';

import backgroundApiProxy from '../../../../background/instance/backgroundApiProxy';
import { useWebTabs } from '../Controller/useWebTabs';
import {
MAX_OR_SHOW,
Expand Down Expand Up @@ -187,10 +186,6 @@ const FloatingContainer: FC<
beforeMaximize: innerBeforeMaximize,
afterMinimize: innerAfterMinimize,
});
backgroundApiProxy.serviceDiscover.updateBookmark({
tabId: currentTab?.id,
url: currentTab?.url,
});
}}
>
<FloatingBar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const WebTabCard: FC<
WebTab & {
width: number;
}
> = ({ width, isCurrent, title, favicon, id, thumbnail, url }) => (
> = ({ width, isCurrent, title, favicon, id, thumbnail }) => (
<Pressable
w={width}
h={width}
Expand All @@ -56,7 +56,6 @@ const WebTabCard: FC<
onPress={() => {
if (!isCurrent) {
backgroundApiProxy.dispatch(setCurrentWebTab(id));
backgroundApiProxy.serviceDiscover.updateBookmark({ tabId: id, url });
}
hideTabGrid(id);
}}
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/views/Discover/Explorer/MoreMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ const MoreMenu: FC<{ onClose: () => void }> = ({ onClose }) => {
value: () => {
if (!currentTab) return;
if (currentTab.isBookmarked) {
backgroundApiProxy.serviceDiscover.removeBookmark(currentTab);
backgroundApiProxy.serviceDiscover.removeFavorite(currentTab.url);
} else {
backgroundApiProxy.serviceDiscover.addBookmark(currentTab);
backgroundApiProxy.serviceDiscover.addFavorite(currentTab.url);
}
Toast.show({ title: intl.formatMessage({ id: 'msg__success' }) });
},
Expand Down