-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upload image after switching accounts from comment editor (#1855)
- Loading branch information
Showing
4 changed files
with
208 additions
and
98 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
88 changes: 88 additions & 0 deletions
88
src/features/shared/markdown/editing/modal/contents/TemporarySelectedAccountContext.tsx
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,88 @@ | ||
import { useIonModal } from "@ionic/react"; | ||
import { noop } from "es-toolkit"; | ||
import { LemmyHttp } from "lemmy-js-client"; | ||
import { createContext, useContext, useState } from "react"; | ||
|
||
import AccountSwitcher from "#/features/auth/AccountSwitcher"; | ||
import { | ||
loggedInAccountsSelector, | ||
userHandleSelector, | ||
} from "#/features/auth/authSelectors"; | ||
import { Credential } from "#/features/auth/authSlice"; | ||
import { getClient } from "#/services/lemmy"; | ||
import { useAppSelector } from "#/store"; | ||
|
||
const TemporarySelectedAccountContext = createContext<{ | ||
account: Credential | undefined; | ||
accountClient: LemmyHttp | undefined; | ||
presentAccountSwitcher: (onDidDismiss: () => void) => void; | ||
}>({ | ||
account: undefined, | ||
accountClient: undefined, | ||
presentAccountSwitcher: noop, | ||
}); | ||
|
||
export function useTemporarySelectedAccount() { | ||
return useContext(TemporarySelectedAccountContext); | ||
} | ||
|
||
export function TemporarySelectedAccountProvider({ | ||
children, | ||
onSelectAccount, | ||
}: { | ||
children: React.ReactNode; | ||
onSelectAccount: (account: string) => Promise<void>; | ||
}) { | ||
const userHandle = useAppSelector(userHandleSelector); | ||
const [selectedAccountHandle, setSelectedAccountHandle] = | ||
useState(userHandle); | ||
|
||
const accounts = useAppSelector(loggedInAccountsSelector); | ||
const selectedAccount = accounts?.find( | ||
({ handle }) => handle === selectedAccountHandle, | ||
); | ||
|
||
const selectedAccountJwt = selectedAccount?.jwt; | ||
const selectedAccountClient = (() => { | ||
if (!selectedAccountHandle) return; | ||
|
||
const instance = selectedAccountHandle.split("@")[1]!; | ||
|
||
return getClient(instance, selectedAccountJwt); | ||
})(); | ||
|
||
const [presentAccountSwitcher, onDismissAccountSwitcher] = useIonModal( | ||
AccountSwitcher, | ||
{ | ||
allowEdit: false, | ||
showGuest: false, | ||
activeHandle: selectedAccountHandle, | ||
onDismiss: (data?: string, role?: string) => | ||
onDismissAccountSwitcher(data, role), | ||
onSelectAccount: async (account: string) => { | ||
onSelectAccount(account); | ||
|
||
setSelectedAccountHandle(account); | ||
}, | ||
}, | ||
); | ||
|
||
return ( | ||
<TemporarySelectedAccountContext.Provider | ||
value={{ | ||
account: selectedAccount, | ||
accountClient: selectedAccountClient, | ||
presentAccountSwitcher: (onDidDismiss) => { | ||
if (accounts?.length === 1) return; | ||
|
||
presentAccountSwitcher({ | ||
cssClass: "small", | ||
onDidDismiss, | ||
}); | ||
}, | ||
}} | ||
> | ||
{children} | ||
</TemporarySelectedAccountContext.Provider> | ||
); | ||
} |
Oops, something went wrong.