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

mendersoftware / gui / 1301920191

23 May 2024 07:13AM UTC coverage: 83.42% (-16.5%) from 99.964%
1301920191

Pull #4421

gitlab-ci

mzedel
fix: fixed an issue that sometimes prevented reopening paginated auditlog links

Ticket: None
Changelog: Title
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4421: MEN-7034 - device information in auditlog entries

4456 of 6367 branches covered (69.99%)

34 of 35 new or added lines in 7 files covered. (97.14%)

1668 existing lines in 162 files now uncovered.

8473 of 10157 relevant lines covered (83.42%)

140.52 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 };
184✔
25

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

33
const FilterOption = (props, option) => {
184✔
34
  let content = getOptionLabel(option);
10✔
35
  if (option.category === 'recently used') {
10!
UNCOV
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>;
10✔
44
};
45

46
const optionsFilter = createFilterOptions();
184✔
47

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

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

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

75
  useEffect(() => {
18✔
76
    setKey(emptyFilter.key);
6✔
77
    setScope(emptyFilter.scope);
6✔
78
    let attributesClean = attributes.map(attr => {
6✔
79
      if (!attr.category && attr.scope) {
16!
UNCOV
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(() => {
18✔
97
    setKey(filter.key);
4✔
98
  }, [filter.key]);
99

100
  useEffect(() => {
18✔
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) => {
18✔
UNCOV
109
    if (!value) {
×
UNCOV
110
      return removeFilter();
×
111
    }
UNCOV
112
    const { key = value, scope: fallbackScope } = attributes.find(filter => filter.key === value) ?? {};
×
UNCOV
113
    setKey(key);
×
UNCOV
114
    setScope(selectedScope || fallbackScope);
×
115
  };
116

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

124
  return (
18✔
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}
10✔
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
        }
UNCOV
146
        updateFilterKey(key, scope);
×
147
      }}
148
      options={options}
149
      renderInput={params => <TextField {...params} label={label} style={textFieldStyle} />}
30✔
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