@@ -8,37 +8,47 @@ interface StaticVersions {
88 firsVersion : string ;
99 airlinesVersion : string ;
1010}
11-
1211interface DexieFeature {
1312 id : string ;
1413 feature : FIRFeature | SimAwareTraconFeature ;
1514}
15+ type Manifest = { key : string ; versions : StaticVersions } ;
16+
17+ const R2_BUCKET_URL = process . env . NODE_ENV === "development" ? process . env . NEXT_PUBLIC_R2_BUCKET_URL_DEV : process . env . NEXT_PUBLIC_R2_BUCKET_URL ;
1618
1719const db = new Dexie ( "StaticDatabase" ) as Dexie & {
1820 airports : EntityTable < StaticAirport , "id" > ;
1921 firs : EntityTable < DexieFeature , "id" > ;
2022 tracons : EntityTable < DexieFeature , "id" > ;
2123 airlines : EntityTable < StaticAirline , "id" > ;
24+ manifest : EntityTable < Manifest , "key" > ;
2225} ;
2326
2427db . version ( 1 ) . stores ( {
2528 airports : "id" ,
2629 firs : "id" ,
2730 tracons : "id" ,
2831 airlines : "id" ,
32+ manifest : "key" ,
2933} ) ;
3034
3135export async function dxInitDatabases ( ) : Promise < void > {
32- const serverVersions = await fetchApi < StaticVersions > ( "/static/versions" ) ;
33- const localVersions : StaticVersions = JSON . parse ( localStorage . getItem ( "databaseVersions" ) || "{}" ) ;
34-
35- if ( serverVersions . airportsVersion !== localVersions . airportsVersion ) {
36- const entries = ( await fetchStaticData ( "airports" ) ) as StaticAirport [ ] ;
36+ const latestManifest = await fetchApi < StaticVersions > ( `${ R2_BUCKET_URL } /manifest.json` , {
37+ cache : "no-store" ,
38+ } ) ;
39+ const storedManifest = ( await db . manifest . get ( "databaseVersions" ) ) as Manifest | undefined ;
40+
41+ if ( latestManifest . airportsVersion !== storedManifest ?. versions . airportsVersion ) {
42+ const entries = ( await fetchApi < StaticAirport [ ] > ( `${ R2_BUCKET_URL } /airports_${ latestManifest . airportsVersion } .json` , {
43+ cache : "no-store" ,
44+ } ) ) as StaticAirport [ ] ;
3745 storeData ( entries , db . airports as EntityTable < any , "id" > ) ;
3846 }
3947
40- if ( serverVersions . firsVersion !== localVersions . firsVersion ) {
41- const features = ( await fetchStaticData ( "firs" ) ) as FIRFeature [ ] ;
48+ if ( latestManifest . firsVersion !== storedManifest ?. versions . firsVersion ) {
49+ const features = ( await fetchApi < FIRFeature [ ] > ( `${ R2_BUCKET_URL } /firs_${ latestManifest . firsVersion } .json` , {
50+ cache : "no-store" ,
51+ } ) ) as FIRFeature [ ] ;
4252 const entries : DexieFeature [ ] = features . map ( ( f ) => ( {
4353 id : f . properties . id ,
4454 feature : f ,
@@ -47,8 +57,10 @@ export async function dxInitDatabases(): Promise<void> {
4757 storeData ( entries , db . firs as EntityTable < any , "id" > ) ;
4858 }
4959
50- if ( serverVersions . traconsVersion !== localVersions . traconsVersion ) {
51- const features = ( await fetchStaticData ( "tracons" ) ) as SimAwareTraconFeature [ ] ;
60+ if ( latestManifest . traconsVersion !== storedManifest ?. versions . traconsVersion ) {
61+ const features = ( await fetchApi < SimAwareTraconFeature [ ] > ( `${ R2_BUCKET_URL } /tracons_${ latestManifest . traconsVersion } .json` , {
62+ cache : "no-store" ,
63+ } ) ) as SimAwareTraconFeature [ ] ;
5264 const entries : DexieFeature [ ] = features . map ( ( f ) => ( {
5365 id : f . properties . id ,
5466 feature : f ,
@@ -57,16 +69,14 @@ export async function dxInitDatabases(): Promise<void> {
5769 storeData ( entries , db . tracons as EntityTable < any , "id" > ) ;
5870 }
5971
60- if ( serverVersions . airlinesVersion !== localVersions . airlinesVersion ) {
61- const entries = ( await fetchStaticData ( "airlines" ) ) as StaticAirline [ ] ;
72+ if ( latestManifest . airlinesVersion !== storedManifest ?. versions . airlinesVersion ) {
73+ const entries = ( await fetchApi < StaticAirline [ ] > ( `${ R2_BUCKET_URL } /airlines_${ latestManifest . airlinesVersion } .json` , {
74+ cache : "no-store" ,
75+ } ) ) as StaticAirline [ ] ;
6276 storeData ( entries , db . airlines as EntityTable < any , "id" > ) ;
6377 }
6478
65- localStorage . setItem ( "databaseVersions" , JSON . stringify ( serverVersions ) ) ;
66- }
67-
68- async function fetchStaticData ( type : string ) : Promise < any > {
69- return await fetchApi < any > ( `/static/${ type } ` ) ;
79+ await db . manifest . put ( { key : "databaseVersions" , versions : latestManifest } ) ;
7080}
7181
7282async function storeData ( data : any [ ] , db : EntityTable < any , "id" > ) : Promise < void > {
0 commit comments