:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, afterMount, connect, kea, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { starredSpaces, todaySpacesLogic } from '~/layout/today/todaySpacesLogic'
import { Breadcrumb } from '~/types'
import { taskChannelsContributorsRetrieve, tasksList } from '../generated/api'
import { ChannelDTOApi, TaskListItemApi, TaskUserBasicInfoApi } from '../generated/api.schemas'
import { PRESENCE_RECENT_WINDOW_MS, SpacePresence, presenceBySpace } from './spacePresence'
const SPACE_PRESENCE_FETCH_LIMIT = 100
const SPACE_PRESENCE_MAX_PAGES = 10
const SPACE_PRESENCE_POLL_INTERVAL_MS = 90_000
export interface SpacesIndexLists {
starred: ChannelDTOApi[]
rest: ChannelDTOApi[]
}
export function spacesIndexLists(spaces: ChannelDTOApi[], search: string): SpacesIndexLists {
const needle = search.trim().toLowerCase()
const shown = needle ? spaces.filter((space) => space.name.toLowerCase().includes(needle)) : spaces
const starred = starredSpaces(shown)
const starredIds = new Set(starred.map((space) => space.id))
return { starred, rest: shown.filter((space) => !starredIds.has(space.id)) }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spacesSceneLogicValues {
currentTeamId: number | null // teamLogic
pendingSpaceIds: string[] // todaySpacesLogic
sortedSpaces: ChannelDTOApi[] // todaySpacesLogic
spacesLoading: boolean // todaySpacesLogic
spacesUnavailable: boolean // todaySpacesLogic
breadcrumbs: Breadcrumb[]
contributors: Record<string, TaskUserBasicInfoApi[]> | null
contributorsLoading: boolean
lists: SpacesIndexLists
search: string
spacePresence: Record<string, SpacePresence>
spacePresenceLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spacesSceneLogicActions {
loadSpaces: () => any // todaySpacesLogic
toggleStar: (
spaceId: string,
starred: boolean
) => {
spaceId: string
starred: boolean
} // todaySpacesLogic
loadContributors: () => any
loadContributorsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadContributorsSuccess: (
contributors: {
[k: string]: TaskUserBasicInfoApi[]
} | null,
payload?: any
) => {
contributors: {
[k: string]: TaskUserBasicInfoApi[]
} | null
payload?: any
}
loadSpacePresence: () => any
loadSpacePresenceFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSpacePresenceSuccess: (
spacePresence: Record<string, SpacePresence>,
payload?: any
) => {
spacePresence: Record<string, SpacePresence>
payload?: any
}
setSearch: (search: string) => {
search: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface spacesSceneLogicMeta {
__keaTypeGenInternalSelectorTypes: {
lists: (sortedSpaces: ChannelDTOApi[], search: string) => SpacesIndexLists
}
}
export type spacesSceneLogicType = MakeLogicType<
spacesSceneLogicValues,
spacesSceneLogicActions,
Record<string, any>,
spacesSceneLogicMeta
>
export const spacesSceneLogic = kea<spacesSceneLogicType>([
path(['products', 'tasks', 'spaces', 'spacesSceneLogic']),
connect(() => ({
values: [
todaySpacesLogic,
['sortedSpaces', 'spacesLoading', 'spacesUnavailable', 'pendingSpaceIds'],
teamLogic,
['currentTeamId'],
],
actions: [todaySpacesLogic, ['loadSpaces', 'toggleStar']],
})),
actions({
setSearch: (search: string) => ({ search }),
}),
reducers({
search: ['', { setSearch: (_, { search }) => search }],
}),
loaders(({ values }) => ({
spacePresence: [
{} as Record<string, SpacePresence>,
{
loadSpacePresence: async () => {
if (!values.currentTeamId) {
return {}
}
const now = Date.now()
const tasks: TaskListItemApi[] = []
for (let page = 0; page < SPACE_PRESENCE_MAX_PAGES; page++) {
const response = await tasksList(String(values.currentTeamId), {
ordering: '-last_activity_at',
basic: true,
limit: SPACE_PRESENCE_FETCH_LIMIT,
offset: page * SPACE_PRESENCE_FETCH_LIMIT,
})
tasks.push(...response.results)
const oldest = response.results[response.results.length - 1]?.last_activity_at
// The list is newest first, so a page that reaches past the window holds the last faces.
if (!response.next || !oldest || now - Date.parse(oldest) >= PRESENCE_RECENT_WINDOW_MS) {
break
}
}
return presenceBySpace(tasks, now)
},
},
],
contributors: [
null as Record<string, TaskUserBasicInfoApi[]> | null,
{
loadContributors: async () => {
if (!values.currentTeamId) {
return null
}
const rows = await taskChannelsContributorsRetrieve(String(values.currentTeamId))
return Object.fromEntries(rows.map((row) => [row.channel, row.people]))
},
},
],
})),
selectors({
lists: [
(s) => [s.sortedSpaces, s.search],
(sortedSpaces: ChannelDTOApi[], search: string): SpacesIndexLists => spacesIndexLists(sortedSpaces, search),
],
breadcrumbs: [
() => [],
(): Breadcrumb[] => [{ key: Scene.TaskSpaces, name: 'Spaces', path: urls.taskSpaces(), iconType: 'task' }],
],
}),
afterMount(({ actions, cache }) => {
actions.loadContributors()
cache.disposables.add(() => {
actions.loadSpacePresence()
const pollTimer = window.setInterval(() => actions.loadSpacePresence(), SPACE_PRESENCE_POLL_INTERVAL_MS)
return () => clearInterval(pollTimer)
}, 'spacePresencePoll')
}),
])