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

feat: add per organization sync status #1186

Merged
merged 1 commit into from
Apr 2, 2024
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
10 changes: 9 additions & 1 deletion src/renderer/api/cadt/v1/organizations/organizations.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ const organizationsApi = cadtApi.injectEndpoints({
transformResponse(baseQueryReturnValue: BaseQueryResult<any>): any[]{
return Object.values(baseQueryReturnValue);
}
}),
getOrganizationsMap: builder.query({
query: () => ({
url: `${host}/v1/organizations`,
method: 'GET',
}),
providesTags: [organizationsTag],
})
})
});


export const {
useGetOrganizationsListQuery
useGetOrganizationsListQuery,
useGetOrganizationsMapQuery
} = organizationsApi;
22 changes: 10 additions & 12 deletions src/renderer/components/blocks/widgets/OrganizationSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect } from 'react';
import { useGetOrganizationsListQuery } from "@/api/cadt/v1/organizations";
import { Dropdown } from '@/components';
import { useGetOrganizationsListQuery } from '@/api/cadt/v1/organizations';
import { Dropdown, SyncIndicator } from '@/components';

interface OrganizationSelectorProps {
onSelect: (organization: any | undefined) => void;
Expand All @@ -13,9 +13,9 @@ const OrganizationSelector: React.FC<OrganizationSelectorProps> = ({ onSelect, d

useEffect(() => {
if (defaultOrgUid === null) {
setSelectedOrganization({ name: "All Organizations" });
setSelectedOrganization({ name: 'All Organizations' });
} else if (defaultOrgUid !== undefined && organizations) {
const defaultOrganization = organizations.find(org => org.orgUid === defaultOrgUid);
const defaultOrganization = organizations.find((org) => org.orgUid === defaultOrgUid);
if (defaultOrganization) {
setSelectedOrganization(defaultOrganization);
}
Expand All @@ -31,27 +31,25 @@ const OrganizationSelector: React.FC<OrganizationSelectorProps> = ({ onSelect, d
}

const handleSelect = (organization: any | undefined) => {
if (!organization) {
setSelectedOrganization({ name: "All Organizations" });
} else {
setSelectedOrganization(organization);
}
setSelectedOrganization(organization || { name: 'All Organizations' });
onSelect(organization);
};

return (
<Dropdown label={selectedOrganization ? selectedOrganization.name : "Select Organization"} inline={true}>
<Dropdown label={selectedOrganization ? selectedOrganization.name : 'All Organizations'} inline={true}>
<Dropdown.Item onClick={() => handleSelect(undefined)}>All Organizations</Dropdown.Item>
{organizations.map((organization) => (
<Dropdown.Item
key={organization.orgUid}
onClick={() => handleSelect(organization)}
className="flex justify-between items-center"
>
{organization.name}
<span className="mr-2.5">{organization.name}</span>
<SyncIndicator orgUid={organization.orgUid} detailed={false} />
</Dropdown.Item>
))}
</Dropdown>
);
}
};

export { OrganizationSelector };
44 changes: 44 additions & 0 deletions src/renderer/components/blocks/widgets/SyncIndicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { useGetOrganizationsMapQuery, useGetOrganizationsListQuery } from '@/api/cadt/v1/organizations';

interface SyncIndicatorProps {
detailed: boolean;
orgUid?: string;
}

/**
* Component to display sync status indicator.
* It shows a green circle if the organization is synced, and a pulsing yellow circle if syncing.
* When 'detailed' is true, it also shows a label next to the indicator.
* The sync status is determined based on 'orgUid' from the organizations data.
*/
const SyncIndicator: React.FC<SyncIndicatorProps> = ({ detailed, orgUid }) => {
const { data: organizationsMap } = useGetOrganizationsMapQuery(null, {
skip: !orgUid,
});

const { data: organizationList } = useGetOrganizationsListQuery(null, {
skip: Boolean(orgUid),
});

let isSynced = false;

if (!orgUid) {
isSynced = !organizationList?.some((org) => !org.synced);
} else {
isSynced = organizationsMap?.[orgUid]?.synced;
}

return (
<div className={`flex items-center space-x-2 ${detailed ? 'p-2 rounded-lg shadow bg-white dark:bg-gray-800' : ''}`}>
<div className={`${isSynced ? 'bg-green-500' : 'animate-pulse bg-yellow-500'} h-4 w-4 rounded-full`}></div>
{detailed && (
<span className={`text-sm ${isSynced ? 'text-green-700' : 'text-yellow-600'}`}>
{isSynced ? 'Synced' : 'Syncing...'}
</span>
)}
</div>
);
};

export { SyncIndicator };
3 changes: 2 additions & 1 deletion src/renderer/components/blocks/widgets/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './SearchBox';
export * from './OrganizationSelector';
export * from './OrganizationSelector';
export * from './SyncIndicator';
2 changes: 2 additions & 0 deletions src/renderer/pages/ProjectsList/ProjectsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SkeletonTable,
ProjectsListTable,
SearchBox,
SyncIndicator
} from '@/components';
import {FormattedMessage} from "react-intl";

Expand Down Expand Up @@ -62,6 +63,7 @@ const ProjectsList: React.FC = () => {
<div className="flex flex-col md:flex-row gap-6 pl-1 my-2.5 relative z-30">
<SearchBox defaultValue={search} onChange={handleSearchChange} />
<OrganizationSelector onSelect={handleOrganizationSelected} defaultOrgUid={orgUid} />
<SyncIndicator detailed={true} orgUid={orgUid} />
</div>

{projectsLoading ? (
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/pages/UnitsList/UnitsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SkeletonTable,
SearchBox,
UnitsListTable,
SyncIndicator
} from '@/components';
import {FormattedMessage} from "react-intl";

Expand Down Expand Up @@ -62,6 +63,7 @@ const UnitsList: React.FC = () => {
<div className="flex flex-col md:flex-row gap-6 pl-1 my-2.5 relative z-30">
<SearchBox defaultValue={search} onChange={handleSearchChange} />
<OrganizationSelector onSelect={handleOrganizationSelected} defaultOrgUid={orgUid} />
<SyncIndicator detailed={true} orgUid={orgUid} />
</div>

{unitsLoading ? (
Expand Down