Newer
Older
import React, { useState, useEffect, useMemo } from 'react';
import { useLiveQuery } from 'loot-core/src/client/query-hooks';
import { send } from 'loot-core/src/platform/client/fetch';
import CustomNotesPaper from '../svg/v2/CustomNotesPaper';
import { View, Button, Tooltip, useTooltip } from './common';
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
export function NotesTooltip({
defaultNotes,
position = 'bottom-left',
onClose
}) {
let [notes, setNotes] = useState(defaultNotes);
let inputRef = React.createRef();
useEffect(() => {
inputRef.current.focus();
}, []);
return (
<Tooltip position={position} onClose={() => onClose(notes)}>
<textarea
ref={inputRef}
{...css({
border: '1px solid ' + colors.border,
minWidth: 300,
minHeight: 120,
outline: 'none'
})}
value={notes || ''}
onChange={e => setNotes(e.target.value)}
></textarea>
</Tooltip>
);
}
export default function NotesButton({
id,
width = 12,
height = 12,
defaultColor = colors.n8,
tooltipPosition,
style
}) {
let tooltip = useTooltip();
let { data } = useLiveQuery(
useMemo(() => q('notes').filter({ id }).select('*'), [id])
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
);
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();
}
return (
<View
style={[
{ flexShrink: 0 },
tooltip.isOpen && {
'& button, & .hover-visible': {
display: 'flex',
opacity: 1,
color: colors.n1
}
},
hasNotes && {
'& button, & .hover-visible': { display: 'flex', opacity: 1 }
}
]}
>
<Button
bare
className="hover-visible"
style={[{ color: defaultColor }, style]}
{...tooltip.getOpenEvents()}
>
<CustomNotesPaper style={{ width, height, color: 'currentColor' }} />
</Button>
{tooltip.isOpen && (
<NotesTooltip
defaultNotes={note}
position={tooltipPosition}
onClose={onClose}
/>
)}
</View>
);
}