Skip to content

Commit

Permalink
feat: save window settings popin d'accueil #27
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Aug 12, 2024
1 parent 3b4a671 commit c5dbd73
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 168 deletions.
1 change: 1 addition & 0 deletions electron-app/ecoindex-app/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"defaultValue": "to translate",
"keySeparator": null,
"nsSeparator": null,
"i18nextInstanceNames": ["_i18n", "i18next", "i18n"],
"keyAsDefaultValue": ["en"],
"useI18nextDefaultValue": false,
"keyAsDefaultValueForDerivedKeys": false,
Expand Down
7 changes: 7 additions & 0 deletions electron-app/ecoindex-app/src/interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export interface IVersionsAPI {
getNodeVersion: () => Promise<string>
}

export interface IStoreAPI {
set: (key: string, value: any) => Promise<void>
get: (key: string) => Promise<string>
delete: (key: string) => Promise<void>
}
export interface IElectronAPI {
// i18nextElectronBackend: any
// Main → Front
Expand All @@ -31,6 +36,7 @@ export interface IElectronAPI {
) => Promise<string>
handleJsonReadAndReload: () => Promise<IJsonMesureData>
handleIsJsonConfigFileExist: (workDir: string) => Promise<boolean>
hideHelloWindow: () => Promise<void>
}

declare global {
Expand Down Expand Up @@ -59,5 +65,6 @@ declare global {
interface Window {
versions: IVersionsAPI
electronAPI: IElectronAPI
store: IStoreAPI
}
}
3 changes: 3 additions & 0 deletions electron-app/ecoindex-app/src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"Delete this course": "Delete this course",
"Description": "Description",
"Desktop": "application",
"Do not show this window again.": "Do not show this window again.",
"Don't worry, you can always open it from the Help menu.": "Don't worry, you can always open it from the Help menu.",
"Enter a key": "Enter a key",
"Enter a value": "Enter a value",
"Enter an url": "Enter an url",
Expand Down Expand Up @@ -60,6 +62,7 @@
"Measures": "Measures",
"Minimize": "Minimize",
"Node is not installed, install it (you must be admin of your computer)! After installation, restart application.": "Node is not installed, install it (you must be admin of your computer)! After installation, restart application.",
"Open splash window...": "Open welcome window...",
"Quit": "Quit",
"Reload": "Reload",
"Reload if too long": "...Reload",
Expand Down
3 changes: 3 additions & 0 deletions electron-app/ecoindex-app/src/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"Delete this course": "Supprimer ce parcours",
"Description": "Description",
"Desktop": "application",
"Do not show this window again.": "Do not show this window again.",
"Don't worry, you can always open it from the Help menu.": "Ne vous inquiétez pas, vous pouvez toujours l'ouvrir à partir du menu Aide.",
"Enter a key": "Entrer une clé",
"Enter a value": "Entrer une valeur",
"Enter an url": "Entrer une URL",
Expand Down Expand Up @@ -60,6 +62,7 @@
"Measures": "Mesures",
"Minimize": "Réduire",
"Node is not installed, install it (you must be admin of your computer)! After installation, restart application.": "Node n'est pas installé, installez-le (vous devez être administrateur de votre ordinateur) ! Après l'installation, redémarrez l'application.",
"Open splash window...": "Ouvrir la fenêtre d'accueil...",
"Quit": "Quitter",
"Reload": "Recharger",
"Reload if too long": "...Recharger",
Expand Down
57 changes: 36 additions & 21 deletions electron-app/ecoindex-app/src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as menuFactoryService from '../services/menuFactory'

import { BrowserWindow, app, ipcMain } from 'electron'
import { channels, scripts } from '../shared/constants'
import {
getMainWindow,
getWelcomeWindow,
hasShowWelcomeWindow,
isDev,
setHasShowedWelcomeWindow,
setMainWindow,
setWelcomeWindow,
} from '../shared/memory'
Expand All @@ -16,7 +13,10 @@ import {
handleSimpleCollect,
} from './handlers/HandleCollectAll'

import Store from 'electron-store'
import Updater from './Updater'
import { channels } from '../shared/constants'
import { convertVersion } from './utils'
import fixPath from 'fix-path'
import { handleGetNodeVersion } from './handlers/HandleGetNodeVersion'
import { handleHomeDir } from './handlers/HandleHomeDir'
Expand All @@ -40,6 +40,9 @@ if (require('electron-squirrel-startup')) {
log.initialize()
const mainLog = log.scope('main')

const store = new Store()
mainLog.debug(`userData`, app.getPath('userData'))

export const getMainLog = () => {
return log
}
Expand Down Expand Up @@ -168,6 +171,21 @@ app.on('ready', () => {
// }
})

ipcMain.handle('store-set', (event, key: string, value: any) => {
store.set(key, value)
})

ipcMain.handle('store-get', (event, key: string) => {
return store.get(key)
})

ipcMain.handle('store-delete', (event, key: string) => {
store.delete(key)
})
ipcMain.handle(channels.SHOW_HIDE_WELCOME_WINDOW, () => {
getWelcomeWindow().hide()
})

app.setAboutPanelOptions({
applicationName: packageJson.productName,
applicationVersion: packageJson.name,
Expand Down Expand Up @@ -256,24 +274,21 @@ const _createMainWindow = (): void => {
mainLog.error(`i18n`, error)
}

if (!hasShowWelcomeWindow()) {
setWelcomeWindow(
new BrowserWindow({
width: 800,
height: 700,
icon: '/assets/app-ico.png',
webPreferences: {
preload: HELLO_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
parent: getMainWindow(),
modal: true,
show: true,
})
)
getWelcomeWindow().loadURL(HELLO_WINDOW_WEBPACK_ENTRY)
} else {
setHasShowedWelcomeWindow(true)
}
const displayHello = `displayHello.${convertVersion(packageJson.version)}`
setWelcomeWindow(
new BrowserWindow({
width: 800,
height: 700,
icon: '/assets/app-ico.png',
webPreferences: {
preload: HELLO_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
parent: getMainWindow(),
modal: true,
show: !store.get(displayHello) && store.get(displayHello) !== true,
})
)
getWelcomeWindow().loadURL(HELLO_WINDOW_WEBPACK_ENTRY)

// Open the DevTools.
// mainWindow.webContents.openDevTools({ mode: 'detach' })
Expand Down
55 changes: 8 additions & 47 deletions electron-app/ecoindex-app/src/main/utils.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
// /**
// * TODO: Method to clean returned log from sh files
// * @param stout any
// * @returns any
// */
// export const cleanLogString = (stout: any) => {
// if (typeof stout !== 'string') return stout
// // eslint-disable-next-line no-control-regex, no-useless-escape
// const gm = new RegExp(']2;(.*)]1; ?(\n?)', 'gm')
// if (stout.match(gm)) return stout.replace(gm, '')
// else return stout
// }

// /**
// * ISimpleUrlInput[] -> string[]
// * @param jsonDatas with urls ISimpleUrlInput[]
// */
// export const convertJSONDatasFromISimpleUrlInput = (
// jsonDatas: IJsonMesureData
// ): IJsonMesureData => {
// const output = jsonDatas
// jsonDatas.courses.forEach((course, index) => {
// const urls: string[] = course.urls.map((url: any) => url.value)
// jsonDatas.courses[index].urls = urls
// })
// return output
// }

// /**
// * string[] -> ISimpleUrlInput[]
// * @param jsonDatas with urls string[]
// */
// export const convertJSONDatasFromString = (
// jsonDatas: IJsonMesureData
// ): IJsonMesureData => {
// const output = jsonDatas
// jsonDatas.courses.forEach((course, index) => {
// const urls: ISimpleUrlInput[] = course.urls.map((url: any) => {
// return {
// value: url,
// }
// })
// jsonDatas.courses[index].urls = urls
// })
// // console.log(`_convertJSONDatasFromString`, output)
// return output
// }
/**
* Convert Version to string
* @param version string
* @returns string
*/
export const convertVersion = (version: string) => {
return version.replace(/\./gm, '_').replace(/-/gm, '_')
}
7 changes: 7 additions & 0 deletions electron-app/ecoindex-app/src/menus/darwinMenu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserWindow, app as ElectronApp, shell } from 'electron'

import { config } from '../configs/app.config'
import { getWelcomeWindow } from '../shared/memory'
import i18n from 'i18next'
import log from 'electron-log/main'
import pkg from '../../package.json'
Expand Down Expand Up @@ -103,6 +104,12 @@ export const darwinTemplate = (
)
},
},
{
label: `${_i18n.t('Open splash window...')}`,
click: async () => {
await getWelcomeWindow().show()
},
},
],
},
// {
Expand Down
7 changes: 7 additions & 0 deletions electron-app/ecoindex-app/src/menus/otherMenu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserWindow, app as ElectronApp, shell } from 'electron'

import { config } from '../configs/app.config'
import { getWelcomeWindow } from '../shared/memory'
import i18n from 'i18next'
import log from 'electron-log/main'
import pkg from '../../package.json'
Expand Down Expand Up @@ -79,6 +80,12 @@ export const otherTemplate = (
)
},
},
{
label: `${_i18n.t('Open splash window...')}`,
click: async () => {
await getWelcomeWindow().show()
},
},
],
},
]
Expand Down
73 changes: 60 additions & 13 deletions electron-app/ecoindex-app/src/renderer/HelloWindow/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ChangeEvent, useEffect, useState } from 'react'
import { Route, MemoryRouter as Router, Routes } from 'react-router-dom'
import { useEffect, useState } from 'react'

import { Button } from '../ui/button'
import { Checkbox } from '../ui/checkbox'
import { CheckedState } from '@radix-ui/react-checkbox'
import { DarkModeSwitcher } from '../components/dark-mode-switcher'
import { Header } from '../components/Header'
import { convertVersion } from '../../main/utils'
import i18nResources from '../../configs/i18nResources'
import log from 'electron-log/renderer'
import pkg from '../../../package.json'
Expand All @@ -13,8 +16,18 @@ const frontLog = log.scope('front/HelloApp')

function HelloApp() {
const [language, setLanguage] = useState('en')
const [checked, setChecked] = useState(false)
const closeHandler = () => {
window.close()
window.electronAPI.hideHelloWindow()
window.scrollTo(0, 0)
}

const handlerDoNotDisplayAgain = (event: CheckedState) => {
setChecked(event.valueOf() as boolean)
window.store.set(
`displayHello.${convertVersion(pkg.version)}`,
event.valueOf()
)
}

useEffect(() => {
Expand All @@ -34,6 +47,15 @@ function HelloApp() {
frontLog.error(error)
}
})

const updateCheckBox = async () => {
const displayHello = `displayHello.${convertVersion(pkg.version)}`
const value = (await window.store.get(
displayHello
)) as unknown as boolean
setChecked(value)
}
updateCheckBox()
}, [])

const { t } = useTranslation()
Expand Down Expand Up @@ -234,17 +256,42 @@ function HelloApp() {
</>
)}
</div>
<div className="grid place-items-center">
<Button
onClick={closeHandler}
id="close-window"
size="default"
variant="default"
className="mt-8 w-fit"
title={t('Close this window')}
>
{t('Close')}
</Button>
<div className="mt-8 flex w-full items-center justify-between">
<div className="flex w-1/3 items-start space-x-2 *:justify-start">
<Checkbox
id="do-not-display-again"
checked={checked}
onCheckedChange={(event) =>
handlerDoNotDisplayAgain(event)
}
/>
<div className="grid gap-1.5 leading-none">
<label
htmlFor="do-not-display-again"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{t(`Do not show this window again.`)}
</label>
<p className="text-sm text-muted-foreground">
{t(
`Don't worry, you can always open it from the Help menu.`
)}
</p>
</div>
</div>
<div className="flex w-1/3 !justify-center">
<Button
onClick={closeHandler}
id="close-window"
size="default"
variant="default"
className="w-fit"
title={t('Close this window')}
>
{t('Close')}
</Button>
</div>
<div className="w-1/3"></div>
</div>
<div className="prose prose-sm text-center font-semibold dark:prose-invert">
{t('Version of the application')}: {pkg.version}
Expand Down
8 changes: 8 additions & 0 deletions electron-app/ecoindex-app/src/renderer/HelloWindow/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ contextBridge.exposeInMainWorld('electronAPI', {
channels.CHANGE_LANGUAGE_TO_FRONT,
(_event, languageCode) => callback(languageCode)
),
hideHelloWindow: () =>
ipcRenderer.invoke(channels.SHOW_HIDE_WELCOME_WINDOW),
})
contextBridge.exposeInMainWorld('store', {
set: (key: string, value: any) =>
ipcRenderer.invoke('store-set', key, value),
get: (key: string) => ipcRenderer.invoke('store-get', key),
delete: (key: string) => ipcRenderer.invoke('store-delete', key),
})
Loading

0 comments on commit c5dbd73

Please sign in to comment.