Skip to content

Commit

Permalink
Fix the handling of VC models
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrho committed Sep 25, 2024
1 parent 3b5dece commit da0732d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 37 deletions.
2 changes: 2 additions & 0 deletions jccm/src/Frontend/Components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const Eye = bundleIcon(EyeRegular, EyeRegular);
const EyeOff = bundleIcon(EyeOffRegular, EyeOffRegular);
const Dismiss = bundleIcon(DismissFilled, DismissRegular);

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

export const Login = ({ isOpen, onClose }) => {
const {
setUser,
Expand Down
2 changes: 1 addition & 1 deletion jccm/src/Frontend/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const Main = () => {

useEffect(() => {
const generateEvents = async () => {
await delay(500);
await delay(1000);
await eventBus.emit('user-session-check');
await eventBus.emit('local-inventory-refresh');
await eventBus.emit('device-facts-refresh');
Expand Down
98 changes: 62 additions & 36 deletions jccm/src/Frontend/MainEventProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const MainEventProcessor = () => {
const { currentActiveThemeName, setCurrentActiveThemeName } = useStore();

const userRef = useRef(user);
const isUserLoggedInRef = useRef(isUserLoggedIn);
const inventoryRef = useRef(inventory);
const deviceFactsRef = useRef(deviceFacts);
const cloudInventoryRef = useRef(cloudInventory);
Expand All @@ -42,13 +43,29 @@ export const MainEventProcessor = () => {
importSettings();
}, []);

useEffect(() => {
isUserLoggedInRef.current = isUserLoggedIn;
}, [isUserLoggedIn]);

useEffect(() => {
userRef.current = user;
}, [user]);

useEffect(() => {
inventoryRef.current = inventory;
}, [inventory]);

useEffect(() => {
deviceFactsRef.current = deviceFacts;
}, [deviceFacts]);

useEffect(() => {
cloudInventoryRef.current = cloudInventory;
}, [cloudInventory]);

useEffect(() => {
currentActiveThemeNameRef.current = currentActiveThemeName;
}, [inventory, deviceFacts, cloudInventory, currentActiveThemeName]);
}, [currentActiveThemeName]);

useEffect(() => {
const handleLocalInventoryRefresh = async ({
Expand Down Expand Up @@ -78,13 +95,52 @@ export const MainEventProcessor = () => {
}
};

const handleUserSessionCheck = async ({ message = '' } = {}) => {
console.log(
`Event: "user-session-check" ${
message.length > 0 ? `-> "${message}"` : ''
}`
);
try {
const data = await electronAPI.saWhoamiUser();

if (data.sessionValid) {
if (!_.isEqual(userRef.current, data.user)) {
setUser(data.user);
setIsUserLoggedIn(true);
}
if (
currentActiveThemeNameRef.current !== data?.user?.theme
) {
setCurrentActiveThemeName(data.user.theme);
}

setTimeout(async () => {
await handleCloudInventoryRefresh();
}, 3000);
} else {
setUser(null);
setCloudInventory([]);
setIsUserLoggedIn(false);
setCurrentActiveThemeName(data.theme);
}
} catch (error) {
setUser(null);
setCloudInventory([]);
setIsUserLoggedIn(false);
console.error('Session check error:', error);
}
};

const handleCloudInventoryRefresh = async ({
targetOrgs = null,
notification = false,
} = {}) => {
if (!isUserLoggedIn) return;

// console.log('Event: "cloud-inventory-refresh"');
console.log('Event: "cloud-inventory-refresh"');
if (!isUserLoggedInRef.current) return;
console.log(
'Event: "cloud-inventory-refresh" -> User is logged in'
);

// Initialize a timeout for setting the loading state
const loadingTimeout = setTimeout(() => {
Expand Down Expand Up @@ -163,36 +219,6 @@ export const MainEventProcessor = () => {
}
};

const handleUserSessionCheck = async ({ message = '' } = {}) => {
// console.log(`Event: "user-session-check" ${message.length > 0 ? `-> "${message}"` : ''}`);
try {
const data = await electronAPI.saWhoamiUser();
if (data.sessionValid) {
if (!_.isEqual(userRef.current, data.user)) {
setUser(data.user);
setIsUserLoggedIn(true);
}
if (
currentActiveThemeNameRef.current !== data?.user?.theme
) {
setCurrentActiveThemeName(data.user.theme);
}

await handleCloudInventoryRefresh();
} else {
setUser(null);
setCloudInventory([]);
setIsUserLoggedIn(false);
setCurrentActiveThemeName(data.theme);
}
} catch (error) {
setUser(null);
setCloudInventory([]);
setIsUserLoggedIn(false);
console.error('Session check error:', error);
}
};

const handleDeviceFactsRefresh = async () => {
// console.log('Event: "device-facts-refresh"');
const data = await electronAPI.saLoadDeviceFacts();
Expand All @@ -209,11 +235,11 @@ export const MainEventProcessor = () => {
cleanUpDeviceFacts();
};

eventBus.on('user-session-check', handleUserSessionCheck);
eventBus.on('local-inventory-refresh', handleLocalInventoryRefresh);
eventBus.on('cloud-inventory-refresh', handleCloudInventoryRefresh);
eventBus.on('cloud-inventory-reset', handleCloudInventoryReset);
eventBus.on('reset-device-facts', handleResetDeviceFacts);
eventBus.on('user-session-check', handleUserSessionCheck);
eventBus.on('device-facts-refresh', handleDeviceFactsRefresh);
eventBus.on('device-facts-cleanup', handleDeviceFactsCleanup);

Expand All @@ -226,9 +252,9 @@ export const MainEventProcessor = () => {
'cloud-inventory-refresh',
handleCloudInventoryRefresh
);
eventBus.off('user-session-check', handleUserSessionCheck);
eventBus.off('cloud-inventory-reset', handleCloudInventoryReset);
eventBus.off('reset-device-facts', handleResetDeviceFacts);
eventBus.off('user-session-check', handleUserSessionCheck);
eventBus.off('device-facts-refresh', handleDeviceFactsRefresh);
eventBus.off('device-facts-cleanup', handleDeviceFactsCleanup);
};
Expand Down

0 comments on commit da0732d

Please sign in to comment.