mirror of https://github.com/msgbyte/tailchat
refactor: 根据登录状态修改加入按钮的变更
parent
c731cd0690
commit
56cf913675
@ -0,0 +1,25 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import type { FetchQueryOptions } from 'react-query';
|
||||
import { queryClient } from './';
|
||||
|
||||
/**
|
||||
* 构建缓存请求
|
||||
* TODO: 这里的类型真的不好写, 先用any来过滤内部的, 只保证外部使用ok
|
||||
*
|
||||
* @example
|
||||
* const queryData = buildCachedRequest('key')((arg1, arg2) => {
|
||||
* return request.post(...)
|
||||
* })
|
||||
*/
|
||||
export function buildCachedRequest<
|
||||
R extends any,
|
||||
F extends (...args: any) => Promise<R>
|
||||
>(prefix: string, fn: F, options?: FetchQueryOptions): F {
|
||||
return ((...args: any) => {
|
||||
return queryClient.fetchQuery(
|
||||
[prefix, JSON.stringify(args)],
|
||||
() => fn(...args),
|
||||
options
|
||||
);
|
||||
}) as any;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import { getUserJWT } from '@/utils/jwt-helper';
|
||||
import { Button } from 'antd';
|
||||
import React, { useCallback } from 'react';
|
||||
import { useHistory } from 'react-router';
|
||||
import { checkTokenValid, t, useAsync } from 'tailchat-shared';
|
||||
|
||||
interface Props {
|
||||
onJoinGroup: () => void;
|
||||
}
|
||||
export const JoinBtn: React.FC<Props> = React.memo((props) => {
|
||||
const history = useHistory();
|
||||
const { loading, value: isTokenValid } = useAsync(async () => {
|
||||
const token = await getUserJWT();
|
||||
const isTokenValid = await checkTokenValid(token);
|
||||
return isTokenValid;
|
||||
});
|
||||
|
||||
const handleRegister = useCallback(() => {
|
||||
history.push(
|
||||
`/entry/register?redirect=${encodeURIComponent(location.pathname)}`
|
||||
);
|
||||
}, []);
|
||||
|
||||
if (loading) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return isTokenValid ? (
|
||||
<Button
|
||||
block={true}
|
||||
type="primary"
|
||||
size="large"
|
||||
onClick={props.onJoinGroup}
|
||||
>
|
||||
{t('加入群组')}
|
||||
</Button>
|
||||
) : (
|
||||
<Button block={true} type="primary" size="large" onClick={handleRegister}>
|
||||
{t('立即注册')}
|
||||
</Button>
|
||||
);
|
||||
});
|
||||
JoinBtn.displayName = 'JoinBtn';
|
Loading…
Reference in New Issue