Skip to content
Snippets Groups Projects
LoggedInUser.tsx 2.88 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useState, useEffect } from 'react';
    
    import { useSelector } from 'react-redux';
    
    James Long's avatar
    James Long committed
    
    
    import { useActions } from '../hooks/useActions';
    
    import { colors, styles, type CSSProperties } from '../style';
    
    import Button from './common/Button';
    import Menu from './common/Menu';
    import Text from './common/Text';
    import View from './common/View';
    
    import { useServerURL } from './ServerContext';
    
    James Long's avatar
    James Long committed
    
    
    type LoggedInUserProps = {
      hideIfNoServer?: boolean;
      style?: CSSProperties;
      color?: string;
    };
    export default function LoggedInUser({
      hideIfNoServer,
      style,
      color,
    }: LoggedInUserProps) {
    
      let userData = useSelector(state => state.user.data);
    
      let { getUserData, signOut, closeBudget } = useActions();
    
    James Long's avatar
    James Long committed
      let [loading, setLoading] = useState(true);
      let [menuOpen, setMenuOpen] = useState(false);
    
    Jed Fox's avatar
    Jed Fox committed
      const serverUrl = useServerURL();
    
    James Long's avatar
    James Long committed
    
      useEffect(() => {
        getUserData().then(() => setLoading(false));
      }, []);
    
      async function onChangePassword() {
        await closeBudget();
    
        window.__navigate('/change-password');
    
      async function onMenuSelect(type) {
    
    James Long's avatar
    James Long committed
        setMenuOpen(false);
    
        switch (type) {
          case 'change-password':
            onChangePassword();
            break;
    
          case 'sign-in':
            await closeBudget();
    
            window.__navigate('/login');
    
    James Long's avatar
    James Long committed
          case 'sign-out':
            signOut();
            break;
    
          case 'config-server':
            await closeBudget();
    
            window.__navigate('/config-server');
    
    James Long's avatar
    James Long committed
          default:
        }
      }
    
    
      function serverMessage() {
    
          return 'No server';
    
    
        if (userData?.offline) {
          return 'Server offline';
        }
    
        return 'Server online';
      }
    
      if (hideIfNoServer && !serverUrl) {
        return null;
    
      if (loading && serverUrl) {
    
    James Long's avatar
    James Long committed
        return (
    
            style={{
              color: colors.n5,
              fontStyle: 'italic',
              ...styles.delayedFadeIn,
              ...style,
            }}
    
    James Long's avatar
    James Long committed
          </Text>
        );
    
    James Long's avatar
    James Long committed
    
    
        <View style={{ flexDirection: 'row', alignItems: 'center', ...style }}>
    
          <Button type="bare" onClick={() => setMenuOpen(true)} style={{ color }}>
    
    
          {menuOpen && (
            <Tooltip
              position="bottom-right"
              style={{ padding: 0 }}
              onClose={() => setMenuOpen(false)}
            >
              <Menu
                onMenuSelect={onMenuSelect}
                items={[
                  serverUrl &&
                    !userData?.offline && {
                      name: 'change-password',
                      text: 'Change password',
                    },
                  serverUrl && { name: 'sign-out', text: 'Sign out' },
                  {
                    name: 'config-server',
                    text: serverUrl ? 'Change server URL' : 'Start using a server',
                  },
                ]}
              />
            </Tooltip>
          )}
        </View>
      );
    
    James Long's avatar
    James Long committed
    }