All files / src/components/supportWidget SupportWidget.tsx

77.77% Statements 35/45
56.52% Branches 13/23
63.63% Functions 7/11
76.19% Lines 32/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235                                      3x   32x 32x 32x 32x 32x   32x     32x 32x   32x 5x 3x 1x         5x 5x 5x   5x 5x     5x 5x         5x 5x       5x                     32x                 32x                                                                                                         32x                                                                                                           32x 160x     32x             32x     160x   32x                                                              
import React, { HTMLAttributeAnchorTarget, ReactElement, useEffect, useState } from 'react';
import { Dropdown, Space } from 'antd';
import {
  QuestionCircleOutlined, CustomerServiceOutlined, ReadOutlined,
  DeploymentUnitOutlined, PhoneOutlined, BulbOutlined, BugOutlined,
  CodeSandboxOutlined, UserOutlined, LinkOutlined
} from
  '@ant-design/icons';
import "./SupportWidget.less";
 
import { useTranslation } from 'react-i18next';
import FeedbackForm from './FeedbackForm';
import { ExtendedMenuProps, FeedbackType, SupportWidgetProps } from '.';
import { useConfig } from '@utils/hooks';
import SetBackendModal from "../../development/SetBackendModal";
import { SERVER_URL } from "@config/base";
import { loggedIn } from '@utils/LoginUtils';
import { fetchConfig } from '@utils/hooks/useConfig';
 
export const SupportWidget = (props: SupportWidgetProps): ReactElement => {
 
  const { zIndex, authorized } = props;
  const [showMenu, setShowMenu] = useState<boolean>(false);
  const [showFeedbackType, setShowFeedbackType] = useState<FeedbackType>();
  const [ipAddress, setIpAddress] = useState('');
  const [value, setValue] = useState<string>();
 
  const { t } = useTranslation();
 
 
  const { value: userManualLink } = useConfig("feedback.user_manual_link");
  const { value: developerManualLink } = useConfig("feedback.developer_manual_link");
 
  useEffect(() => {
    if (loggedIn()) {
      fetchConfig("feedback.enable_types").then(r => {
        Iif (r) {
          setValue(r.value as string);
        }
      });
    }
    const fetchIpAddress = async (): Promise<void> => {
      const cacheKey = `${SERVER_URL}_ipAddress`;
      try {
        // Check if the ipAddress is stored in the local storage
        const cachedIpAddress = localStorage.getItem(cacheKey);
        Iif (cachedIpAddress) {
          setIpAddress(cachedIpAddress);
        } else {
          const response = await fetch('https://api.ipify.org?format=json');
          const data = await response.json();
          setIpAddress(data.ip);
          localStorage.setItem(cacheKey, data.ip);
        }
      } catch (error) {
        console.error(error);
        setIpAddress('');
      }
    };
 
    fetchIpAddress().then(r => r);
  }, []);
 
 
  interface MenuItemLabelProps {
    icon: ReactElement;
    text: string;
    href?: string;
    target?: HTMLAttributeAnchorTarget;
  }
 
  const MenuItemLabel: React.FC<MenuItemLabelProps> = ({ icon, text, href, target }: MenuItemLabelProps) => (
    <Space direction="horizontal">
      {icon}
      <a rel="noopener noreferrer" href={href ?? "#/"} target={target}>
        {text}
      </a>
    </Space>
  );
 
  const items: ExtendedMenuProps['items'] = [
    {
      key: 'QUESTION',
      authorized: true,
      label: (
        <MenuItemLabel
          icon={<CustomerServiceOutlined />}
          text={t('Function consultation')}
        />
      ),
    },
    {
      key: 'BUG',
      authorized: true,
      label: (
        <MenuItemLabel
          icon={<BugOutlined />}
          text={t('Report bug')}
        />
      ),
    },
    {
      key: 'SUGGESTION',
      authorized: true,
      label: (
        <MenuItemLabel
          icon={<BulbOutlined />}
          text={t('Feature request')}
        />
      ),
    },
    {
      key: 'PURCHASE_CONSULT',
      authorized: true,
      label: (
        <MenuItemLabel
          icon={<UserOutlined />}
          text={t('Purchase consultation')}
        />
      ),
    },
    {
      key: 'BUSINESS_COOPERATION',
      authorized: true,
      label: (
        <MenuItemLabel
          icon={<PhoneOutlined />}
          text={t('Business cooperation')}
        />
      ),
    },
  ];
 
  const alwaysShowItems = [
    {
      key: '210',
      authorized: false,
      label: (
        <MenuItemLabel
          icon={<ReadOutlined />}
          text={t('User manual')}
          href={(userManualLink as string) ?? 'https://docs.muyan.io'}
          target="_blank"
        />
      ),
    },
    {
      key: '220',
      authorized: false,
      label: (
        <MenuItemLabel
          icon={<CodeSandboxOutlined />}
          text={t('Developer manual')}
          href={(developerManualLink as string) ?? 'https://docs.muyan.io'}
          target="_blank"
        />
      ),
    },
    {
      key: '230',
      authorized: false,
      label: <SetBackendModal visibleProp={false} />,
    },
    {
      key: '240',
      authorized: false,
      label: (
        <Space>
          <DeploymentUnitOutlined />版本: {process.env.REACT_APP_VERSION}
        </Space>
      ),
    },
    // Add link to https://next.muyan.io, our next generation UI
    {
      key: '250',
      authorized: false,
      label: (
        <MenuItemLabel
          icon={<LinkOutlined />}
          text={t('Next generation UI')}
          href="https://next.muyan.io"
          target="_blank"
        />
      ),
    },
  ];
 
  const enableTypes = (value as string)?.split(',') || [];
  const filteredItems = items.filter(item => enableTypes.includes(item.key as string) &&
    (item.authorized === authorized || item.authorized === false));
  // 如果显示的反馈类型不为空,则添加一个分割线
  Iif (filteredItems.length > 0) {
    filteredItems.push({
      key: '200',
      authorized: false,
      label: (<span style={{ cursor: 'arrow' }} ><hr /></span>),
    });
  }
  const displayMenus = [...filteredItems, ...alwaysShowItems];
  // 从数组中过滤掉不需要显示的属性(authorized)
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const trimed = displayMenus.map(({ authorized, ..._ }) => _);
 
  return (
    <div className="affix-footer" >
      <Dropdown
        menu={{
          items: trimed,
          onClick: ({ key }) => {
            if (enableTypes.includes(key as string)) {
              setShowFeedbackType(key as FeedbackType);
            }
          },
        }}
        placement="top"
        arrow={false}
        trigger={['click']}
        overlayStyle={{
          zIndex: zIndex + 1,
        }}
      >
        <QuestionCircleOutlined
          onClick={() => setShowMenu(!showMenu)}
        />
      </Dropdown>
      {showFeedbackType && (<FeedbackForm
        zIndex={zIndex + 1}
        type={showFeedbackType}
        onCancelCallback={() => setShowFeedbackType(undefined)}
        ipAddress={ipAddress}
      />)}
    </div>
  );
};