|
| 1 | +import {AuthState} from '@codeimage/store/auth/auth'; |
| 2 | +import {provideAppState} from '@codeimage/store/index'; |
| 3 | +import {HStack, Loading, Text, VStack} from '@codeimage/ui'; |
| 4 | +import { |
| 5 | + As, |
| 6 | + Button, |
| 7 | + Dialog, |
| 8 | + DialogPanelContent, |
| 9 | + DialogPanelFooter, |
| 10 | + IconButton, |
| 11 | + Popover, |
| 12 | + PopoverContent, |
| 13 | + PopoverTrigger, |
| 14 | +} from '@codeui/kit'; |
| 15 | +import {ControlledDialogProps} from '@core/hooks/createControlledDialog'; |
| 16 | +import {For, Suspense} from 'solid-js'; |
| 17 | +import {createResource} from 'solid-js'; |
| 18 | +import {deletePasskey, listPasskeys} from '../../../data-access/passkey'; |
| 19 | +import {PencilAlt} from '../../Icons/Pencil'; |
| 20 | +import {TrashIcon} from '../../Icons/TrashIcon'; |
| 21 | +import {KeyIcon} from '../../UserBadge/KeyIcon'; |
| 22 | +import {UserBadge} from '../../UserBadge/UserBadge'; |
| 23 | +import { |
| 24 | + passkeyItem, |
| 25 | + passkeysBox, |
| 26 | + passkeysBoxTitle, |
| 27 | + passkeysList, |
| 28 | + profileBox, |
| 29 | +} from './ProfileDialog.css'; |
| 30 | + |
| 31 | +type ProfileDialog = ControlledDialogProps; |
| 32 | +export function ProfileDialog(props: ProfileDialog) { |
| 33 | + const authState = provideAppState(AuthState); |
| 34 | + const [passkeys, {mutate, refetch}] = createResource(() => listPasskeys()); |
| 35 | + |
| 36 | + const removePasskey = (id: string) => { |
| 37 | + return deletePasskey({params: {credentialId: id}}).then(() => { |
| 38 | + mutate(passkeys => (passkeys ?? []).filter(passkey => passkey.id !== id)); |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <Dialog |
| 44 | + title={'Profile'} |
| 45 | + open={props.isOpen} |
| 46 | + onOpenChange={open => props.onOpenChange(open)} |
| 47 | + size={'lg'} |
| 48 | + > |
| 49 | + <DialogPanelContent> |
| 50 | + <div class={profileBox}> |
| 51 | + <UserBadge /> |
| 52 | + |
| 53 | + {authState().user?.email} |
| 54 | + </div> |
| 55 | + <div class={passkeysBox}> |
| 56 | + <div class={passkeysBoxTitle}> |
| 57 | + <Text size={'lg'} weight={'semibold'}> |
| 58 | + Passkeys |
| 59 | + </Text> |
| 60 | + |
| 61 | + <Button |
| 62 | + theme={'primary'} |
| 63 | + disabled={passkeys.loading} |
| 64 | + size={'sm'} |
| 65 | + leftIcon={<KeyIcon size={'sm'} />} |
| 66 | + onClick={() => |
| 67 | + authState.providers.hanko |
| 68 | + .registerPasskey() |
| 69 | + .then(() => refetch()) |
| 70 | + } |
| 71 | + > |
| 72 | + Add passkey |
| 73 | + </Button> |
| 74 | + </div> |
| 75 | + |
| 76 | + <Suspense fallback={<Loading />}> |
| 77 | + <ul class={passkeysList}> |
| 78 | + <For each={passkeys()}> |
| 79 | + {passkey => ( |
| 80 | + <li class={passkeyItem}> |
| 81 | + {passkey.id} |
| 82 | + <HStack spacing={'1'}> |
| 83 | + <IconButton |
| 84 | + theme={'secondary'} |
| 85 | + size={'xs'} |
| 86 | + aria-label={'Delete'} |
| 87 | + onClick={() => removePasskey(passkey.id)} |
| 88 | + > |
| 89 | + <PencilAlt size={'sm'} /> |
| 90 | + </IconButton> |
| 91 | + |
| 92 | + <Popover> |
| 93 | + <PopoverTrigger asChild> |
| 94 | + <As |
| 95 | + component={IconButton} |
| 96 | + theme={'secondary'} |
| 97 | + size={'xs'} |
| 98 | + aria-label={'Delete'} |
| 99 | + > |
| 100 | + <TrashIcon size={'sm'} /> |
| 101 | + </As> |
| 102 | + </PopoverTrigger> |
| 103 | + <PopoverContent> |
| 104 | + <VStack spacing={'4'}> |
| 105 | + <Text size={'sm'}> |
| 106 | + Do you want to delete this passkey from your |
| 107 | + account? |
| 108 | + </Text> |
| 109 | + <div> |
| 110 | + <Button |
| 111 | + theme={'negative'} |
| 112 | + size={'xs'} |
| 113 | + aria-label={'Delete'} |
| 114 | + onClick={() => removePasskey(passkey.id)} |
| 115 | + > |
| 116 | + Confirm |
| 117 | + </Button> |
| 118 | + </div> |
| 119 | + </VStack> |
| 120 | + </PopoverContent> |
| 121 | + </Popover> |
| 122 | + </HStack> |
| 123 | + </li> |
| 124 | + )} |
| 125 | + </For> |
| 126 | + </ul> |
| 127 | + </Suspense> |
| 128 | + </div> |
| 129 | + </DialogPanelContent> |
| 130 | + <DialogPanelFooter> |
| 131 | + <Button theme={'secondary'} onClick={() => props.onOpenChange(false)}> |
| 132 | + Close |
| 133 | + </Button> |
| 134 | + </DialogPanelFooter> |
| 135 | + </Dialog> |
| 136 | + ); |
| 137 | +} |
0 commit comments