summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/NotificationBanner.tsx
blob: 56efb41611210566393749ce237b782acef52333 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import * as React from 'react';
import { Animated, Button, Component, Styles, Text, Types, UserInterface, View } from 'reactxp';
import { colors } from '../../config.json';
import ImageView from './ImageView';

const styles = {
  collapsible: Styles.createViewStyle({
    backgroundColor: 'rgba(25, 38, 56, 0.95)',
    overflow: 'hidden',
  }),
  drawer: Styles.createViewStyle({
    justifyContent: 'flex-end',
  }),
  container: Styles.createViewStyle({
    flexDirection: 'row',
    paddingTop: 8,
    paddingLeft: 20,
    paddingRight: 10,
    paddingBottom: 8,
  }),
  indicator: {
    base: Styles.createViewStyle({
      width: 10,
      height: 10,
      flex: 0,
      borderRadius: 5,
      marginTop: 4,
      marginRight: 8,
    }),
    warning: Styles.createViewStyle({
      backgroundColor: colors.yellow,
    }),
    success: Styles.createViewStyle({
      backgroundColor: colors.green,
    }),
    error: Styles.createViewStyle({
      backgroundColor: colors.red,
    }),
  },
  textContainer: Styles.createViewStyle({
    flex: 1,
  }),
  actionContainer: Styles.createViewStyle({
    flex: 0,
    flexDirection: 'column',
    justifyContent: 'center',
    marginLeft: 5,
  }),
  actionButton: Styles.createButtonStyle({
    flex: 1,
    justifyContent: 'center',
    cursor: 'default',
    paddingLeft: 5,
    paddingRight: 5,
  }),
  title: Styles.createTextStyle({
    fontFamily: 'Open Sans',
    fontSize: 13,
    fontWeight: '800',
    lineHeight: 18,
    color: colors.white,
  }),
  subtitle: Styles.createTextStyle({
    fontFamily: 'Open Sans',
    fontSize: 13,
    fontWeight: '600',
    lineHeight: 18,
    color: colors.white60,
  }),
};

export class NotificationTitle extends Component {
  public render() {
    return <Text style={styles.title}>{this.props.children}</Text>;
  }
}

export class NotificationSubtitle extends Component {
  public render() {
    return React.Children.count(this.props.children) > 0 ? (
      <Text style={styles.subtitle}>{this.props.children}</Text>
    ) : null;
  }
}

export class NotificationOpenLinkAction extends Component<{ onPress: () => void }> {
  public state = {
    hovered: false,
  };

  public render() {
    return (
      <Button
        style={styles.actionButton}
        onPress={this.props.onPress}
        onHoverStart={this.onHoverStart}
        onHoverEnd={this.onHoverEnd}>
        <ImageView
          height={12}
          width={12}
          tintColor={this.state.hovered ? colors.white80 : colors.white60}
          source="icon-extLink"
        />
      </Button>
    );
  }

  private onHoverStart = () => {
    this.setState({ hovered: true });
  };

  private onHoverEnd = () => {
    this.setState({ hovered: false });
  };
}

export class NotificationContent extends Component {
  public render() {
    return <View style={styles.textContainer}>{this.props.children}</View>;
  }
}

export class NotificationActions extends Component {
  public render() {
    return <View style={styles.actionContainer}>{this.props.children}</View>;
  }
}

export class NotificationIndicator extends Component<{ type: 'success' | 'warning' | 'error' }> {
  public render() {
    return <View style={[styles.indicator.base, styles.indicator[this.props.type]]} />;
  }
}

interface INotificationBannerProps {
  children: React.ReactNode; // Array<NotificationContent | NotificationActions>,
  style?: Types.ViewStyleRuleSet;
  visible: boolean;
  animationDuration: number;
}

interface INotificationBannerState {
  contentPinnedToBottom: boolean;
}

export class NotificationBanner extends Component<
  INotificationBannerProps,
  INotificationBannerState
> {
  public static defaultProps = {
    animationDuration: 350,
  };

  public state = {
    contentPinnedToBottom: false,
  };

  private containerRef = React.createRef<Animated.View>();
  private contentHeight = 0;
  private heightValue = Animated.createValue(0);
  private animationStyle: Types.AnimatedViewStyleRuleSet;
  private animation?: Types.Animated.CompositeAnimation;
  private didFinishFirstLayoutPass = false;

  constructor(props: INotificationBannerProps) {
    super(props);

    this.animationStyle = Styles.createAnimatedViewStyle({
      height: this.heightValue,
    });
  }

  public shouldComponentUpdate(
    nextProps: INotificationBannerProps,
    nextState: INotificationBannerState,
  ) {
    return (
      this.props.children !== nextProps.children ||
      this.props.visible !== nextProps.visible ||
      this.state.contentPinnedToBottom !== nextState.contentPinnedToBottom
    );
  }

  public componentDidUpdate(prevProps: INotificationBannerProps) {
    if (prevProps.visible !== this.props.visible) {
      // enable drawer-like animation when changing banner's visibility
      this.setState({ contentPinnedToBottom: true }, () => {
        this.animateHeightChanges();
      });
    }
  }

  public componentWillUnmount() {
    if (this.animation) {
      this.animation.stop();
    }
  }

  public render() {
    return (
      <Animated.View
        style={[
          styles.collapsible,
          this.state.contentPinnedToBottom ? styles.drawer : undefined,
          this.animationStyle,
          this.props.style,
        ]}
        ref={this.containerRef}>
        <View onLayout={this.onLayout}>
          <View style={styles.container}>{this.props.children}</View>
        </View>
      </Animated.View>
    );
  }

  private onLayout = ({ height }: Types.ViewOnLayoutEvent) => {
    const oldHeight = this.contentHeight;
    this.contentHeight = height;

    // The first layout pass should not be animated because this would cause the initially visible
    // notification banner to slide down each time the component is mounted.
    if (this.didFinishFirstLayoutPass) {
      if (oldHeight !== height) {
        this.animateHeightChanges();
      }
    } else {
      this.didFinishFirstLayoutPass = true;
      if (this.props.visible) {
        this.stopAnimation();
        this.heightValue.setValue(height);
      }
    }
  };

  private async animateHeightChanges() {
    const containerView = this.containerRef.current;
    if (!containerView) {
      return;
    }

    this.stopAnimation();

    // calculate the animation duration based on travel distance
    const layout = await UserInterface.measureLayoutRelativeToWindow(containerView);
    const toValue = this.props.visible ? this.contentHeight : 0;
    const multiplier = Math.abs(toValue - layout.height) / Math.max(1, this.contentHeight);
    const duration = Math.ceil(this.props.animationDuration * multiplier);

    const animation = Animated.timing(this.heightValue, {
      toValue,
      easing: Animated.Easing.InOut(),
      duration,
      useNativeDriver: true,
    });

    this.animation = animation;

    animation.start(({ finished }) => {
      if (finished) {
        // disable drawer-like animations for content updates when the banner is visible
        this.setState({ contentPinnedToBottom: false });
      }
    });
  }

  private stopAnimation() {
    if (this.animation) {
      this.animation.stop();
      this.animation = undefined;
    }
  }
}