• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.36
/src/js/components/devices/widgets/attribute-autocomplete.js
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useCallback, useEffect, useRef, useState } from 'react';
15

16
// material ui
17
import { Autocomplete, TextField, createFilterOptions } from '@mui/material';
18

19
import { TIMEOUTS } from '../../../constants/appConstants';
20
import { emptyFilter } from '../../../constants/deviceConstants';
21
import { defaultHeaders } from '../base-devices';
22
import { getFilterLabelByKey } from './filters';
23

24
const textFieldStyle = { marginTop: 0, marginBottom: 15 };
183✔
25

26
export const getOptionLabel = option => {
183✔
27
  const header = Object.values(defaultHeaders).find(
113✔
28
    ({ attribute }) => attribute.scope === option.scope && (attribute.name === option.key || attribute.alternative === option.key)
664✔
29
  );
30
  return header?.title || option.title || option.value || option.key || option;
113✔
31
};
32

33
const FilterOption = (props, option) => {
183✔
34
  let content = getOptionLabel(option);
20✔
35
  if (option.category === 'recently used') {
20!
36
    content = (
×
37
      <div className="flexbox center-aligned space-between" style={{ width: '100%' }}>
38
        <div>{content}</div>
39
        <div className="muted slightly-smaller">({option.scope})</div>
40
      </div>
41
    );
42
  }
43
  return <li {...props}>{content}</li>;
20✔
44
};
45

46
const optionsFilter = createFilterOptions();
183✔
47

48
const filterOptions = (options, params) => {
183✔
49
  const filtered = optionsFilter(options, params);
9✔
50
  if (filtered.length !== 1 && params.inputValue !== '') {
9✔
51
    filtered.push({
4✔
52
      inputValue: params.inputValue,
53
      key: 'custom',
54
      value: `Use "${params.inputValue}"`,
55
      category: 'custom',
56
      priority: 99
57
    });
58
  }
59
  return filtered;
9✔
60
};
61

62
export const AttributeAutoComplete = ({ attributes, disabled = false, filter = emptyFilter, label = 'Attribute', onRemove, onSelect, ...remainder }) => {
183!
63
  const [key, setKey] = useState(filter.key); // this refers to the selected filter with key as the id
14✔
64
  const [options, setOptions] = useState([]);
14✔
65
  const [reset, setReset] = useState(true);
14✔
66
  const [scope, setScope] = useState(filter.scope);
14✔
67
  const timer = useRef();
14✔
68

69
  useEffect(() => {
14✔
70
    return () => {
4✔
71
      clearTimeout(timer.current);
4✔
72
    };
73
  }, []);
74

75
  useEffect(() => {
14✔
76
    setKey(emptyFilter.key);
6✔
77
    setScope(emptyFilter.scope);
6✔
78
    let attributesClean = attributes.map(attr => {
6✔
79
      if (!attr.category && attr.scope) {
16!
80
        attr.category = attr.scope;
×
81
      }
82
      return attr;
16✔
83
    });
84
    setOptions(
6✔
85
      attributesClean.sort((a, b) =>
86
        a.category == b.category
26✔
87
          ? a.priority == b.priority
6!
88
            ? (a.key || '').localeCompare(b.key || '', { sensitivity: 'case' })
12!
89
            : a.priority - b.priority
90
          : (a.category || '').localeCompare(b.category || '', { sensitivity: 'case' })
40!
91
      )
92
    );
93
    // eslint-disable-next-line react-hooks/exhaustive-deps
94
  }, [attributes.length, reset]);
95

96
  useEffect(() => {
14✔
97
    setKey(filter.key);
4✔
98
  }, [filter.key]);
99

100
  useEffect(() => {
14✔
101
    clearTimeout(timer.current);
8✔
102
    timer.current = setTimeout(() => onSelect({ key, scope }), TIMEOUTS.debounceDefault);
8✔
103
    return () => {
8✔
104
      clearTimeout(timer.current);
8✔
105
    };
106
  }, [key, onSelect, scope]);
107

108
  const updateFilterKey = (value, selectedScope) => {
14✔
109
    if (!value) {
×
110
      return removeFilter();
×
111
    }
112
    const { key = value, scope: fallbackScope } = attributes.find(filter => filter.key === value) ?? {};
×
113
    setKey(key);
×
114
    setScope(selectedScope || fallbackScope);
×
115
  };
116

117
  const removeFilter = useCallback(() => {
14✔
118
    if (key) {
×
119
      onRemove({ key, scope });
×
120
    }
121
    setReset(!reset);
×
122
  }, [key, onRemove, reset, setReset, scope]);
123

124
  return (
14✔
125
    <Autocomplete
126
      {...remainder}
127
      autoComplete
128
      autoHighlight
129
      autoSelect
130
      disabled={disabled}
131
      freeSolo
132
      filterSelectedOptions
133
      filterOptions={filterOptions}
134
      getOptionLabel={getOptionLabel}
135
      groupBy={option => option.category}
20✔
136
      renderOption={FilterOption}
137
      id="filter-selection"
138
      includeInputInList={true}
139
      onChange={(e, changedValue) => {
140
        const { inputValue, key = changedValue, scope } = changedValue || {};
1!
141
        if (inputValue) {
1!
142
          // only circumvent updateFilterKey if we deal with a custom attribute - those will be treated as inventory attributes
143
          setKey(inputValue);
1✔
144
          return setScope(emptyFilter.scope);
1✔
145
        }
146
        updateFilterKey(key, scope);
×
147
      }}
148
      options={options}
149
      renderInput={params => <TextField {...params} label={label} style={textFieldStyle} />}
25✔
150
      key={reset}
151
      value={getFilterLabelByKey(key, attributes)}
152
    />
153
  );
154
};
155

156
export default AttributeAutoComplete;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc