-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added consent work for groups Updated AppConfig Cleaned up some push handling for now
- Loading branch information
Alex Risch
authored and
Alex Risch
committed
May 28, 2024
1 parent
b9d4909
commit e2b24e8
Showing
7 changed files
with
132 additions
and
60 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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {useCallback, useEffect, useState} from 'react'; | ||
import {AppConfig} from '../consts/AppConfig'; | ||
import {mmkvStorage} from '../services/mmkvStorage'; | ||
import {useClient} from './useClient'; | ||
import {useGroup} from './useGroup'; | ||
|
||
type ConsentState = 'allowed' | 'denied' | 'unknown'; | ||
|
||
const getInitialConsent = (groupId?: string): ConsentState => { | ||
if (!AppConfig.GROUP_CONSENT || !groupId) { | ||
return 'allowed'; | ||
} | ||
return (mmkvStorage.getGroupConsent(groupId) as ConsentState) ?? 'unknown'; | ||
}; | ||
|
||
export const useGroupConsent = (topic: string) => { | ||
const {data: group} = useGroup(topic); | ||
const {client} = useClient(); | ||
const [consent, setConsent] = useState<ConsentState>( | ||
getInitialConsent(group?.id), | ||
); | ||
|
||
useEffect(() => { | ||
if (!group) { | ||
return; | ||
} | ||
if (!AppConfig.GROUP_CONSENT) { | ||
setConsent('allowed'); | ||
return; | ||
} | ||
group.consentState().then(currentConsent => { | ||
setConsent(currentConsent); | ||
mmkvStorage.saveGroupConsent(group.id, currentConsent); | ||
}); | ||
}, [group, topic]); | ||
|
||
const allow = useCallback(async () => { | ||
if (!group?.id) { | ||
return; | ||
} | ||
await client?.contacts.allowGroups([group.id]); | ||
setConsent('allowed'); | ||
mmkvStorage.saveGroupConsent(group.id, 'allowed'); | ||
}, [client?.contacts, group?.id]); | ||
|
||
const deny = useCallback(async () => { | ||
if (!group?.id) { | ||
return; | ||
} | ||
await client?.contacts.denyGroups([group.id]); | ||
setConsent('denied'); | ||
mmkvStorage.saveGroupConsent(group.id, 'denied'); | ||
}, [client?.contacts, group?.id]); | ||
|
||
return { | ||
consent, | ||
allow, | ||
deny, | ||
}; | ||
}; |
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