Saved Command sorting

Kaldek 10 months ago

Depending on a a few factors (we assume the sequence which commands are added to a user account), the list of Saved Commands is in a random order.

Is there any method of enforcing the order of commands listed under Saved Commands for users, or at the very least is there a means of confirming what dictates the command ordering in the menu?

Anton Tananaev 10 months ago

I don't think we support sorting. The order is likely what the database gives us.

Kaldek 10 months ago

Alright, we'll put this one into our queue for working out to solve it and do a PR upstream if we do.

AussieTraccar 10 months ago

My apologies, but I am fairly new to coding and Traccar (A big thank you to Anton and team).

I believe the Saved Commands are referenced by SelectField.jsx. These are the changes that I made to SelectField.jsx:

diff --git a/src/common/components/SelectField.jsx b/src/common/components/SelectField.jsx
index efe4487a..199bfafd 100644
--- a/src/common/components/SelectField.jsx
+++ b/src/common/components/SelectField.jsx
@@ -32,7 +32,9 @@ const SelectField = ({
   useEffectAsync(async () => {
     if (endpoint) {
       const response = await fetchOrThrow(endpoint);
-      setItems(await response.json());
+        const data = await response.json();
+        data.sort((a, b) => titleGetter(a).localeCompare(titleGetter(b)));
+        setItems(data);
     }
   }, []);

There is also sorting that can be applied to LinkField.jsx (device and user connections etc). These are the changes that I made to LinkField.jsx:

diff --git a/src/common/components/LinkField.jsx b/src/common/components/LinkField.jsx
index 4317ecf7..9a99d198 100644
--- a/src/common/components/LinkField.jsx
+++ b/src/common/components/LinkField.jsx
@@ -25,7 +25,9 @@ const LinkField = ({
   useEffectAsync(async () => {
     if (active) {
       const response = await fetchOrThrow(endpointAll);
-      setItems(await response.json());
+        const data = await response.json();
+        data.sort((a, b) => titleGetter(a).localeCompare(titleGetter(b)));
+        setItems(data);
     }
   }, [active]);

Another place that sorting that can be applied to, is ColumnSelect.jsx (reports columns). These are the changes that I made to ColumnSelect.jsx:

diff --git a/src/reports/components/ColumnSelect.jsx b/src/reports/components/ColumnSelect.jsx
index da39b420..4766ee49 100644
--- a/src/reports/components/ColumnSelect.jsx
+++ b/src/reports/components/ColumnSelect.jsx
@@ -21,7 +21,7 @@ const ColumnSelect = ({
           multiple
           disabled={disabled}
         >
-          {columnsArray.map(([key, string]) => (
+          {columnsArray.sort((a, b) => a.toString().localeCompare(b)).map(([key, string]) => (
             <MenuItem key={key} value={key}>{rawValues ? string : t(string)}</MenuItem>
           ))}
         </Select>

Not that I have ever done a PR before, but it might be of some benefit and worth down the track?