-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add window persisting data (#1487)
Signed-off-by: Svetoslav Borislavov <[email protected]>
- Loading branch information
1 parent
53c174b
commit 22cc67c
Showing
7 changed files
with
245 additions
and
16 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
front-end/src/main/modules/ipcHandlers/localUser/safeStorage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { Rectangle } from 'electron'; | ||
|
||
import { BrowserWindow } from 'electron'; | ||
|
||
import { STATIC_USER, WINDOW_STATE } from '@main/shared/constants'; | ||
|
||
import { getPrismaClient } from '@main/db/prisma'; | ||
|
||
/* Sets the window state*/ | ||
export const setWindowBounds = async (window: BrowserWindow): Promise<void> => { | ||
try { | ||
const prisma = getPrismaClient(); | ||
|
||
const claim_value = JSON.stringify(window.getBounds()); | ||
|
||
const alreadyAdded = await prisma.claim.findFirst({ | ||
where: { | ||
claim_key: WINDOW_STATE, | ||
user: { | ||
email: STATIC_USER, | ||
}, | ||
}, | ||
}); | ||
|
||
if (alreadyAdded) { | ||
await prisma.claim.update({ | ||
where: { id: alreadyAdded?.id }, | ||
data: { claim_value: JSON.stringify(window.getBounds()) }, | ||
}); | ||
return; | ||
} | ||
|
||
await prisma.claim.create({ | ||
data: { | ||
claim_key: WINDOW_STATE, | ||
claim_value, | ||
user: { | ||
connect: { | ||
email: STATIC_USER, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
/* Get window bounds */ | ||
export const getWindowBounds = async (): Promise<Rectangle | null> => { | ||
try { | ||
const prisma = getPrismaClient(); | ||
const bounds = await prisma.claim.findFirst({ | ||
where: { | ||
claim_key: WINDOW_STATE, | ||
user: { | ||
email: STATIC_USER, | ||
}, | ||
}, | ||
}); | ||
return bounds ? JSON.parse(bounds.claim_value) : null; | ||
} catch (error) { | ||
console.log(error); | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { mockDeep } from 'vitest-mock-extended'; | ||
import prisma from '@main/db/__mocks__/prisma'; | ||
|
||
import { STATIC_USER, WINDOW_STATE } from '@main/shared/constants'; | ||
|
||
import { Claim } from '@prisma/client'; | ||
import { BrowserWindow } from 'electron'; | ||
import { setWindowBounds, getWindowBounds } from '@main/services/windowState'; | ||
|
||
vi.mock('@main/db/prisma'); | ||
|
||
describe('Window state Service', () => { | ||
const mockWindow = mockDeep<BrowserWindow>(); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
describe('setWindowBounds', () => { | ||
it('should update the window bounds if the claim already exists', async () => { | ||
const bounds = { x: 0, y: 0, width: 800, height: 600 }; | ||
mockWindow.getBounds.mockReturnValue(bounds); | ||
|
||
prisma.claim.findFirst.mockResolvedValueOnce({ | ||
id: '1', | ||
claim_value: JSON.stringify(bounds), | ||
} as Claim); | ||
|
||
await setWindowBounds(mockWindow); | ||
|
||
expect(prisma.claim.update).toHaveBeenCalledWith({ | ||
where: { id: '1' }, | ||
data: { claim_value: JSON.stringify(bounds) }, | ||
}); | ||
}); | ||
|
||
it('should create a new claim if it does not exist', async () => { | ||
const bounds = { x: 0, y: 0, width: 800, height: 600 }; | ||
mockWindow.getBounds.mockReturnValue(bounds); | ||
|
||
prisma.claim.findFirst.mockResolvedValueOnce(null); | ||
|
||
await setWindowBounds(mockWindow); | ||
|
||
expect(prisma.claim.create).toHaveBeenCalledWith({ | ||
data: { | ||
claim_key: WINDOW_STATE, | ||
claim_value: JSON.stringify(bounds), | ||
user: { | ||
connect: { | ||
email: STATIC_USER, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle errors gracefully', async () => { | ||
const bounds = { x: 0, y: 0, width: 800, height: 600 }; | ||
mockWindow.getBounds.mockReturnValue(bounds); | ||
|
||
prisma.claim.findFirst.mockRejectedValueOnce(new Error('Database error')); | ||
|
||
await expect(setWindowBounds(mockWindow)).resolves.not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('getWindowBounds', () => { | ||
it('should retrieve the window bounds if the claim exists', async () => { | ||
const bounds = { x: 0, y: 0, width: 800, height: 600 }; | ||
prisma.claim.findFirst.mockResolvedValueOnce({ | ||
claim_value: JSON.stringify(bounds), | ||
} as Claim); | ||
|
||
const result = await getWindowBounds(); | ||
|
||
expect(result).toEqual(bounds); | ||
}); | ||
|
||
it('should return null if the claim does not exist', async () => { | ||
prisma.claim.findFirst.mockResolvedValueOnce(null); | ||
|
||
const result = await getWindowBounds(); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it('should handle errors gracefully', async () => { | ||
prisma.claim.findFirst.mockRejectedValueOnce(new Error('Database error')); | ||
|
||
const result = await getWindowBounds(); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters