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

fix: add width caculate #120

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 10 additions & 28 deletions src/content/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import IconButton from '@mui/material/IconButton';
import { styled } from '@mui/material/styles';

import * as toaster from '../../components/Toaster';
import { XFANS_CONTENT_WIDTH, XFANS_MIN_WIDTH } from '../../constants';
import { XFANS_CONTENT_WIDTH } from '../../constants';
import { ProfileData } from '../../service/login/me';
import { TwitterOauth2Data } from '../../service/login/twiterOuth2';
import http, { ResultData } from '../../service/request';
Expand All @@ -18,9 +18,9 @@ import ProfileModal from '../../welcome/Wallet/Profile';
import CongratulationPage from '../loginPage/congratulationPage';
import InvitePage from '../loginPage/invitePage';
import SignInWithXPage from '../loginPage/signInWithXPage';
import { getElementRightByXPath } from '../../utils';
import { caculateBackWidth, caculateDrawerWidth } from '../../utils';
import LogoButton from './logoButton';
import { XFANS_CHECK_RETWEET, XFANS_TOKEN, OAUTH2, XFANS_TWITTER_HOMEPAGE } from '../../constants';
import { XFANS_TOKEN } from '../../constants';
import '../../tailwind.css';

const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })<{
Expand Down Expand Up @@ -51,7 +51,9 @@ const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })<{
}));

export default function PersistentDrawerRight() {
const { isShowDrawer, goPage, page, logout } = useGlobalStore((state) => ({ ...state }));
const { isShowDrawer, goPage, page, logout, drawerWidth, backWidth } = useGlobalStore(
(state) => ({ ...state })
);

const [loginLoading, setLoginLoading] = React.useState(false);

Expand All @@ -67,33 +69,13 @@ export default function PersistentDrawerRight() {
});
};

const caculateDrawerWidth = () => {
if (window.location.href.includes(OAUTH2)) {
return XFANS_MIN_WIDTH;
}
// 首页信息流dom
const xPath = '/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div';
const right = getElementRightByXPath(xPath);

if (right === null) {
return XFANS_MIN_WIDTH;
}
return Math.max(window.innerWidth - right, XFANS_MIN_WIDTH);
};

const caculateBackWidth = () => {
return caculateDrawerWidth() - XFANS_CONTENT_WIDTH;
};

const [drawerWidth, setDrawerWidth] = React.useState(caculateDrawerWidth());

const [backWidth, setBackWidth] = React.useState(caculateBackWidth());

React.useEffect(() => {
function handleResize() {
// 当窗口大小变化时,更新 width 的值
setBackWidth(caculateBackWidth());
setDrawerWidth(caculateDrawerWidth());
useGlobalStore.setState({
drawerWidth: caculateDrawerWidth(),
backWidth: caculateBackWidth(),
});
}

// 添加窗口大小变化时的事件监听器
Expand Down
6 changes: 5 additions & 1 deletion src/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Store } from '@eduardoac-skimlinks/webext-redux';

import { proxyStore as store } from '../app/proxyStore';
import useGlobalStore from '../store/useGlobalStore';

import { caculateBackWidth, caculateDrawerWidth } from '../utils';
import { addTwitterComponent, addUserPagePriceComponent } from './addToTwitterHome';
import Content from './Content';

Expand Down Expand Up @@ -36,6 +36,10 @@ withProxyStore(<Content />, store).then((component) => {
// 延迟执行的代码 3000毫秒后执行,即3秒
setTimeout(() => {
setInterval(() => {
useGlobalStore.setState({
drawerWidth: caculateDrawerWidth(),
backWidth: caculateBackWidth(),
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要定时去算宽度来着

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的计算频率是在onload onresize两个事件触发的时候进行计算。但是onresize不会经常被触发,除非用户手动调整窗口。onload要基于twitter全部加载完成,有时候时间很长,要30秒。其次是还有很多时候用户切换到其他页面等等因素,这两个时机是不够的,因此加入到全局的计算中去。定时计算宽度,如果宽度不变,我认为性能损耗是不高的。

Copy link

@huangbinjie huangbinjie Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我测试了下,宽度不变定时去 set 也会导致 rerender。而且因为是根节点和到处都用的全局 store,导致整个插件定时的 rerender。感觉 onLoad 够用了,长点的话插件有最小宽度保底问题不大

// 使用示例
const userIsOnProfilePage = whereIsUser();
const { token } = useGlobalStore.getState();
Expand Down
4 changes: 4 additions & 0 deletions src/store/useGlobalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface GlobalStoreProps {
userInfo: UserInfo | null;
goPage(page: PageType): void;
logout(): void;
drawerWidth: number;
backWidth: number;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emmm,看起来宽度没必要 lift 到全局把

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要是想把定时任务的代码都收拢到/content/index.ts中去方便管理。然后宽度这个东西勉强算个全局变量吧,描述的是整体的宽度

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你放到 drawer/index.tsx 里面更适合,不然你定时去 setInterval 会导致所有组件都会定时去 rerender

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的 放到drawer/index.tsx

userVote: Record<string, boolean> | null;
}

Expand All @@ -48,6 +50,8 @@ const useGlobalStore = create<GlobalStoreProps>()(
page: PageType.Login,
});
},
drawerWidth: 0,
backWidth: 0,
userVote: null,
}),
{
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BigNumber from 'bignumber.js';
import dayjs from 'dayjs';
import { OAUTH2, XFANS_MIN_WIDTH, XFANS_CONTENT_WIDTH } from './constants';

export function formatTime(seconds: any) {
if (seconds < 60) {
Expand Down Expand Up @@ -69,3 +70,22 @@ export function getElementRightByXPath(xpath: string): number | null {
return null;
}
}

export const caculateDrawerWidth = () => {
if (window.location.href.includes(OAUTH2)) {
return XFANS_MIN_WIDTH;
}
// 首页信息流dom
const xPath = '/html/body/div[1]/div/div/div[2]/main/div/div/div/div[1]/div';
const right = getElementRightByXPath(xPath);

if (right === null) {
return XFANS_MIN_WIDTH;
}

return Math.max(document.body.clientWidth - right, XFANS_MIN_WIDTH);
huangbinjie marked this conversation as resolved.
Show resolved Hide resolved
};

export const caculateBackWidth = () => {
return caculateDrawerWidth() - XFANS_CONTENT_WIDTH;
};
Loading