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

chore: ensure app Id is maintained #4541

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
1 change: 1 addition & 0 deletions core/src/types/config/appConfigEntity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type AppConfiguration = {
data_folder: string
quick_ask: boolean
distinct_id?: string
}
14 changes: 12 additions & 2 deletions web/containers/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import ImportModelOptionModal from '@/screens/Settings/ImportModelOptionModal'
import ImportingModelModal from '@/screens/Settings/ImportingModelModal'
import SelectingModelModal from '@/screens/Settings/SelectingModelModal'

import { getAppDistinctId, updateDistinctId } from '@/utils/settings'

import LoadingModal from '../LoadingModal'

import MainViewContainer from '../MainViewContainer'
Expand Down Expand Up @@ -93,8 +95,16 @@ const BaseLayout = () => {
return properties
},
})
posthog.opt_in_capturing()
posthog.register({ app_version: VERSION })
// Attempt to restore distinct Id from app global settings
getAppDistinctId()
.then((id) => {
if (id) posthog.identify(id)
})
.finally(() => {
posthog.opt_in_capturing()
posthog.register({ app_version: VERSION })
updateDistinctId(posthog.get_distinct_id())
})
} else {
posthog.opt_out_capturing()
}
Expand Down
1 change: 1 addition & 0 deletions web/hooks/useFactoryReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
await window.core?.api?.getAppConfigurations()

if (!appConfiguration) {
console.debug('Failed to get app configuration')

Check warning on line 33 in web/hooks/useFactoryReset.ts

View workflow job for this annotation

GitHub Actions / coverage-check

33 line is not covered with tests
}

const janDataFolderPath = appConfiguration!.data_folder
Expand All @@ -42,7 +42,7 @@
EngineManager.instance()
.engines.values()
.map(async (engine) => {
await engine.onUnload()

Check warning on line 45 in web/hooks/useFactoryReset.ts

View workflow job for this annotation

GitHub Actions / coverage-check

45 line is not covered with tests
})
)

Expand All @@ -58,6 +58,7 @@
const configuration: AppConfiguration = {
data_folder: defaultJanDataFolder,
quick_ask: appConfiguration?.quick_ask ?? false,
distinct_id: appConfiguration?.distinct_id,
}
await window.core?.api?.updateAppConfiguration(configuration)
}
Expand Down
22 changes: 22 additions & 0 deletions web/utils/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AppConfiguration } from '@janhq/core'

/**
* Update app distinct Id
* @param id
*/
export const updateDistinctId = async (id: string) => {
const appConfiguration: AppConfiguration =
await window.core?.api?.getAppConfigurations()
appConfiguration.distinct_id = id
await window.core?.api?.updateAppConfiguration(appConfiguration)
}

/**
* Retrieve app distinct Id
* @param id
*/
export const getAppDistinctId = async (): Promise<string | undefined> => {
const appConfiguration: AppConfiguration =
await window.core?.api?.getAppConfigurations()
return appConfiguration.distinct_id
}
Loading