fix: Always return a default user (if there are any added)

This commit is contained in:
LynithDev 2024-08-08 22:05:36 +02:00
parent 444061c0a7
commit 7eb2f74297
6 changed files with 76 additions and 37 deletions

View file

@ -23,9 +23,15 @@ pub async fn remove_user(uuid: Uuid) -> Result<(), String> {
#[specta::specta]
#[tauri::command]
pub async fn get_default_user() -> Result<Option<MinecraftCredentials>, String> {
pub async fn get_default_user(fallback: Option<bool>) -> Result<Option<MinecraftCredentials>, String> {
let uuid = minecraft::get_default_user().await?;
if let Some(fallback) = fallback {
if fallback && uuid.is_none() {
return Ok(minecraft::users().await?.first().cloned());
}
}
match uuid {
Some(uuid) => Ok(Some(minecraft::get_user(uuid).await?)),
None => Ok(None),
@ -34,7 +40,7 @@ pub async fn get_default_user() -> Result<Option<MinecraftCredentials>, String>
#[specta::specta]
#[tauri::command]
pub async fn set_default_user(uuid: Uuid) -> Result<(), String> {
pub async fn set_default_user(uuid: Option<Uuid>) -> Result<(), String> {
minecraft::set_default_user(uuid).await?;
Ok(())
}

View file

@ -82,7 +82,7 @@ function Navbar() {
onClick={() => setProfileMenuOpen(!profileMenuOpen())}
class="hover:opacity-70"
>
<PlayerHead class="w-[32px] h-[32px] rounded-md" uuid={controller.defaultAccount()?.id} />
<PlayerHead class="min-w-8 min-h-8 w-8 h-8 max-w-8 max-h-8 rounded-md" uuid={controller.defaultAccount()?.id} />
</button>
</div>

View file

@ -1,4 +1,4 @@
import { Show, createSignal, splitProps } from 'solid-js';
import { Show, createEffect, createSignal, on, splitProps } from 'solid-js';
import type { JSX } from 'solid-js';
import steveSrc from '../../../assets/images/steve.png';
@ -13,26 +13,31 @@ function headSrc(uuid: string) {
function PlayerHead(props: PlayerHeadProps) {
const [split, rest] = splitProps(props, ['uuid', 'class']);
const [isLoading, setLoading] = createSignal(true);
const [isLoaded, setLoaded] = createSignal(false);
createEffect(on(() => props.uuid, () => setLoaded(false)));
return (
<>
<Show when={isLoading() === true}>
<img
src={steveSrc}
class={`image-render-pixel ${split.class}`}
{...rest}
/>
</Show>
<Show when={props.uuid !== null && props.uuid !== undefined}>
<img
src={headSrc(split.uuid!)}
onLoad={() => setLoading(false)}
onError={() => props.onError && props.onError()}
class={`image-render-pixel ${isLoading() ? 'hidden' : ''} ${split.class}`}
{...rest}
/>
</Show>
<Show
when={props.uuid !== null && props.uuid !== undefined}
fallback={(
<img
src={steveSrc}
class={`image-render-pixel ${split.class}`}
{...rest}
/>
)}
children={(
<img
src={headSrc(split.uuid!)}
onLoad={() => setLoaded(true)}
onError={() => props.onError && props.onError()}
class={`image-render-pixel ${isLoaded() ? '' : 'hidden'} ${split.class}`}
{...rest}
/>
)}
/>
</>
);
}

View file

@ -1,5 +1,5 @@
import { For, Show } from 'solid-js';
import { LogOut01Icon, PlusIcon, Settings01Icon } from '@untitled-theme/icons-solid';
import { PlusIcon, Settings01Icon } from '@untitled-theme/icons-solid';
import { useNavigate } from '@solidjs/router';
import Button from '../../base/Button';
import PlayerHead from '../../game/PlayerHead';
@ -13,9 +13,22 @@ interface AccountComponentProps {
}
function AccountComponent(props: AccountComponentProps) {
const controller = useAccountController();
function login() {
if (props.loggedIn === true)
return;
if (props.account)
controller.setDefaultAccount(props.account.id);
}
return (
<Show when={props.account}>
<div class={`flex flex-row justify-between p-2 rounded-lg ${!props.loggedIn && 'hover:bg-gray-05 active:bg-gray-10 hover:text-fg-primary-hover'}`}>
<div
onClick={login}
class={`flex flex-row justify-between p-2 rounded-lg ${props.loggedIn !== true && 'hover:bg-gray-05 active:bg-gray-10 hover:text-fg-primary-hover'}`}
>
<div class="flex flex-row justify-start flex-1 gap-x-3">
<PlayerHead class="w-8 h-8 rounded-md" uuid={props.account!.id} />
<div class="flex flex-col items-center justify-center">
@ -25,11 +38,14 @@ function AccountComponent(props: AccountComponentProps) {
</div>
</div>
</div>
{props.loggedIn && (
<Button buttonStyle="icon">
<LogOut01Icon class=" stroke-danger" />
</Button>
)}
{/* <Show when={props.loggedIn}>
<Button
buttonStyle="icon"
onClick={() => controller.setDefaultAccount(null)}
children={<LogOut01Icon class=" stroke-danger" />}
/>
</Show> */}
</div>
</Show>
);
@ -46,10 +62,14 @@ function AccountPopup(props: Popup.PopupProps) {
<Popup {...props}>
<div class="bg-secondary rounded-xl border border-gray-10 w-72 p-2 shadow-md shadow-black/30">
<div class="flex flex-col gap-y-2 text-fg-primary">
<AccountComponent account={controller.defaultAccount()} loggedIn />
<Show when={controller.defaultAccount() !== null || controller.defaultAccount() !== undefined}>
<AccountComponent
account={controller.defaultAccount()}
loggedIn
/>
</Show>
<Show when={filteredAccounts().length !== 0}>
<div class="w-full h-px bg-gray-05 rounded-md" />
<For each={filteredAccounts()}>
{account => (
<AccountComponent account={account} />

View file

@ -11,7 +11,7 @@ interface AccountControllerContextFunc {
refetch: () => void;
accounts: Resource<MinecraftCredentials[]>;
defaultAccount: Resource<MinecraftCredentials | null>;
setDefaultAccount: (uuid: string) => Promise<void>;
setDefaultAccount: (uuid: string | null) => Promise<void>;
removeAccount: (uuid: string, force?: boolean) => Promise<void>;
}
@ -19,7 +19,7 @@ const AccountControllerContext = createContext<AccountControllerContextFunc>() a
export function AccountControllerProvider(props: ParentProps) {
const [accounts, { refetch: refetchAccounts }] = useCommand(bridge.commands.getUsers);
const [defaultAccount, { refetch: refetchDefaultAccount }] = useCommand(bridge.commands.getDefaultUser);
const [defaultAccount, { refetch: refetchDefaultAccount }] = useCommand(bridge.commands.getDefaultUser, true);
const [deleteModalUuid, setDeleteModalUuid] = createSignal<string>();
@ -36,8 +36,8 @@ export function AccountControllerProvider(props: ParentProps) {
refetchDefaultAccount();
}
async function setDefaultAccount(uuid: string) {
await tryResult(bridge.commands.setDefaultUser, uuid);
async function setDefaultAccount(uuid: string | null) {
await tryResult(bridge.commands.setDefaultUser, uuid).then(refetch);
}
async function removeAccount(uuid: string, force?: boolean) {

View file

@ -31,12 +31,16 @@ pub async fn get_default_user() -> crate::Result<Option<uuid::Uuid>> {
/// Set the current default user by [`uuid::Uuid`].
#[tracing::instrument]
pub async fn set_default_user(user: uuid::Uuid) -> crate::Result<()> {
let user = get_user(user).await?;
pub async fn set_default_user(user: Option<uuid::Uuid>) -> crate::Result<()> {
let user = match user {
Some(user) => Some(get_user(user).await?.id),
None => None,
};
let state = State::get().await?;
let mut users = state.users.write().await;
users.default_user = Some(user.id);
users.default_user = user;
users.save().await?;
Ok(())
@ -50,6 +54,10 @@ pub async fn remove_user(user: uuid::Uuid) -> crate::Result<()> {
users.remove(user).await?;
if users.default_user == Some(user) {
set_default_user(Some(user)).await?;
}
Ok(())
}