Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛠 #2115 Fix workspace user search #2205

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const searchBackend = async (
if (result && scope === 'workspace') {
final = result.map(wsUser => ({
...wsUser.user,
workspaces: [{ id: workspaceId, company_id: companyId }],
workspaces: [{ id: workspaceId, company_id: companyId, role: wsUser.role }],
}));
}

Expand Down
4 changes: 4 additions & 0 deletions twake/frontend/src/app/features/users/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export type UserType = {
groups_id?: string[];
id?: string;
identity_provider?: string;
provider_id?: string;
provider?: string;
last_activity?: number;
isNew?: boolean;
isRobot?: boolean;
language?: string;
Expand All @@ -49,6 +52,7 @@ export type UserType = {
username: string;
companies?: UserCompanyType[];
preferences: UserPreferencesType;
preference?: UserPreferencesType;

/**
* this field is filled when available and so we cannot rely on it except on search service for filtering
Expand Down
37 changes: 3 additions & 34 deletions twake/frontend/src/app/features/workspaces/types/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserType } from 'app/features/users/types/user';
import { UserCompanyType, UserType } from 'app/features/users/types/user';

export type WorkspaceType = {
id: string;
Expand All @@ -15,42 +15,11 @@ export type WorkspaceType = {
};
};

export type WorkspaceUserCompanyType = {
role: 'owner' | 'admin' | 'member' | 'guest';
status: 'active' | 'deactivated' | 'invited';
company: {
id: string; //Related to console "code"
name: string;
logo: string;
};
};

export type WorkspaceUserRole = 'moderator' | 'member';
export type WorkspaceUserType = {
id: string;
provider: string; //"console",
provider_id: string;

email: string;
is_verified: boolean;
picture: string;
first_name: string;
last_name: string;
created_at: number;
deleted: boolean;

status: string; //Single string for the status
last_activity: number;

export type WorkspaceUserType = UserType & {
role: WorkspaceUserRole;
user_id: string;
workspace_id: string;
user: UserType;

//Below is only if this is myself
preference: {
locale: string; //"fr-FR",
timezone: number; //minutesFromGMT,
};
companies: WorkspaceUserCompanyType[];
companies?: UserCompanyType[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export default () => {
<Input
placeholder={Languages.t('components.listmanager.filter')}
prefix={<Search size={16} color="var(--grey-dark)" />}
value={searchValue}
onChange={e => setSearchValue(e.target.value)}
/>
</Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import Icon from 'app/components/icon/icon';
import UserAPIClient from 'app/features/users/api/user-api-client';
import { WorkspaceUserType } from 'app/features/workspaces/types/workspace';
import { useSetUserList } from 'app/features/users/hooks/use-user-list';
import {
searchBackend,
searchFrontend,
useSearchUserList,
} from 'app/features/users/hooks/use-search-user-list';

type ColumnObjectType = { [key: string]: any };

Expand All @@ -47,10 +52,11 @@ export default ({ filter }: { filter: string }) => {
const [data, setData] = useState<ColumnObjectType[]>([]);
const [pageToken, setPageToken] = useState<string | null>(null);
const [serverSearchedData, setServerSearchedData] = useState<ColumnObjectType[]>([]);
const [filteredData, setFilteredData] = useState<ColumnObjectType[] | null>(null);
const companyId = useRouterCompany();
const workspaceId = useRouterWorkspace();

const { search, result } = useSearchUserList({ scope: 'workspace' });

const prefixRoute = '/internal/services/workspaces/v1';
const workspaceUsersRoute = `${prefixRoute}/companies/${companyId}/workspaces/${workspaceId}/users`;

Expand All @@ -64,58 +70,20 @@ export default ({ filter }: { filter: string }) => {
}, []);

useEffect(() => {
onSearch();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filter, data]);

const onSearch = async () => {
if (filter.length) {
delayRequest('workspace_members_search', async () => {
try {
setLoading(true);
await UserAPIClient.search<WorkspaceUserType>(
filter,
{ scope: 'workspace', companyId, workspaceId },
wsUsers => {
const resources: ColumnObjectType[] = wsUsers;

// Make sure we have unicity in this combined list
setServerSearchedData(
_.uniqBy([...serverSearchedData, ...(resources || [])], col => col.user.id),
);
updateFilteredData();
wsUsers &&
setUserList(
wsUsers.map(wsUser => ({
...wsUser.user,
workspaces: [{ id: workspaceId, company_id: companyId }],
})),
);
},
);

setLoading(false);
} catch (e) {
console.error(e);
}
});
}
updateFilteredData();
};

const updateFilteredData = () => {
if (filter.length) {
// Make sure we have unicity in this combined list
const filtered = _.uniqBy([...data, ...serverSearchedData], col => col.user.id).filter(col =>
`${col.user.email} ${UserService.getFullName(col.user)}`
.toLocaleLowerCase()
.includes(filter.toLocaleLowerCase()),
);
setFilteredData(filtered);
} else {
setFilteredData(null);
}
};
delayRequest('workspace_partners_search', () => search(filter));
}, [filter]);

const filteredData: WorkspaceUserType[] | null = filter
? (result
.map(u => ({
user: u,
...u,
role: u.workspaces?.find(w => w.id === workspaceId)?.role,
user_id: u.id || '',
workspace_id: workspaceId || '',
}))
.slice(0, 50) as WorkspaceUserType[])
: null;

const requestWorkspaceUsers = async (pageToken?: string) => {
try {
Expand All @@ -142,7 +110,6 @@ export default ({ filter }: { filter: string }) => {

const updateData = (updater: (data: ColumnObjectType[]) => ColumnObjectType[]) => {
setData(updater(data));
setFilteredData(filteredData !== null ? updater(filteredData || []) : null);
setServerSearchedData(updater(serverSearchedData));
};

Expand Down Expand Up @@ -313,7 +280,7 @@ export default ({ filter }: { filter: string }) => {
dataIndex: 'menu',
width: 50,
render: (text, col, index) =>
workspaceUserRightsService.hasWorkspacePrivilege() && buildMenu(col),
false && workspaceUserRightsService.hasWorkspacePrivilege() && buildMenu(col),
},
];

Expand Down