Skip to content

Commit

Permalink
fixes and code enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
lelemm committed Oct 9, 2024
1 parent 4453145 commit 3ec6cc1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
6 changes: 5 additions & 1 deletion packages/desktop-client/src/components/LoggedInUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ export function LoggedInUser({
useEffect(() => {
if (cloudFileId && currentFile) {
setIsOwner(
currentFile.usersWithAccess.some(u => u.userId === userData?.userId),
currentFile.usersWithAccess.some(
u => u.userId === userData?.userId && u.owner,
),
);
} else {
setIsOwner(false);
}
}, [cloudFileId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const UserDirectoryRow = memo(
onMouseEnter={() => onHover && onHover(user.id)}
onMouseLeave={() => onHover && onHover(null)}
>
{!user.master && (
{!user.owner && (
<SelectCell
exposed={hovered || selected}
focused={true}
Expand All @@ -55,7 +55,7 @@ export const UserDirectoryRow = memo(
selected={selected}
/>
)}
{user.master && (
{user.owner && (
<Cell
width={20}
style={{ alignItems: 'center', userSelect: 'none' }}
Expand Down Expand Up @@ -118,7 +118,7 @@ export const UserDirectoryRow = memo(
plain
style={{ padding: '0 15px', paddingLeft: 5 }}
>
<Checkbox checked={user.master} disabled={true} />
<Checkbox checked={user.owner} disabled={true} />
</Cell>

<Cell
Expand Down
12 changes: 3 additions & 9 deletions packages/desktop-client/src/components/manager/BudgetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function FileState({
return '';
}

const userFound = file.usersWithAccess?.find(f => f.userId === file.owner);
const userFound = file.usersWithAccess?.find(f => f.owner);
return userFound?.displayName ?? userFound?.userName ?? 'unknown';
}
}
Expand Down Expand Up @@ -304,7 +304,6 @@ function FileItem({
{multiuserEnabled && (
<UserAccessForFile
fileId={(file as RemoteFile).cloudFileId}
ownerId={file.owner}
currentUserId={currentUserId}
/>
)}
Expand Down Expand Up @@ -599,14 +598,9 @@ export function BudgetList({ showHeader = true, quickSwitchMode = false }) {
type UserAccessForFileProps = {
fileId: string;
currentUserId: string;
ownerId?: string;
};

function UserAccessForFile({
fileId,
currentUserId,
ownerId,
}: UserAccessForFileProps) {
function UserAccessForFile({ fileId, currentUserId }: UserAccessForFileProps) {
const allFiles = useSelector(state => state.budgets.allFiles || []);
const remoteFiles = allFiles.filter(
f => f.state === 'remote' || f.state === 'synced' || f.state === 'detached',
Expand All @@ -615,7 +609,7 @@ function UserAccessForFile({
const multiuserEnabled = useMultiuserEnabled();

let usersAccess = currentFile?.usersWithAccess ?? [];
usersAccess = usersAccess?.filter(user => user.userId !== ownerId) ?? [];
usersAccess = usersAccess?.filter(user => user.userName !== '') ?? [];

const sortedUsersAccess = [...usersAccess].sort((a, b) => {
const textA =
Expand Down
8 changes: 4 additions & 4 deletions packages/desktop-client/src/components/modals/EditUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ function EditUser({ defaultUser, onSave: originalOnSave }: EditUserProps) {
<Checkbox
id="enabled-field"
checked={enabled}
disabled={defaultUser.master}
disabled={defaultUser.owner}
style={{
color: defaultUser.master ? theme.pageTextSubdued : 'inherit',
color: defaultUser.owner ? theme.pageTextSubdued : 'inherit',
}}
onChange={() => setEnabled(!enabled)}
/>
Expand All @@ -180,7 +180,7 @@ function EditUser({ defaultUser, onSave: originalOnSave }: EditUserProps) {
</label>
</View>
</Stack>
{defaultUser.master && (
{defaultUser.owner && (
<label
style={{
...styles.verySmallText,
Expand Down Expand Up @@ -224,7 +224,7 @@ function EditUser({ defaultUser, onSave: originalOnSave }: EditUserProps) {
<FormField style={{ flex: 1 }}>
<FormLabel title="Role" htmlFor="name-field" />
<Select
disabled={defaultUser.master}
disabled={defaultUser.owner}
options={Object.entries(PossibleRoles)}
value={role}
onChange={newValue => setRole(newValue)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';

import { closeAndLoadBudget } from 'loot-core/client/actions';
import { type State } from 'loot-core/client/state-types';
import { send } from 'loot-core/platform/client/fetch';
import { getUserAccessErrors } from 'loot-core/shared/errors';
import { type Budget } from 'loot-core/types/budget';
import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file';
import { type UserEntity } from 'loot-core/types/models';

import { useActions } from '../../hooks/useActions';
Expand All @@ -30,12 +33,18 @@ export function TransferOwnership({
const [error, setSetError] = useState<string | null>(null);
const [availableUsers, setAvailableUsers] = useState<[string, string][]>([]);
const [cloudFileId] = useMetadataPref('cloudFileId');
const allFiles = useSelector(state => state.budgets.allFiles || []);
const remoteFiles = allFiles.filter(
f => f.state === 'remote' || f.state === 'synced' || f.state === 'detached',
) as (SyncedLocalFile | RemoteFile)[];
const currentFile = remoteFiles.find(f => f.cloudFileId === cloudFileId);
const dispatch = useDispatch();

useEffect(() => {
send('users-get').then((users: UserEntity[]) =>
setAvailableUsers(
users
.filter(user => userData?.userId !== user.id)
.filter(f => currentFile?.owner !== f.id)
.map(user => [
user.id,
user.displayName
Expand All @@ -44,7 +53,7 @@ export function TransferOwnership({
]),
),
);
}, [userData?.userId]);
}, [userData?.userId, currentFile?.owner]);

async function onSave() {
if (cloudFileId) {
Expand Down Expand Up @@ -138,7 +147,15 @@ export function TransferOwnership({
<Button style={{ marginRight: 10 }} onPress={actions.popModal}>
Cancel
</Button>
<Button isDisabled={availableUsers.length === 0} onPress={onSave}>
<Button
isDisabled={availableUsers.length === 0}
onPress={async () => {
await onSave();
close();

await dispatch(closeAndLoadBudget((currentFile as Budget).id));
}}
>
Transfer ownership
</Button>
</Stack>
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/server/cloud-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface UsersWithAccess {
userId: string;
userName: string;
displayName: string;
owner: boolean;
}
export interface RemoteFile {
deleted: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/types/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface NewUserEntity {

export interface UserEntity extends NewUserEntity {
id: string;
master: boolean;
owner: boolean;
}

export interface UserEntityDropdown {
Expand Down

0 comments on commit 3ec6cc1

Please sign in to comment.