Skip to content
Snippets Groups Projects
NotesButton.js 2.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • James Long's avatar
    James Long committed
    import React, { useState, useEffect, useMemo } from 'react';
    
    James Long's avatar
    James Long committed
    import { css } from 'glamor';
    
    James Long's avatar
    James Long committed
    import q from 'loot-core/src/client/query-helpers';
    
    import { useLiveQuery } from 'loot-core/src/client/query-hooks';
    import { send } from 'loot-core/src/platform/client/fetch';
    
    James Long's avatar
    James Long committed
    import { colors } from '../style';
    
    import CustomNotesPaper from '../svg/v2/CustomNotesPaper';
    
    import { View, Button, Tooltip, useTooltip, Text } from './common';
    
    James Long's avatar
    James Long committed
    
    export function NotesTooltip({
    
    James Long's avatar
    James Long committed
      defaultNotes,
      position = 'bottom-left',
      onClose
    }) {
      let [notes, setNotes] = useState(defaultNotes);
      let inputRef = React.createRef();
    
      useEffect(() => {
    
        if (editable) {
          inputRef.current.focus();
        }
      }, [inputRef, editable]);
    
    James Long's avatar
    James Long committed
    
      return (
        <Tooltip position={position} onClose={() => onClose(notes)}>
    
          {editable ? (
            <textarea
              ref={inputRef}
              {...css({
                border: '1px solid ' + colors.border,
                padding: 7,
                minWidth: 300,
                minHeight: 120,
                outline: 'none'
              })}
              value={notes || ''}
              onChange={e => setNotes(e.target.value)}
            ></textarea>
          ) : (
            <Text
              {...css({
                display: 'block',
                maxWidth: 225,
                padding: 8,
                whiteSpace: 'pre-wrap',
                overflowWrap: 'break-word'
              })}
            >
              {notes}
            </Text>
          )}
    
    James Long's avatar
    James Long committed
        </Tooltip>
      );
    }
    
    export default function NotesButton({
      id,
      width = 12,
      height = 12,
      defaultColor = colors.n8,
      tooltipPosition,
      style
    }) {
    
      let [hover, setHover] = useState(false);
    
    James Long's avatar
    James Long committed
      let tooltip = useTooltip();
      let { data } = useLiveQuery(
    
        useMemo(() => q('notes').filter({ id }).select('*'), [id])
    
    James Long's avatar
    James Long committed
      );
      let note = data && data.length > 0 ? data[0].note : null;
      let hasNotes = note && note !== '';
    
      function onClose(notes) {
        send('notes-save', { id, note: notes });
        tooltip.close();
      }
    
    
      // This account for both the tooltip hover, and editing tooltip
      const tooltipOpen = tooltip.isOpen || (hasNotes && hover);
    
    
    James Long's avatar
    James Long committed
      return (
        <View
          style={[
            { flexShrink: 0 },
    
    James Long's avatar
    James Long committed
              '& button, & .hover-visible': {
                display: 'flex',
                opacity: 1,
                color: colors.n1
              }
            },
            hasNotes && {
              '& button, & .hover-visible': { display: 'flex', opacity: 1 }
            }
          ]}
    
          onMouseEnter={() => setHover(true)}
          onMouseLeave={() => setHover(false)}
    
    James Long's avatar
    James Long committed
        >
          <Button
            bare
            className="hover-visible"
            style={[{ color: defaultColor }, style]}
            {...tooltip.getOpenEvents()}
          >
            <CustomNotesPaper style={{ width, height, color: 'currentColor' }} />
          </Button>
    
    James Long's avatar
    James Long committed
            <NotesTooltip
    
              editable={tooltip.isOpen}
    
    James Long's avatar
    James Long committed
              defaultNotes={note}
              position={tooltipPosition}
              onClose={onClose}
            />
          )}
        </View>
      );
    }