~cytrogen/masto-fe

ref: 20a89f1c8eefa0f766a4650be4d2e5f1fa92ee71 masto-fe/app/javascript/flavours/glitch/components/avatar_composite.jsx -rw-r--r-- 2.2 KiB
20a89f1c — Cytrogen [feature] Bookmark folders UI 9 days ago
                                                                                
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
import PropTypes from "prop-types";
import { PureComponent } from "react";

import ImmutablePropTypes from "react-immutable-proptypes";

import { autoPlayGif } from "flavours/glitch/initial_state";

export default class AvatarComposite extends PureComponent {

  static propTypes = {
    accounts: ImmutablePropTypes.list.isRequired,
    animate: PropTypes.bool,
    size: PropTypes.number.isRequired,
  };

  static defaultProps = {
    animate: autoPlayGif,
  };

  renderItem (account, size, index) {
    const { animate } = this.props;

    let width  = 50;
    let height = 100;
    let top    = "auto";
    let left   = "auto";
    let bottom = "auto";
    let right  = "auto";

    if (size === 1) {
      width = 100;
    }

    if (size === 4 || (size === 3 && index > 0)) {
      height = 50;
    }

    if (size === 2) {
      if (index === 0) {
        right = "1px";
      } else {
        left = "1px";
      }
    } else if (size === 3) {
      if (index === 0) {
        right = "1px";
      } else if (index > 0) {
        left = "1px";
      }

      if (index === 1) {
        bottom = "1px";
      } else if (index > 1) {
        top = "1px";
      }
    } else if (size === 4) {
      if (index === 0 || index === 2) {
        right = "1px";
      }

      if (index === 1 || index === 3) {
        left = "1px";
      }

      if (index < 2) {
        bottom = "1px";
      } else {
        top = "1px";
      }
    }

    const style = {
      left: left,
      top: top,
      right: right,
      bottom: bottom,
      width: `${width}%`,
      height: `${height}%`,
      backgroundSize: "cover",
      backgroundImage: `url(${account.get(animate ? "avatar" : "avatar_static")})`,
    };

    return (
      <div key={account.get("id")} style={style} data-avatar-of={`@${account.get("acct")}`} />
    );
  }

  render() {
    const { accounts, size } = this.props;

    return (
      <div className='account__avatar-composite' style={{ width: `${size}px`, height: `${size}px` }}>
        {accounts.take(4).map((account, i) => this.renderItem(account, Math.min(accounts.size, 4), i))}

        {accounts.size > 4 && (
          <span className='account__avatar-composite__label'>
            +{accounts.size - 4}
          </span>
        )}
      </div>
    );
  }

}