diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 44eedef5bbb95..1a279b58c350b 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -71,6 +71,7 @@ import { getCurrentExporterData } from '~/exporter/exporterViewLogic' import { encodeParams } from 'kea-router' export const ACTIVITY_PAGE_SIZE = 20 +const PAGINATION_DEFAULT_MAX_PAGES = 10 export interface PaginatedResponse { results: T[] @@ -1863,6 +1864,23 @@ const api = { } return response }, + + async loadPaginatedResults( + url: string | null, + maxIterations: number = PAGINATION_DEFAULT_MAX_PAGES + ): Promise { + let results: any[] = [] + for (let i = 0; i <= maxIterations; ++i) { + if (!url) { + break + } + + const { results: partialResults, next } = await api.get(url) + results = results.concat(partialResults) + url = next + } + return results + }, } function reportError(method: string, url: string, response: Response, startTime: number): void { diff --git a/frontend/src/scenes/pipeline/transformationsLogic.tsx b/frontend/src/scenes/pipeline/transformationsLogic.tsx index 40c83cb3c6750..eadb6799ed898 100644 --- a/frontend/src/scenes/pipeline/transformationsLogic.tsx +++ b/frontend/src/scenes/pipeline/transformationsLogic.tsx @@ -36,7 +36,7 @@ export const pipelineTransformationsLogic = kea, { loadPlugins: async () => { - const results: PluginType[] = await loadPaginatedResults( + const results: PluginType[] = await api.loadPaginatedResults( `api/organizations/@current/pipeline_transformations` ) const plugins: Record = {} @@ -60,7 +60,7 @@ export const pipelineTransformationsLogic = kea { const pluginConfigs: Record = {} - const results = await loadPaginatedResults( + const results = await api.loadPaginatedResults( `api/projects/${values.currentTeamId}/pipeline_transformations_configs` ) @@ -185,21 +185,3 @@ export const pipelineTransformationsLogic = kea { - let results: any[] = [] - for (let i = 0; i <= maxIterations; ++i) { - if (!url) { - break - } - - const { results: partialResults, next } = await api.get(url) - results = results.concat(partialResults) - url = next - } - return results -} diff --git a/frontend/src/scenes/plugins/pluginsLogic.ts b/frontend/src/scenes/plugins/pluginsLogic.ts index 3e6954c3cf081..dcb20bcfccda5 100644 --- a/frontend/src/scenes/plugins/pluginsLogic.ts +++ b/frontend/src/scenes/plugins/pluginsLogic.ts @@ -29,8 +29,6 @@ export interface PluginSelectionType { url?: string } -const PAGINATION_DEFAULT_MAX_PAGES = 10 - function capturePluginEvent(event: string, plugin: PluginType, type?: PluginInstallationType): void { posthog.capture(event, { plugin_name: plugin.name, @@ -40,23 +38,6 @@ function capturePluginEvent(event: string, plugin: PluginType, type?: PluginInst }) } -async function loadPaginatedResults( - url: string | null, - maxIterations: number = PAGINATION_DEFAULT_MAX_PAGES -): Promise { - let results: any[] = [] - for (let i = 0; i <= maxIterations; ++i) { - if (!url) { - break - } - - const { results: partialResults, next } = await api.get(url) - results = results.concat(partialResults) - url = next - } - return results -} - export const pluginsLogic = kea([ path(['scenes', 'plugins', 'pluginsLogic']), connect(frontendAppsLogic), @@ -102,7 +83,7 @@ export const pluginsLogic = kea([ {} as Record, { loadPlugins: async () => { - const results: PluginType[] = await loadPaginatedResults('api/organizations/@current/plugins') + const results: PluginType[] = await api.loadPaginatedResults('api/organizations/@current/plugins') const plugins: Record = {} for (const plugin of results) { plugins[plugin.id] = plugin @@ -160,7 +141,7 @@ export const pluginsLogic = kea([ { loadPluginConfigs: async () => { const pluginConfigs: Record = {} - const results: PluginConfigType[] = await loadPaginatedResults('api/plugin_config') + const results: PluginConfigType[] = await api.loadPaginatedResults('api/plugin_config') for (const pluginConfig of results) { pluginConfigs[pluginConfig.plugin] = { ...pluginConfig } diff --git a/frontend/src/scenes/web-analytics/webAnalyticsLogic.ts b/frontend/src/scenes/web-analytics/webAnalyticsLogic.ts index 43a3740b04bff..84b83288da6cc 100644 --- a/frontend/src/scenes/web-analytics/webAnalyticsLogic.ts +++ b/frontend/src/scenes/web-analytics/webAnalyticsLogic.ts @@ -661,12 +661,38 @@ export const webAnalyticsLogic = kea([ shouldShowGeographyTile: { _default: null as boolean | null, loadShouldShowGeographyTile: async (): Promise => { - const response = await api.propertyDefinitions.list({ - event_names: ['$pageview'], - properties: ['$geoip_country_code'], - }) - const countryCodeDefinition = response.results.find((r) => r.name === '$geoip_country_code') - return !!countryCodeDefinition && !isDefinitionStale(countryCodeDefinition) + const [propertiesResponse, pluginsResponse, pluginsConfigResponse] = await Promise.allSettled([ + api.propertyDefinitions.list({ + event_names: ['$pageview'], + properties: ['$geoip_country_code'], + }), + api.loadPaginatedResults('api/organizations/@current/plugins'), + api.loadPaginatedResults('api/plugin_config'), + ]) + + const hasNonStaleCountryCodeDefinition = + propertiesResponse.status === 'fulfilled' && + propertiesResponse.value.results.some( + (property) => property.name === '$geoip_country_code' && !isDefinitionStale(property) + ) + + if (!hasNonStaleCountryCodeDefinition) { + return false + } + + const geoIpPlugin = + pluginsResponse.status === 'fulfilled' && + pluginsResponse.value.find( + (plugin) => plugin.url === 'https://www.npmjs.com/package/@posthog/geoip-plugin' + ) + const geoIpPluginId = geoIpPlugin ? geoIpPlugin.id : undefined + + const geoIpPluginConfig = + isNotNil(geoIpPluginId) && + pluginsConfigResponse.status === 'fulfilled' && + pluginsConfigResponse.value.find((plugin) => plugin.id === geoIpPluginId) + + return !!geoIpPluginConfig && geoIpPluginConfig.enabled }, }, })),