Skip to content

Commit

Permalink
chore: improve discover layout (#2145)
Browse files Browse the repository at this point in the history
Co-authored-by: loatheb <zhangzhao@onekey.so>
  • Loading branch information
kwoktung and loatheb authored Dec 6, 2022
1 parent 3d654e2 commit 01e110b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/kit/src/background/services/ServiceSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import semver from 'semver';
import simpleDb from '@onekeyhq/engine/src/dbs/simple/simpleDb';
import { getFiatEndpoint } from '@onekeyhq/engine/src/endpoint';

import { setEnableIOSDappSearch } from '../../store/reducers/discover';
import { setShowFullLayout } from '../../store/reducers/discover';
import {
disableExtSwitchTips,
toggleDisableExt,
Expand Down Expand Up @@ -44,7 +44,7 @@ export default class ServiceSetting extends ServiceBase {
if (data.helloVersion && semver.valid(data.helloVersion)) {
const version = appSelector((s) => s.settings.version);
if (semver.lte(version, data.helloVersion)) {
dispatch(setEnableIOSDappSearch(true));
dispatch(setShowFullLayout(true));
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions packages/kit/src/store/reducers/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type InitialState = {

history: Record<string, DiscoverHistory>;
firstRemindDAPP: boolean;
enableIOSDappSearch?: boolean;
// enableIOSDappSearch?: boolean;
showFullLayout?: boolean;
};

const initialState: InitialState = {
Expand Down Expand Up @@ -234,9 +235,11 @@ export const discoverSlice = createSlice({
// ) {
// state.tagDapps = action.payload;
// },

setEnableIOSDappSearch(state, action: PayloadAction<boolean>) {
state.enableIOSDappSearch = action.payload;
// setEnableIOSDappSearch(state, action: PayloadAction<boolean>) {
// state.enableIOSDappSearch = action.payload;
// },
setShowFullLayout(state, action: PayloadAction<boolean>) {
state.showFullLayout = action.payload;
},
cleanOldState(state) {
state.dappItems = undefined;
Expand Down Expand Up @@ -273,7 +276,8 @@ export const {
// setCategoryDapps,
// setTagDapps,
clearHistory,
setEnableIOSDappSearch,
// setEnableIOSDappSearch,
setShowFullLayout,
cleanOldState,
setHomeData,
} = discoverSlice.actions;
Expand Down
10 changes: 6 additions & 4 deletions packages/kit/src/views/Discover/Home/Desktop/Others.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ const ChainsSelector: FC<{ networkIds: string[] }> = ({ networkIds }) => {
);
};

const DappsContainer = () => {
const { categoryId } = useContext(DiscoverContext);
const dapps = useCategoryDapps(categoryId);
type DappsContainerProps = {
dapps: DAppItemType[];
};

const DappsContainer: FC<DappsContainerProps> = ({ dapps }) => {
const { selectedNetworkId } = useContext(SelectedNetworkContext);

const { onItemSelect } = useContext(DiscoverContext);
Expand Down Expand Up @@ -221,7 +223,7 @@ export function Container() {
</Box>
<Divider orientation="vertical" />
<Box flex="1">
<DappsContainer />
<DappsContainer dapps={dapps} />
</Box>
</Box>
</Box>
Expand Down
12 changes: 7 additions & 5 deletions packages/kit/src/views/Discover/Home/Desktop/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FC, useContext } from 'react';

import { Box } from '@onekeyhq/components';
import platformEnv from '@onekeyhq/shared/src/platformEnv';

import { useShowFullLayout } from '../../hooks';
import { DiscoverContext } from '../context';

import { Beta } from './Beta';
import { Mine } from './Mine';
import { Others } from './Others';

export const DesktopDefault = () => {
export const DesktopFull = () => {
const { categoryId } = useContext(DiscoverContext);
return (
<Box flex="1" bg="background-default">
Expand All @@ -18,11 +18,13 @@ export const DesktopDefault = () => {
);
};

export const DesktopMac = () => (
export const DesktopMini = () => (
<Box flex="1" bg="background-default">
<Beta />
</Box>
);

export const Desktop: FC = () =>
platformEnv.isMas ? <DesktopMac /> : <DesktopDefault />;
export const Desktop: FC = () => {
const showFullLayout = useShowFullLayout();
return !showFullLayout ? <DesktopMini /> : <DesktopFull />;
};
16 changes: 6 additions & 10 deletions packages/kit/src/views/Discover/Home/Mobile/Others.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ const DappsContainerItem: FC<{ item: DAppItemType }> = ({ item }) => {
);
};

const DappsContainer = () => {
const { categoryId } = useContext(DiscoverContext);
const fullDapps = useCategoryDapps(categoryId);
const [dapps, setDapps] = useState<DAppItemType[]>([]);
const { selectedNetworkId } = useContext(SelectedNetworkContext);
type DappsContainerProps = {
dapps: DAppItemType[];
};

const DappsContainer: FC<DappsContainerProps> = ({ dapps }) => {
const { selectedNetworkId } = useContext(SelectedNetworkContext);
const data = useMemo(() => {
if (!selectedNetworkId) {
return dapps;
Expand All @@ -154,10 +154,6 @@ const DappsContainer = () => {
[],
);

useEffect(() => {
setDapps(fullDapps);
}, [fullDapps]);

return (
<FlatList
data={data}
Expand Down Expand Up @@ -194,7 +190,7 @@ export function Container() {
</Box>
<Divider orientation="vertical" />
<Box flex="1">
<DappsContainer />
<DappsContainer dapps={dapps} />
</Box>
</Box>
</Box>
Expand Down
23 changes: 22 additions & 1 deletion packages/kit/src/views/Discover/Home/Mobile/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import { useContext } from 'react';

import { Box } from '@onekeyhq/components';

import { useShowFullLayout } from '../../hooks';
import { DiscoverContext } from '../context';

import { Beta } from './Beta';
import { Mine } from './Mine';
import { Others } from './Others';

export const MobileFull = () => {
const { categoryId } = useContext(DiscoverContext);
return (
<Box flex="1" bg="background-default">
{categoryId ? <Others /> : <Mine />}
</Box>
);
};

export const Mobile = Beta;
export const Mobile = () => {
const showFullLayout = useShowFullLayout();
return !showFullLayout ? <Beta /> : <MobileFull />;
};
12 changes: 12 additions & 0 deletions packages/kit/src/views/Discover/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useMemo, useState } from 'react';

import { useAppSelector } from '@onekeyhq/kit/src/hooks';
import platformEnv from '@onekeyhq/shared/src/platformEnv';

import backgroundApiProxy from '../../../background/instance/backgroundApiProxy';
import { MatchDAppItemType } from '../Explorer/explorerUtils';
Expand Down Expand Up @@ -211,3 +212,14 @@ export function useCategories() {
return home.categories;
}, [home]);
}

export function useShowFullLayout() {
const isApple = platformEnv.isNativeIOS || platformEnv.isMas;
const showFullLayout = useAppSelector((s) => s.discover.showFullLayout);
return useMemo(() => {
if (!isApple) {
return true;
}
return showFullLayout;
}, [showFullLayout, isApple]);
}
17 changes: 5 additions & 12 deletions packages/kit/src/views/Discover/hooks/useSearchLocalDapp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useEffect, useMemo, useState } from 'react';

import Fuse from 'fuse.js';
import { useShowFullLayout } from '.';

import platformEnv from '@onekeyhq/shared/src/platformEnv';
import Fuse from 'fuse.js';

import backgroundApiProxy from '../../../background/instance/backgroundApiProxy';
import { useAppSelector } from '../../../hooks';
import { MatchDAppItemType } from '../Explorer/explorerUtils';
import { DAppItemType } from '../type';

Expand Down Expand Up @@ -42,10 +41,7 @@ export const useSearchLocalDapp = (
): { loading: boolean; searchedDapps: MatchDAppItemType[] } => {
const [loading, setLoading] = useState(false);
const [allDapps, setAllDapps] = useState<DAppItemType[]>([]);

const enableIOSDappSearch = useAppSelector(
(s) => s.discover.enableIOSDappSearch,
);
const showFullLayout = useShowFullLayout();

useEffect(() => {
async function main() {
Expand All @@ -69,17 +65,14 @@ export const useSearchLocalDapp = (
setLoading(true);
try {
let items = allDapps;
if (
(platformEnv.isNativeIOS || platformEnv.isMas) &&
!enableIOSDappSearch
) {
if (!showFullLayout) {
items = [];
}
return searchDapps(items, terms);
} finally {
setLoading(false);
}
}, [allDapps, terms, enableIOSDappSearch]);
}, [allDapps, terms, showFullLayout]);

return {
loading,
Expand Down

0 comments on commit 01e110b

Please sign in to comment.