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 all commits
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
86 changes: 32 additions & 54 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,53 @@ 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;
const base = addFavorite(url);
const tabs = appSelector((s) => s.webTabs.tabs);
const list = tabs.filter((tab) => tab.url === url);
const actions = list.map((tab) =>
setWebTabData({ ...tab, isBookmarked: true }),
);
dispatch(base, ...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;
const base = removeFavorite(url);
const tabs = appSelector((s) => s.webTabs.tabs);
const list = tabs.filter((tab) => tab.url === url);
const actions = list.map((tab) =>
setWebTabData({ ...tab, isBookmarked: false }),
);
dispatch(base, ...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
14 changes: 2 additions & 12 deletions packages/kit/src/views/Discover/Home/Desktop/Beta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ListRenderItem } from 'react-native';
import {
Box,
Button,
Empty,
FlatList,
Image,
Pressable,
Expand All @@ -25,8 +24,6 @@ import { DiscoverModalRoutes } from '../../type';
import CardView from '../CardView';
import { DiscoverContext } from '../context';

import { EmptySkeleton } from './EmptySkeleton';

import type { SectionDataType } from '../../type';

const ListHeaderItemsEmptyComponent = () => {
Expand Down Expand Up @@ -187,8 +184,8 @@ const ListHeaderItems = () => {
};

const ListHeaderComponent = () => {
const dappItems = useAppSelector((s) => s.discover.dappItems);
if (!dappItems) {
const home = useAppSelector((s) => s.discover.home);
if (!home) {
return null;
}
return (
Expand All @@ -199,12 +196,6 @@ const ListHeaderComponent = () => {
);
};

const ListEmptyComponent = () => {
const dappItems = useAppSelector((s) => s.discover.dappItems);
const listedCategories = useAppSelector((s) => s.discover.listedCategories);
return dappItems && listedCategories ? <Empty title="" /> : <EmptySkeleton />;
};

export const Beta: FC = () => {
const intl = useIntl();
const navigation = useNavigation();
Expand Down Expand Up @@ -237,7 +228,6 @@ export const Beta: FC = () => {
renderItem={renderItem}
keyExtractor={(item, index) => `${item.title ?? ''}${index}`}
ListHeaderComponent={ListHeaderComponent}
ListEmptyComponent={ListEmptyComponent}
/>
</Box>
);
Expand Down