Skip to content

Commit

Permalink
fix: get prove post in first setup correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jul 22, 2019
1 parent 4b48e2c commit c7ec481
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/extension/background-script/WelcomeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../database/people'
import { BackupJSONFileLatest } from '../../utils/type-transform/BackupFile'
import { PersonIdentifier } from '../../database/type'
import { MessageCenter } from '../../utils/messages'

OnlyRunInContext('background', 'WelcomeService')
async function generateBackupJSON(identifier: PersonIdentifier, full = false): Promise<BackupJSONFileLatest> {
Expand All @@ -24,10 +25,11 @@ async function generateBackupJSON(identifier: PersonIdentifier, full = false): P
//#region data.whoami
const localKeys = await getLocalKeysDB()
if (localKeys.size === 0) {
// ? New user !
console.log('New user! Generating key pairs')
await generateLocalKeyDB(identifier)
await generateMyIdentityDB(identifier)
// ? New user !
MessageCenter.emit('generateKeyPair', undefined)
console.log('New user! Generating key pairs')
return generateBackupJSON(identifier, full)
}
async function addWhoAmI(data: PersonRecordPublicPrivate) {
Expand Down
22 changes: 15 additions & 7 deletions src/extension/options-page/Welcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { setStorage, LATEST_WELCOME_VERSION } from '../../../components/Welcomes
import { geti18nString } from '../../../utils/i18n'
import { Dialog, withMobileDialog } from '@material-ui/core'
import { Identifier, PersonIdentifier } from '../../../database/type'
import { MessageCenter } from '../../../utils/messages'
import { ValueRef } from '@holoflows/kit/es'
import { useValueRef } from '../../../utils/hooks/useValueRef'

//#region Welcome
enum WelcomeState {
Expand Down Expand Up @@ -123,24 +126,29 @@ function Welcome(props: Welcome) {
}
}
const ResponsiveDialog = withMobileDialog()(Dialog)
const ProvePostRef = new ValueRef('')
const IdentifierRef = new ValueRef(PersonIdentifier.unknown)
const getMyProveBio = async () => {
const post = await Services.Crypto.getMyProveBio(IdentifierRef.value)
if (post) ProvePostRef.value = post
}
MessageCenter.on('generateKeyPair', getMyProveBio)
IdentifierRef.addListener(getMyProveBio)

export default withRouter(function _WelcomePortal(props: RouteComponentProps<{ identifier: string }>) {
const [step, setStep] = useState(WelcomeState.Start)
const [provePost, setProvePost] = useState('')
const [identifier, setIdentifier] = useState<PersonIdentifier>(PersonIdentifier.unknown)
const provePost = useValueRef(ProvePostRef)
const identifier = useValueRef(IdentifierRef)

useEffect(() => {
const raw = new URLSearchParams(props.location.search).get('identifier') || ''
const id = Identifier.fromString(raw)
if (id && id.equals(identifier)) return
if (id instanceof PersonIdentifier) {
setIdentifier(id)
IdentifierRef.value = id
}
}, [props.location.search])

useAsync(() => Services.Crypto.getMyProveBio(identifier), [identifier]).then(
provePost => provePost && setProvePost(provePost),
)

return (
<ResponsiveDialog open>
<Welcome
Expand Down
1 change: 1 addition & 0 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Person } from '../database'
interface UIEvent {}
interface KeyStoreEvent {
newPerson: Person
generateKeyPair: undefined
}
interface TypedMessages extends UIEvent, KeyStoreEvent {}

Expand Down

0 comments on commit c7ec481

Please sign in to comment.