forked from aeharding/voyager
-
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.
* Add initial account switcher * Fix account switching * Add fixes for account switching edge cases when tabs open on posts
- Loading branch information
Showing
38 changed files
with
451 additions
and
95 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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
IonButton, | ||
IonIcon, | ||
IonItem, | ||
IonItemOption, | ||
IonItemOptions, | ||
IonItemSliding, | ||
IonRadio, | ||
ItemSlidingCustomEvent, | ||
} from "@ionic/react"; | ||
import { removeCircle } from "ionicons/icons"; | ||
import { Credential, logoutAccount } from "./authSlice"; | ||
import { useAppDispatch } from "../../store"; | ||
import { useRef } from "react"; | ||
import styled from "@emotion/styled"; | ||
|
||
const RemoveIcon = styled(IonIcon)` | ||
position: relative; | ||
font-size: 1.5rem; | ||
&:after { | ||
z-index: -1; | ||
content: ""; | ||
position: absolute; | ||
inset: 5px; | ||
border-radius: 50%; | ||
background: white; | ||
} | ||
`; | ||
|
||
interface AccountProps { | ||
editing: boolean; | ||
account: Credential; | ||
} | ||
|
||
export default function Account({ editing, account }: AccountProps) { | ||
const dispatch = useAppDispatch(); | ||
const slidingRef = useRef<ItemSlidingCustomEvent["target"]>(null); | ||
|
||
return ( | ||
<IonItemSliding ref={slidingRef}> | ||
<IonItemOptions | ||
side="end" | ||
onIonSwipe={(e) => { | ||
dispatch(logoutAccount(e.detail.value)); | ||
}} | ||
> | ||
<IonItemOption | ||
color="danger" | ||
expandable | ||
onClick={() => { | ||
dispatch(logoutAccount(account.handle)); | ||
}} | ||
> | ||
Log out | ||
</IonItemOption> | ||
</IonItemOptions> | ||
<IonItem> | ||
{editing && ( | ||
<IonButton | ||
color="none" | ||
slot="start" | ||
onClick={() => { | ||
slidingRef.current?.open("end"); | ||
}} | ||
> | ||
<RemoveIcon icon={removeCircle} color="danger" /> | ||
</IonButton> | ||
)} | ||
<IonRadio value={account.handle}>{account.handle}</IonRadio> | ||
</IonItem> | ||
</IonItemSliding> | ||
); | ||
} |
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,91 @@ | ||
import { | ||
IonButton, | ||
IonButtons, | ||
IonContent, | ||
IonHeader, | ||
IonIcon, | ||
IonList, | ||
IonPage, | ||
IonRadioGroup, | ||
IonTitle, | ||
IonToolbar, | ||
useIonModal, | ||
} from "@ionic/react"; | ||
import { add } from "ionicons/icons"; | ||
import { useAppDispatch, useAppSelector } from "../../store"; | ||
import { changeAccount } from "./authSlice"; | ||
import Login from "./Login"; | ||
import { useEffect, useState } from "react"; | ||
import Account from "./Account"; | ||
|
||
interface AccountSwitcherProps { | ||
onDismiss: (data?: string, role?: string) => void; | ||
page: HTMLElement | undefined; | ||
} | ||
|
||
export default function AccountSwitcher({ | ||
onDismiss, | ||
page, | ||
}: AccountSwitcherProps) { | ||
const dispatch = useAppDispatch(); | ||
const accounts = useAppSelector((state) => state.auth.accountData?.accounts); | ||
const activeHandle = useAppSelector( | ||
(state) => state.auth.accountData?.activeHandle | ||
); | ||
const [editing, setEditing] = useState(false); | ||
|
||
const [login, onDismissLogin] = useIonModal(Login, { | ||
onDismiss: (data: string, role: string) => onDismissLogin(data, role), | ||
}); | ||
|
||
useEffect(() => { | ||
if (accounts?.length) return; | ||
|
||
onDismiss(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [accounts]); | ||
|
||
return ( | ||
<IonPage> | ||
<IonHeader> | ||
<IonToolbar> | ||
<IonButtons slot="start"> | ||
{editing ? ( | ||
<IonButton onClick={() => login({ presentingElement: page })}> | ||
<IonIcon icon={add} /> | ||
</IonButton> | ||
) : ( | ||
<IonButton onClick={() => onDismiss()}>Cancel</IonButton> | ||
)} | ||
</IonButtons> | ||
<IonTitle>Accounts</IonTitle> | ||
<IonButtons slot="end"> | ||
{editing ? ( | ||
<IonButton onClick={() => setEditing(false)}>Done</IonButton> | ||
) : ( | ||
<IonButton onClick={() => setEditing(true)}>Edit</IonButton> | ||
)} | ||
</IonButtons> | ||
</IonToolbar> | ||
</IonHeader> | ||
<IonContent> | ||
<IonRadioGroup | ||
value={activeHandle} | ||
onIonChange={(e) => { | ||
dispatch(changeAccount(e.target.value)); | ||
}} | ||
> | ||
<IonList> | ||
{accounts?.map((account) => ( | ||
<Account | ||
key={account.handle} | ||
account={account} | ||
editing={editing} | ||
/> | ||
))} | ||
</IonList> | ||
</IonRadioGroup> | ||
</IonContent> | ||
</IonPage> | ||
); | ||
} |
Oops, something went wrong.