-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ops: 411k and updating storage state
- Loading branch information
Showing
17 changed files
with
338 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
[%zuse 412] | ||
[%zuse 411] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,53 @@ | ||
import _ from 'lodash'; | ||
import { api } from '../api'; | ||
import { reduce } from './reducer'; | ||
import { enableMapSet } from 'immer'; | ||
import { createState, createSubscription, reduceStateN } from '../base'; | ||
import { S3Credentials } from '@urbit/api'; | ||
import { BaseStorageState, StorageUpdate } from './types'; | ||
import reduce from './reducer'; | ||
import { | ||
createState, | ||
createSubscription, | ||
reduceStateN, | ||
BaseState, | ||
} from '../base'; | ||
|
||
enableMapSet(); | ||
|
||
export interface GcpToken { | ||
accessKey: string; | ||
expiresIn: number; | ||
} | ||
|
||
export interface StorageState { | ||
loaded: boolean; | ||
hasCredentials: boolean; | ||
gcp: { | ||
configured?: boolean; | ||
token?: GcpToken; | ||
isConfigured: () => Promise<boolean>; | ||
getToken: () => Promise<void>; | ||
}; | ||
s3: { | ||
configuration: { | ||
buckets: Set<string>; | ||
currentBucket: string; | ||
region: string; | ||
}; | ||
credentials: S3Credentials | null; | ||
}; | ||
} | ||
|
||
let numLoads = 0; | ||
|
||
// @ts-ignore investigate zustand types | ||
const useStorageState = createState<StorageState>( | ||
export type StorageState = BaseStorageState & BaseState<BaseStorageState>; | ||
|
||
export const useStorage = createState<BaseStorageState>( | ||
'Storage', | ||
(set, get) => ({ | ||
() => ({ | ||
loaded: false, | ||
hasCredentials: false, | ||
gcp: { | ||
isConfigured: () => { | ||
return api.thread({ | ||
inputMark: 'noun', | ||
outputMark: 'json', | ||
threadName: 'gcp-is-configured', | ||
body: {} | ||
}); | ||
}, | ||
getToken: async () => { | ||
const token = await api.thread<GcpToken>({ | ||
inputMark: 'noun', | ||
outputMark: 'gcp-token', | ||
threadName: 'gcp-get-token', | ||
body: {} | ||
}); | ||
get().set((state) => { | ||
state.gcp.token = token; | ||
}); | ||
} | ||
}, | ||
s3: { | ||
configuration: { | ||
buckets: new Set(), | ||
currentBucket: '', | ||
region: '' | ||
region: '', | ||
publicUrlBase: '', | ||
presignedUrl: '', | ||
service: 'credentials', | ||
}, | ||
credentials: null | ||
} | ||
credentials: null, | ||
}, | ||
}), | ||
['loaded', 's3', 'gcp'], | ||
['loaded', 'hasCredentials', 's3'], | ||
[ | ||
(set, get) => | ||
createSubscription('storage', '/all', (e) => { | ||
const d = _.get(e, 'storage-update', false); | ||
if (d) { | ||
reduceStateN(get(), d, reduce); | ||
} | ||
|
||
numLoads++; | ||
if (numLoads === 2) { | ||
set({ loaded: true }); | ||
createSubscription( | ||
'storage', | ||
'/all', | ||
(e: { 'storage-update': StorageUpdate }) => { | ||
const data = _.get(e, 'storage-update', false); | ||
if (data) { | ||
reduceStateN(get(), data, reduce); | ||
} | ||
numLoads += 1; | ||
if (numLoads === 2) { | ||
set({ loaded: true }); | ||
} | ||
} | ||
}) | ||
), | ||
] | ||
); | ||
|
||
export default useStorageState; |
Oops, something went wrong.