@@ -2,6 +2,7 @@ import { type TodoistApiClient, TodoistApiError } from "@/api";
22import type { Label , LabelId } from "@/api/domain/label" ;
33import type { Project , ProjectId } from "@/api/domain/project" ;
44import type { Section , SectionId } from "@/api/domain/section" ;
5+ import type { SyncToken } from "@/api/domain/sync" ;
56import type { Task as ApiTask , CreateTaskParams , TaskId } from "@/api/domain/task" ;
67import type { UserInfo } from "@/api/domain/user" ;
78import { StatusCode , StatusCodes } from "@/api/fetcher" ;
@@ -31,12 +32,6 @@ type DataAccessor = {
3132 labels : RepositoryReader < LabelId , Label > ;
3233} ;
3334
34- class LabelsRepository extends Repository < LabelId , Label > {
35- byName ( name : string ) : Label | undefined {
36- return [ ...this . iter ( ) ] . find ( ( label ) => label . name === name ) ;
37- }
38- }
39-
4035export class TodoistAdapter {
4136 public actions = {
4237 closeTask : async ( id : TaskId ) => await this . closeTask ( id ) ,
@@ -47,18 +42,19 @@ export class TodoistAdapter {
4742 private readonly api : Maybe < TodoistApiClient > = Maybe . Empty ( ) ;
4843 private readonly projects : Repository < ProjectId , Project > ;
4944 private readonly sections : Repository < SectionId , Section > ;
50- private readonly labels : LabelsRepository ;
45+ private readonly labels : Repository < LabelId , Label > ;
5146 private readonly subscriptions : SubscriptionManager < Subscription > ;
5247
5348 private readonly tasksPendingClose : TaskId [ ] ;
5449 private userInfo : UserInfo | undefined ;
5550
5651 private hasSynced = false ;
52+ private syncToken : SyncToken = "*" ;
5753
5854 constructor ( ) {
59- this . projects = new Repository ( ( ) => this . api . withInner ( ( api ) => api . getProjects ( ) ) ) ;
60- this . sections = new Repository ( ( ) => this . api . withInner ( ( api ) => api . getSections ( ) ) ) ;
61- this . labels = new LabelsRepository ( ( ) => this . api . withInner ( ( api ) => api . getLabels ( ) ) ) ;
55+ this . projects = new Repository < ProjectId , Project > ( ) ;
56+ this . sections = new Repository < SectionId , Section > ( ) ;
57+ this . labels = new Repository < LabelId , Label > ( ) ;
6258 this . subscriptions = new SubscriptionManager < Subscription > ( ) ;
6359 this . tasksPendingClose = [ ] ;
6460 }
@@ -81,12 +77,7 @@ export class TodoistAdapter {
8177 return ;
8278 }
8379
84- await Promise . all ( [
85- this . syncUserInfo ( ) ,
86- this . projects . sync ( ) ,
87- this . sections . sync ( ) ,
88- this . labels . sync ( ) ,
89- ] ) ;
80+ await Promise . all ( [ this . syncUserInfo ( ) , this . syncMetadata ( ) ] ) ;
9081
9182 for ( const subscription of this . subscriptions . list ( ) ) {
9283 await subscription . update ( ) ;
@@ -106,6 +97,23 @@ export class TodoistAdapter {
10697 }
10798 }
10899
100+ private async syncMetadata ( ) : Promise < void > {
101+ try {
102+ if ( ! this . api . hasValue ( ) ) {
103+ return ;
104+ }
105+
106+ const response = await this . api . withInner ( ( api ) => api . sync ( this . syncToken ) ) ;
107+
108+ this . projects . applyDiff ( response . projects ) ;
109+ this . sections . applyDiff ( response . sections ) ;
110+ this . labels . applyDiff ( response . labels ) ;
111+ this . syncToken = response . syncToken ;
112+ } catch ( error ) {
113+ console . error ( "Failed to sync metadata:" , error ) ;
114+ }
115+ }
116+
109117 public data ( ) : DataAccessor {
110118 return {
111119 projects : this . projects ,
@@ -191,9 +199,11 @@ const makeUnknownProject = (id: string): Project => {
191199 id,
192200 parentId : null ,
193201 name : "Unknown Project" ,
194- order : Number . MAX_SAFE_INTEGER ,
202+ childOrder : Number . MAX_SAFE_INTEGER ,
195203 inboxProject : false ,
196204 color : "grey" ,
205+ isDeleted : false ,
206+ isArchived : false ,
197207 } ;
198208} ;
199209
@@ -202,7 +212,9 @@ const makeUnknownSection = (id: string): Section => {
202212 id,
203213 projectId : "unknown-project" ,
204214 name : "Unknown Section" ,
205- order : Number . MAX_SAFE_INTEGER ,
215+ sectionOrder : Number . MAX_SAFE_INTEGER ,
216+ isDeleted : false ,
217+ isArchived : false ,
206218 } ;
207219} ;
208220
@@ -211,6 +223,7 @@ const makeUnknownLabel = (): Label => {
211223 id : "unknown-label" ,
212224 name : "Unknown Label" ,
213225 color : "grey" ,
226+ isDeleted : false ,
214227 } ;
215228} ;
216229
0 commit comments