11import { useMutation , useQuery } from "@tanstack/react-query" ;
22import { openUrl } from "@tauri-apps/plugin-opener" ;
33import { AlertCircleIcon , ArrowRightIcon , CheckIcon } from "lucide-react" ;
4- import { useMemo } from "react" ;
4+ import { useEffect , useMemo } from "react" ;
55
66import {
77 commands as appleCalendarCommands ,
@@ -188,43 +188,49 @@ function useAppleCalendarSelection() {
188188 const calendars = main . UI . useTable ( "calendars" , main . STORE_ID ) ;
189189 const { user_id } = main . UI . useValues ( main . STORE_ID ) ;
190190
191- const { mutate : syncCalendars , isPending } = useMutation ( {
192- mutationKey : [ "appleCalendars" , "sync" ] ,
193- mutationFn : async ( ) => {
191+ const {
192+ data : incomingCalendars ,
193+ refetch,
194+ isFetching,
195+ } = useQuery ( {
196+ queryKey : [ "appleCalendars" ] ,
197+ queryFn : async ( ) => {
194198 const [ result ] = await Promise . all ( [
195199 appleCalendarCommands . listCalendars ( ) ,
196- new Promise ( ( resolve ) => setTimeout ( resolve , 250 ) ) ,
200+ new Promise ( ( resolve ) => setTimeout ( resolve , 150 ) ) ,
197201 ] ) ;
202+
198203 if ( result . status === "error" ) {
199204 throw new Error ( result . error ) ;
200205 }
201206 return result . data ;
202207 } ,
203- onSuccess : ( incomingCalendars ) => {
204- if ( ! store || ! user_id ) return ;
208+ } ) ;
205209
206- store . transaction ( ( ) => {
207- for ( const cal of incomingCalendars ) {
208- const existingRowId = findCalendarByTrackingId ( store , cal . id ) ;
209- const rowId = existingRowId ?? crypto . randomUUID ( ) ;
210- const existing = existingRowId
211- ? store . getRow ( "calendars" , existingRowId )
212- : null ;
210+ useEffect ( ( ) => {
211+ if ( ! incomingCalendars || ! store || ! user_id ) return ;
213212
214- store . setRow ( "calendars" , rowId , {
215- user_id,
216- created_at : existing ?. created_at || new Date ( ) . toISOString ( ) ,
217- tracking_id_calendar : cal . id ,
218- name : cal . title ,
219- enabled : existing ?. enabled ?? false ,
220- provider : "apple" ,
221- source : cal . source . title ,
222- color : colorToCSS ( cal . color ) ,
223- } ) ;
224- }
225- } ) ;
226- } ,
227- } ) ;
213+ store . transaction ( ( ) => {
214+ for ( const cal of incomingCalendars ) {
215+ const existingRowId = findCalendarByTrackingId ( store , cal . id ) ;
216+ const rowId = existingRowId ?? crypto . randomUUID ( ) ;
217+ const existing = existingRowId
218+ ? store . getRow ( "calendars" , existingRowId )
219+ : null ;
220+
221+ store . setRow ( "calendars" , rowId , {
222+ user_id,
223+ created_at : existing ?. created_at || new Date ( ) . toISOString ( ) ,
224+ tracking_id_calendar : cal . id ,
225+ name : cal . title ,
226+ enabled : existing ?. enabled ?? false ,
227+ provider : "apple" ,
228+ source : cal . source . title ,
229+ color : colorToCSS ( cal . color ) ,
230+ } ) ;
231+ }
232+ } ) ;
233+ } , [ incomingCalendars , store , user_id ] ) ;
228234
229235 const groups = useMemo ( ( ) : CalendarGroup [ ] => {
230236 const appleCalendars = Object . entries ( calendars ) . filter (
@@ -256,8 +262,8 @@ function useAppleCalendarSelection() {
256262 return {
257263 groups,
258264 handleToggle,
259- handleRefresh : syncCalendars ,
260- isLoading : isPending ,
265+ handleRefresh : refetch ,
266+ isLoading : isFetching ,
261267 } ;
262268}
263269
@@ -281,7 +287,7 @@ function DocumentationLink({ href }: { href: string }) {
281287 onClick = { ( ) => openUrl ( href ) }
282288 className = "mb-3 text-xs text-neutral-500 hover:text-neutral-700 hover:underline"
283289 >
284- Read the docs →
290+ Read the docs ↗
285291 </ button >
286292 ) ;
287293}
0 commit comments