mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
208 lines
7.2 KiB
TypeScript
208 lines
7.2 KiB
TypeScript
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
|
|
import { router } from 'kea-router'
|
|
import { subscriptions } from 'kea-subscriptions'
|
|
|
|
import { getCurrentTeamIdOrNone } from 'lib/utils/getAppContext'
|
|
import { teamLogic } from 'scenes/teamLogic'
|
|
import { urls } from 'scenes/urls'
|
|
|
|
import type { TeamPublicType, TeamType } from '../../../frontend/src/types'
|
|
import type { NotificationPermission } from './types'
|
|
|
|
const STORAGE_KEY_PREFIX = 'insights-support-notifications'
|
|
const NOTIFICATION_TAG = 'insights-support-ticket'
|
|
const NOTIFICATION_ICON = '/static/insights-icon.svg'
|
|
const NOTIFICATION_AUTO_CLOSE_MS = 5000
|
|
|
|
const getStorageKey = (): string => {
|
|
const teamId = getCurrentTeamIdOrNone() ?? teamLogic.findMounted()?.values.currentTeamId ?? 'null'
|
|
return `${STORAGE_KEY_PREFIX}-${teamId}`
|
|
}
|
|
|
|
const isNotificationSupported = (): boolean => {
|
|
return typeof window !== 'undefined' && 'Notification' in window
|
|
}
|
|
|
|
const getStoredPreference = (): boolean => {
|
|
if (typeof localStorage === 'undefined') {
|
|
return false
|
|
}
|
|
const stored = localStorage.getItem(getStorageKey())
|
|
return stored === 'true'
|
|
}
|
|
|
|
const setStoredPreference = (enabled: boolean): void => {
|
|
if (typeof localStorage === 'undefined') {
|
|
return
|
|
}
|
|
if (enabled) {
|
|
localStorage.setItem(getStorageKey(), 'true')
|
|
} else {
|
|
localStorage.removeItem(getStorageKey())
|
|
}
|
|
}
|
|
|
|
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
|
|
export interface browserNotificationLogicValues {
|
|
currentTeam: TeamPublicType | TeamType | null // teamLogic
|
|
canShowNotifications: boolean
|
|
enabled: boolean
|
|
isPermissionDenied: boolean
|
|
isSupported: boolean
|
|
permission: NotificationPermission
|
|
}
|
|
|
|
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
|
|
export interface browserNotificationLogicActions {
|
|
requestPermission: () => {
|
|
value: true
|
|
}
|
|
setEnabled: (enabled: boolean) => {
|
|
enabled: boolean
|
|
}
|
|
setPermission: (permission: NotificationPermission) => {
|
|
permission: NotificationPermission
|
|
}
|
|
showNotification: (count: number) => {
|
|
count: number
|
|
}
|
|
}
|
|
|
|
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
|
|
export interface browserNotificationLogicMeta {
|
|
__keaTypeGenInternalSelectorTypes: {
|
|
canShowNotifications: (
|
|
isSupported: boolean,
|
|
permission: NotificationPermission,
|
|
enabled: boolean,
|
|
currentTeam: TeamPublicType | TeamType | null
|
|
) => boolean
|
|
isPermissionDenied: (isSupported: boolean, permission: NotificationPermission) => boolean
|
|
}
|
|
}
|
|
|
|
export type browserNotificationLogicType = MakeLogicType<
|
|
browserNotificationLogicValues,
|
|
browserNotificationLogicActions,
|
|
Record<string, any>,
|
|
browserNotificationLogicMeta
|
|
>
|
|
|
|
export const browserNotificationLogic = kea<browserNotificationLogicType>([
|
|
path(['products', 'conversations', 'frontend', 'browserNotificationLogic']),
|
|
connect(() => ({
|
|
values: [teamLogic, ['currentTeam']],
|
|
})),
|
|
actions({
|
|
requestPermission: true,
|
|
setPermission: (permission: NotificationPermission) => ({ permission }),
|
|
setEnabled: (enabled: boolean) => ({ enabled }),
|
|
showNotification: (count: number) => ({ count }),
|
|
}),
|
|
reducers({
|
|
permission: [
|
|
(isNotificationSupported() ? Notification.permission : 'denied') as NotificationPermission,
|
|
{
|
|
setPermission: (_, { permission }) => permission,
|
|
},
|
|
],
|
|
enabled: [
|
|
false,
|
|
{
|
|
setEnabled: (_, { enabled }) => enabled,
|
|
},
|
|
],
|
|
}),
|
|
listeners(({ actions }) => ({
|
|
requestPermission: async () => {
|
|
if (!isNotificationSupported()) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const result = await Notification.requestPermission()
|
|
actions.setPermission(result as NotificationPermission)
|
|
|
|
// Auto-enable if permission granted
|
|
if (result === 'granted') {
|
|
actions.setEnabled(true)
|
|
}
|
|
} catch {
|
|
// Permission request failed - likely user dismissed
|
|
actions.setPermission(Notification.permission as NotificationPermission)
|
|
}
|
|
},
|
|
setEnabled: ({ enabled }) => {
|
|
setStoredPreference(enabled)
|
|
},
|
|
showNotification: ({ count }) => {
|
|
// Defense in depth - caller should check canShowNotifications but verify here too
|
|
if (!isNotificationSupported() || Notification.permission !== 'granted') {
|
|
return
|
|
}
|
|
|
|
const title = count === 1 ? 'New support message' : `${count} unread support messages`
|
|
const body = 'Click to view your support tickets'
|
|
|
|
const notification = new Notification(title, {
|
|
body,
|
|
icon: NOTIFICATION_ICON,
|
|
tag: NOTIFICATION_TAG, // Replaces previous notification with same tag
|
|
requireInteraction: false,
|
|
})
|
|
|
|
notification.onclick = () => {
|
|
// Focus the window/tab
|
|
window.focus()
|
|
// Navigate to tickets
|
|
router.actions.push(urls.supportTickets())
|
|
// Close the notification
|
|
notification.close()
|
|
}
|
|
|
|
// Auto-close after timeout
|
|
setTimeout(() => {
|
|
notification.close()
|
|
}, NOTIFICATION_AUTO_CLOSE_MS)
|
|
},
|
|
})),
|
|
selectors({
|
|
isSupported: [() => [], () => isNotificationSupported()],
|
|
canShowNotifications: [
|
|
(s) => [s.isSupported, s.permission, s.enabled, s.currentTeam],
|
|
(
|
|
isSupported: boolean,
|
|
permission: NotificationPermission,
|
|
enabled: boolean,
|
|
currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType
|
|
): boolean => isSupported && permission === 'granted' && enabled && !!currentTeam?.conversations_enabled,
|
|
],
|
|
isPermissionDenied: [
|
|
(s) => [s.isSupported, s.permission],
|
|
(isSupported: boolean, permission: NotificationPermission): boolean =>
|
|
isSupported && permission === 'denied',
|
|
],
|
|
}),
|
|
subscriptions(({ actions }) => ({
|
|
// Reset enabled preference when team changes (keep in sync with new team's localStorage)
|
|
currentTeam: (currentTeam, oldTeam) => {
|
|
// Skip initial mount
|
|
if (oldTeam === undefined) {
|
|
return
|
|
}
|
|
// Team changed - load preference for new team
|
|
if (currentTeam?.id !== oldTeam?.id) {
|
|
actions.setEnabled(getStoredPreference())
|
|
}
|
|
},
|
|
})),
|
|
afterMount(({ actions }) => {
|
|
// Load initial permission state
|
|
if (isNotificationSupported()) {
|
|
actions.setPermission(Notification.permission as NotificationPermission)
|
|
}
|
|
// Load stored preference
|
|
actions.setEnabled(getStoredPreference())
|
|
}),
|
|
])
|