1
- import React , { useEffect , useState } from "react" ;
2
1
import {
3
2
Action ,
4
3
ActionPanel ,
@@ -18,18 +17,18 @@ import providers from "./providers";
18
17
import Package from "./package" ;
19
18
import { Track } from "./track" ;
20
19
import AddCommand from "./add-package-to-track" ;
21
- import { getTracking , removeTracking } from "./storage " ;
20
+ import { useLocalStorage } from "@raycast/utils " ;
22
21
23
22
export default function TrackCommand ( ) {
24
- const [ tracking , setTracking ] = useState < Track [ ] > ( [ ] ) ;
25
-
26
- useEffect ( ( ) => {
27
- fetchTracking ( setTracking ) ;
28
- } , [ ] ) ;
23
+ const {
24
+ value : tracking ,
25
+ setValue : setTracking ,
26
+ isLoading ,
27
+ } = useLocalStorage < Track [ ] > ( "tracking" , environment . isDevelopment ? sortTracking ( tempData ) : [ ] ) ;
29
28
30
29
return (
31
- < List >
32
- { tracking . map ( ( item ) => (
30
+ < List isLoading = { isLoading } >
31
+ { tracking ? .map ( ( item ) => (
33
32
< List . Item
34
33
key = { item . id }
35
34
id = { item . id . toString ( ) }
@@ -58,8 +57,7 @@ export default function TrackCommand() {
58
57
title = "Track New Delivery"
59
58
icon = { Icon . Plus }
60
59
shortcut = { Keyboard . Shortcut . Common . New }
61
- target = { < AddCommand /> }
62
- onPop = { ( ) => fetchTracking ( setTracking ) }
60
+ target = { < AddCommand props = { { tracking, setTracking, isLoading } } /> }
63
61
/>
64
62
</ ActionPanel >
65
63
}
@@ -69,23 +67,15 @@ export default function TrackCommand() {
69
67
) ;
70
68
}
71
69
72
- async function fetchTracking ( setTracking : React . Dispatch < React . SetStateAction < Track [ ] > > ) {
73
- let tracking : Track [ ] = await getTracking ( ) ;
74
-
75
- if ( environment . isDevelopment ) {
76
- // running the development version
77
- tracking = tracking . concat ( tempData ) ;
78
- }
79
-
80
- const sortedTracking = sortTracking ( tracking ) ;
81
- setTracking ( sortedTracking ) ;
82
- }
83
-
84
70
async function deleteTracking (
85
71
id : string ,
86
- tracking : Track [ ] ,
87
- setTracking : React . Dispatch < React . SetStateAction < Track [ ] > > ,
72
+ tracking : Track [ ] | undefined ,
73
+ setTracking : ( value : Track [ ] ) => Promise < void > ,
88
74
) {
75
+ if ( ! tracking ) {
76
+ return ;
77
+ }
78
+
89
79
const nameOfTrackToDelete = tracking . find ( ( track ) => track . id === id ) ?. name ?? "Unknown" ;
90
80
91
81
const options : Alert . Options = {
@@ -103,8 +93,8 @@ async function deleteTracking(
103
93
return ;
104
94
}
105
95
106
- await removeTracking ( id ) ;
107
- await fetchTracking ( setTracking ) ;
96
+ const reducedTracking = tracking . filter ( ( track ) => track . id !== id ) ;
97
+ await setTracking ( reducedTracking ) ;
108
98
109
99
await showToast ( {
110
100
style : Toast . Style . Success ,
0 commit comments