@skyhook-io/radar-app 1.22.2 → 1.23.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -8
- package/src/App.tsx +49 -13
- package/src/RadarApp.basename.test.tsx +177 -0
- package/src/RadarApp.settings.test.tsx +33 -0
- package/src/api/agentSandbox.ts +259 -0
- package/src/api/client.autoPromConnect.test.tsx +43 -0
- package/src/api/client.metricsRelayCheck.test.tsx +134 -0
- package/src/api/client.ts +151 -27
- package/src/api/client.updateResource.secretExpiry.test.tsx +47 -0
- package/src/api/cloudInstallDiscover.test.tsx +76 -0
- package/src/api/diagnose.ts +4 -0
- package/src/api/kagent.test.ts +82 -0
- package/src/api/kagent.ts +230 -0
- package/src/api/poll.ts +78 -0
- package/src/api/prometheusCharts.ts +5 -0
- package/src/api/radarFeatures.test.ts +9 -0
- package/src/api/radarFeatures.ts +16 -0
- package/src/api/substrateWorkspace.test.ts +72 -0
- package/src/api/substrateWorkspace.ts +1394 -0
- package/src/api/vulnerabilities.ts +207 -9
- package/src/api/workloadSource.test.tsx +315 -0
- package/src/api/workloadSource.ts +128 -12
- package/src/components/CloudConnectFlow.tsx +46 -7
- package/src/components/CloudFunnelButton.test.tsx +193 -7
- package/src/components/CloudFunnelButton.tsx +128 -32
- package/src/components/SelfManagedStart.tsx +12 -2
- package/src/components/agentsandbox/AgentSandboxKindLists.tsx +41 -0
- package/src/components/agentsandbox/AgentSandboxLists.test.tsx +410 -0
- package/src/components/agentsandbox/AgentSandboxOverview.test.tsx +333 -0
- package/src/components/agentsandbox/AgentSandboxOverview.tsx +555 -0
- package/src/components/agentsandbox/AgentSandboxSidebar.test.tsx +97 -0
- package/src/components/agentsandbox/AgentSandboxSummary.test.tsx +439 -0
- package/src/components/agentsandbox/AgentSandboxSummary.tsx +956 -0
- package/src/components/agentsandbox/AgentSandboxTables.tsx +966 -0
- package/src/components/agentsandbox/AgentSandboxView.tsx +34 -0
- package/src/components/agentsandbox/agentsandbox.fixtures.ts +135 -0
- package/src/components/agentsandbox/data.ts +135 -0
- package/src/components/agentsandbox/host.test.ts +27 -0
- package/src/components/agentsandbox/host.tsx +85 -0
- package/src/components/agentsandbox/labels.test.ts +42 -0
- package/src/components/agentsandbox/labels.ts +146 -0
- package/src/components/agentsandbox/listFilters.ts +85 -0
- package/src/components/agentsandbox/lists.ts +52 -0
- package/src/components/agentsandbox/routes.ts +23 -0
- package/src/components/agentsandbox/shared.tsx +321 -0
- package/src/components/agentsandbox/useAgentSandboxSidebarWorkspace.ts +69 -0
- package/src/components/agentsandbox/workspace.fixtures.ts +125 -0
- package/src/components/agentsandbox/workspace.tsx +258 -0
- package/src/components/agentsandbox/workspaceRoute.ts +18 -0
- package/src/components/applications/ApplicationsView.openResource.test.tsx +248 -3
- package/src/components/applications/ApplicationsView.tsx +156 -3
- package/src/components/audit/VulnerabilitiesColumns.test.tsx +487 -0
- package/src/components/audit/VulnerabilitiesRisk.test.tsx +318 -0
- package/src/components/audit/VulnerabilitiesView.test.tsx +458 -13
- package/src/components/audit/VulnerabilitiesView.tsx +639 -199
- package/src/components/cloudHints/AgentSetupCloudRow.tsx +28 -0
- package/src/components/cloudHints/CloudHintLink.tsx +29 -0
- package/src/components/cloudHints/SharedInstallAiLine.tsx +36 -0
- package/src/components/cloudHints/cloudHints.test.ts +126 -0
- package/src/components/cloudHints/cloudHints.ts +131 -0
- package/src/components/cloudHints/cloudHintsGate.test.tsx +70 -0
- package/src/components/cloudHints/cloudLinks.test.ts +52 -0
- package/src/components/cloudHints/cloudLinks.ts +31 -0
- package/src/components/cloudHints/hintStorage.ts +28 -0
- package/src/components/cnpg/CNPGView.tsx +4 -7
- package/src/components/diagnose/AgentSetupNotice.tsx +5 -0
- package/src/components/diagnose/DiagnoseContext.tsx +7 -0
- package/src/components/diagnose/DiagnoseSurface.escape.test.tsx +215 -0
- package/src/components/diagnose/DiagnoseSurface.tsx +57 -1
- package/src/components/gitops/RemoteDestinationCloudHint.tsx +6 -14
- package/src/components/helm/HelmDrawerRouting.test.tsx +34 -1
- package/src/components/helm/HelmReleaseDrawer.tsx +6 -4
- package/src/components/helm/HelmView.tsx +4 -3
- package/src/components/helm/ObservedHelmNotice.tsx +39 -17
- package/src/components/helm/ObservedHelmRelease.test.tsx +62 -3
- package/src/components/helm/ObservedHelmReleaseDrawer.test.tsx +18 -0
- package/src/components/helm/ObservedHelmReleaseDrawer.tsx +4 -2
- package/src/components/home/HomeAgentBlock.tsx +2 -0
- package/src/components/home/HomeView.tsx +3 -3
- package/src/components/home/MCPSetupDialog.test.tsx +9 -0
- package/src/components/home/MCPSetupDialog.tsx +6 -2
- package/src/components/home/McpCloudLine.test.tsx +162 -0
- package/src/components/home/McpCloudLine.tsx +95 -0
- package/src/components/home/ProblemsPanel.investigate.test.tsx +2 -2
- package/src/components/home/mcpToolCatalog.test.ts +10 -0
- package/src/components/home/mcpToolCatalog.ts +4 -4
- package/src/components/issues/ClusterSchedulingBlockerRow.tsx +35 -0
- package/src/components/issues/IssuesPane.render.test.tsx +36 -1
- package/src/components/issues/IssuesPane.tsx +181 -23
- package/src/components/issues/SinceLastVisitRow.render.test.tsx +302 -0
- package/src/components/issues/SinceLastVisitRow.tsx +71 -0
- package/src/components/issues/sinceLastVisit.test.ts +266 -0
- package/src/components/issues/sinceLastVisit.ts +439 -0
- package/src/components/issues/uncheckedSources.test.ts +8 -0
- package/src/components/issues/uncheckedSources.ts +3 -0
- package/src/components/kagent/KagentAgentRenderer.test.ts +32 -0
- package/src/components/kagent/KagentAgentRenderer.tsx +87 -0
- package/src/components/kagent/KagentAgentSessions.test.tsx +166 -0
- package/src/components/kagent/KagentAgentSessions.tsx +124 -0
- package/src/components/kagent/KagentDetailPage.tsx +106 -0
- package/src/components/kagent/KagentLists.tsx +412 -0
- package/src/components/kagent/KagentOverview.tsx +234 -0
- package/src/components/kagent/KagentPages.tsx +690 -0
- package/src/components/kagent/KagentRuntimeTabs.test.tsx +219 -0
- package/src/components/kagent/KagentRuntimeTabs.tsx +283 -0
- package/src/components/kagent/KagentSidebar.test.tsx +112 -0
- package/src/components/kagent/KagentView.tsx +109 -0
- package/src/components/kagent/KagentWorkspace.test.tsx +876 -0
- package/src/components/kagent/host.tsx +144 -0
- package/src/components/kagent/routes.ts +31 -0
- package/src/components/kagent/shared.tsx +336 -0
- package/src/components/kagent/useKagentRuntime.test.tsx +77 -0
- package/src/components/kagent/useKagentSidebarWorkspace.ts +67 -0
- package/src/components/kagent/workspace.fixtures.ts +108 -0
- package/src/components/kagent/workspaceRoute.ts +18 -0
- package/src/components/nudges/activeNudges.test.ts +39 -0
- package/src/components/nudges/activeNudges.ts +76 -0
- package/src/components/poll/OssPoll.tsx +871 -0
- package/src/components/poll/mount.test.ts +12 -0
- package/src/components/poll/pollCloud.test.ts +23 -0
- package/src/components/poll/pollCloud.ts +6 -0
- package/src/components/poll/pollDefinition.ts +93 -0
- package/src/components/poll/pollFlow.test.ts +140 -0
- package/src/components/poll/pollFlow.ts +137 -0
- package/src/components/poll/pollSender.test.ts +137 -0
- package/src/components/poll/pollSender.ts +76 -0
- package/src/components/poll/pollStorage.test.ts +53 -0
- package/src/components/poll/pollStorage.ts +77 -0
- package/src/components/poll/round.json +350 -0
- package/src/components/product-hunt/ProductHuntNotice.tsx +2 -0
- package/src/components/resource/MetricsNotConnected.test.tsx +28 -0
- package/src/components/resource/MetricsNotConnected.tsx +10 -7
- package/src/components/resource/PrometheusCharts.tsx +10 -1
- package/src/components/resource/PrometheusChartsGrid.render.test.tsx +9 -0
- package/src/components/resource/PrometheusChartsGrid.tsx +3 -3
- package/src/components/resource/SubstrateHealthCharts.test.tsx +13 -1
- package/src/components/resource/SubstrateHealthCharts.tsx +9 -15
- package/src/components/resources/ImageFilesystemModal.test.tsx +21 -1
- package/src/components/resources/ImageFilesystemModal.tsx +42 -3
- package/src/components/resources/ResourceDetailDrawer.tsx +1 -0
- package/src/components/resources/ResourcesView.tsx +24 -11
- package/src/components/resources/renderers/PodRenderer.tsx +8 -2
- package/src/components/settings/LocalIntegrationStatus.test.tsx +1 -0
- package/src/components/settings/LocalIntegrationStatus.tsx +3 -2
- package/src/components/settings/SettingsDialog.tsx +84 -16
- package/src/components/settings/SubstrateSettings.lifecycle.test.tsx +210 -0
- package/src/components/settings/SubstrateSettings.test.tsx +131 -0
- package/src/components/settings/SubstrateSettings.tsx +544 -0
- package/src/components/settings/settings-state.test.ts +21 -3
- package/src/components/settings/settings-state.ts +13 -3
- package/src/components/share/ShareLinkButton.test.tsx +360 -0
- package/src/components/share/ShareLinkButton.tsx +391 -0
- package/src/components/share/openShareInCloud.test.ts +29 -0
- package/src/components/share/openShareInCloud.ts +10 -0
- package/src/components/share/shareLink.test.ts +129 -0
- package/src/components/share/shareLink.ts +175 -0
- package/src/components/substrate/SubstrateActorDrawer.tsx +87 -0
- package/src/components/substrate/SubstrateActorOverview.tsx +489 -0
- package/src/components/substrate/SubstrateActorPage.tsx +384 -0
- package/src/components/substrate/SubstrateActors.test.tsx +1094 -0
- package/src/components/substrate/SubstrateActors.tsx +612 -0
- package/src/components/substrate/SubstrateAttentionList.tsx +441 -0
- package/src/components/substrate/SubstrateConnect.tsx +88 -0
- package/src/components/substrate/SubstrateOverview.test.tsx +396 -0
- package/src/components/substrate/SubstrateOverview.tsx +575 -0
- package/src/components/substrate/SubstratePages.test.tsx +478 -0
- package/src/components/substrate/SubstratePool.test.tsx +462 -0
- package/src/components/substrate/SubstratePoolPage.tsx +95 -0
- package/src/components/substrate/SubstratePoolSummary.tsx +485 -0
- package/src/components/substrate/SubstrateSidebar.test.tsx +101 -0
- package/src/components/substrate/SubstrateStateBar.tsx +63 -0
- package/src/components/substrate/SubstrateTemplates.tsx +376 -0
- package/src/components/substrate/SubstrateView.tsx +173 -0
- package/src/components/substrate/SubstrateWorkerPodContext.tsx +73 -0
- package/src/components/substrate/SubstrateWorkers.tsx +389 -0
- package/src/components/substrate/attention.ts +81 -0
- package/src/components/substrate/contextPin.tsx +69 -0
- package/src/components/substrate/host.tsx +83 -0
- package/src/components/substrate/kubectlTarget.test.tsx +77 -0
- package/src/components/substrate/readings.test.ts +484 -0
- package/src/components/substrate/readings.ts +997 -0
- package/src/components/substrate/routes.ts +103 -0
- package/src/components/substrate/shared.tsx +426 -0
- package/src/components/substrate/substrate.fixtures.ts +325 -0
- package/src/components/substrate/useSubstrateSidebarWorkspace.ts +87 -0
- package/src/components/substrate/workspaceRoute.ts +23 -0
- package/src/components/traffic/TrafficConnectionsTable.test.tsx +9 -0
- package/src/components/traffic/TrafficConnectionsTable.tsx +4 -2
- package/src/components/traffic/TrafficFilterSidebar.tsx +19 -14
- package/src/components/traffic/TrafficFlowList.test.tsx +63 -0
- package/src/components/traffic/TrafficFlowList.tsx +9 -5
- package/src/components/traffic/TrafficGraph.test.tsx +26 -0
- package/src/components/traffic/TrafficGraph.tsx +17 -14
- package/src/components/traffic/TrafficGraphTooLarge.tsx +2 -0
- package/src/components/traffic/TrafficGraphUnavailable.tsx +3 -1
- package/src/components/traffic/TrafficView.tsx +26 -56
- package/src/components/traffic/trafficFilters.test.ts +61 -1
- package/src/components/traffic/trafficFilters.ts +68 -8
- package/src/components/ui/UpdateNotification.tsx +2 -0
- package/src/components/usage-data/UsageDataPrompt.tsx +2 -0
- package/src/components/velero/VeleroOverview.tsx +2 -2
- package/src/components/velero/VeleroView.tsx +3 -6
- package/src/components/vulnerabilities/columns.tsx +132 -0
- package/src/components/vulnerabilities/filters.tsx +317 -0
- package/src/components/vulnerabilities/owner.test.ts +54 -0
- package/src/components/vulnerabilities/owner.tsx +163 -0
- package/src/components/vulnerabilities/parts.test.ts +126 -12
- package/src/components/vulnerabilities/parts.tsx +612 -52
- package/src/components/vulnerabilities/traffic.test.tsx +65 -0
- package/src/components/vulnerabilities/traffic.tsx +40 -0
- package/src/components/whats-new/WhatsNew.tsx +2 -0
- package/src/components/whats-new/releaseNotes.ts +75 -1
- package/src/components/workload/WorkloadView.substrateVersion.test.tsx +88 -0
- package/src/components/workload/WorkloadView.tsx +52 -13
- package/src/components/workspace/layout.tsx +13 -7
- package/src/components/workspace/sidebar.test.ts +48 -0
- package/src/components/workspace/sidebar.ts +73 -3
- package/src/components/workspace/table.ts +2 -1
- package/src/contexts/RegistryAccessMode.test.tsx +51 -0
- package/src/contexts/RegistryAccessMode.tsx +25 -0
- package/src/integrations/resourceHost.ts +12 -6
- package/src/integrations/resourceHosts.test.tsx +27 -1
- package/src/integrations/resourceHosts.tsx +15 -7
- package/src/integrations/sidebarWorkspaces.test.tsx +63 -0
- package/src/integrations/sidebarWorkspaces.ts +41 -0
- package/src/integrations/workspaceRoute.ts +1 -1
- package/src/integrations/workspaceRoutes.ts +7 -1
- package/src/integrations/workspaceScreens.tsx +6 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { KagentAgent, KagentRef, KagentWorkspace } from '../../api/kagent'
|
|
2
|
+
export const ref = (kind: string, name: string, namespace = 'agents'): KagentRef => ({ group: 'api.kagent.dev', kind, namespace, name })
|
|
3
|
+
export function workspace(): KagentWorkspace {
|
|
4
|
+
const keys = [
|
|
5
|
+
'agents',
|
|
6
|
+
'agentTemplates',
|
|
7
|
+
'harnesses',
|
|
8
|
+
'models',
|
|
9
|
+
'mcpServers',
|
|
10
|
+
'egressPolicies',
|
|
11
|
+
'modelProviders',
|
|
12
|
+
'sandboxTemplates',
|
|
13
|
+
] as const
|
|
14
|
+
const coverage = Object.fromEntries(keys.map((k) => [k, { state: 'full' }])) as KagentWorkspace['coverage']
|
|
15
|
+
const count = { n: 0, lowerBound: false }
|
|
16
|
+
return {
|
|
17
|
+
context: 'test',
|
|
18
|
+
installed: true,
|
|
19
|
+
namespaces: [],
|
|
20
|
+
coverage,
|
|
21
|
+
referenceCoverage: coverage,
|
|
22
|
+
agents: [],
|
|
23
|
+
models: [],
|
|
24
|
+
mcpServers: [],
|
|
25
|
+
modelProviders: [],
|
|
26
|
+
findings: [],
|
|
27
|
+
groups: [],
|
|
28
|
+
counts: {
|
|
29
|
+
findings: { ...count },
|
|
30
|
+
critical: { ...count },
|
|
31
|
+
agents: { ...count },
|
|
32
|
+
agentsAffected: { ...count },
|
|
33
|
+
mcpServers: { ...count },
|
|
34
|
+
mcpServersFailing: { ...count },
|
|
35
|
+
models: { ...count },
|
|
36
|
+
modelsWithProblems: { ...count },
|
|
37
|
+
groups: { n: 0, incomplete: false },
|
|
38
|
+
agentsByLatestChange: {},
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function agent(name = 'helper'): KagentAgent {
|
|
43
|
+
return {
|
|
44
|
+
ref: ref('Agent', name),
|
|
45
|
+
reading: { state: 'ready', tone: 'healthy', text: 'Ready', summary: 'Ready to start sessions', condition: 'Ready' },
|
|
46
|
+
latestChange: { state: 'prepared' },
|
|
47
|
+
newSessionsRevision: 'abcdef0123456789',
|
|
48
|
+
inlineTemplate: true,
|
|
49
|
+
description: 'Helps operate the cluster',
|
|
50
|
+
mcpServers: { declared: 0, failing: 0, absent: 0, unread: 0, notPermitted: 0 },
|
|
51
|
+
references: [],
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function blockedWorkspace(): KagentWorkspace {
|
|
55
|
+
const w = workspace(),
|
|
56
|
+
a = agent('blocked-edit'),
|
|
57
|
+
model = ref('ModelConfig', 'broken-model', 'models')
|
|
58
|
+
a.latestChange = { state: 'blocked', message: 'resolve ModelConfig "models/broken-model": secret missing not found' }
|
|
59
|
+
a.reading = { ...a.reading, tone: 'alert', state: 'blocked', text: 'Blocked', message: a.latestChange.message }
|
|
60
|
+
a.model = {
|
|
61
|
+
ref: model,
|
|
62
|
+
state: 'present',
|
|
63
|
+
reading: { state: 'blocked', tone: 'alert', text: 'Blocked', summary: 'API key Secret missing', reason: 'APIKeySecretNotFound' },
|
|
64
|
+
}
|
|
65
|
+
a.references = [
|
|
66
|
+
{ role: 'model', from: a.ref, target: model, state: 'present', field: 'spec.template.inline.modelConfig', reading: a.model.reading },
|
|
67
|
+
]
|
|
68
|
+
w.agents = [a]
|
|
69
|
+
w.models = [
|
|
70
|
+
{
|
|
71
|
+
ref: model,
|
|
72
|
+
reading: a.model.reading!,
|
|
73
|
+
provider: 'OpenAI',
|
|
74
|
+
model: 'gpt',
|
|
75
|
+
credential: { mode: 'apiKeySecret', secret: { ...ref('Secret', 'missing', 'models'), group: '' }, key: 'OPENAI_API_KEY' },
|
|
76
|
+
usedBy: { count: 1, lowerBound: false, consumers: 'seen', agents: [{ agent: a.ref, effect: 'blocked', roles: ['model'] }] },
|
|
77
|
+
},
|
|
78
|
+
]
|
|
79
|
+
w.findings = [
|
|
80
|
+
{
|
|
81
|
+
id: 'agent',
|
|
82
|
+
subject: a.ref,
|
|
83
|
+
severity: 'warning',
|
|
84
|
+
reason: 'KagentChangeBlocked',
|
|
85
|
+
message: 'Blocked edit',
|
|
86
|
+
action: 'Fix the model.',
|
|
87
|
+
cause: { target: model, form: 'failed', role: 'model' },
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: 'model',
|
|
91
|
+
subject: model,
|
|
92
|
+
severity: 'warning',
|
|
93
|
+
reason: 'KagentAPIKeySecretMissing',
|
|
94
|
+
kagentReason: 'APIKeySecretNotFound',
|
|
95
|
+
message: 'secret missing not found',
|
|
96
|
+
action: 'Create Secret models/missing.',
|
|
97
|
+
affects: w.models[0].usedBy,
|
|
98
|
+
},
|
|
99
|
+
]
|
|
100
|
+
w.groups = [{ id: 'model', severity: 'warning', findings: ['model'], folded: [{ finding: 'agent', under: 'model' }] }]
|
|
101
|
+
w.counts.findings.n = 2
|
|
102
|
+
w.counts.groups.n = 1
|
|
103
|
+
w.counts.agents.n = 1
|
|
104
|
+
w.counts.models.n = 1
|
|
105
|
+
w.counts.modelsWithProblems.n = 1
|
|
106
|
+
w.counts.agentsByLatestChange = { blocked: 1 }
|
|
107
|
+
return w
|
|
108
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { WorkspaceRoute } from '../../integrations/workspaceRoute'
|
|
2
|
+
import { parseKagentRoute } from './routes'
|
|
3
|
+
export const kagentWorkspaceRoute: WorkspaceRoute = {
|
|
4
|
+
id: 'kagent',
|
|
5
|
+
path: '/kagent',
|
|
6
|
+
label: 'kagent',
|
|
7
|
+
navView: 'resources',
|
|
8
|
+
namespaceScope: 'view',
|
|
9
|
+
pageTitle: (path) => {
|
|
10
|
+
const r = parseKagentRoute(path)
|
|
11
|
+
return r.detail?.name ?? `kagent ${r.screen === 'mcpservers' ? 'MCP servers' : r.screen}`
|
|
12
|
+
},
|
|
13
|
+
contextSwitchSearch(path, search) {
|
|
14
|
+
const next = new URLSearchParams()
|
|
15
|
+
if (parseKagentRoute(path).detail && search.has('ctx')) next.set('ctx', search.get('ctx')!)
|
|
16
|
+
return next
|
|
17
|
+
},
|
|
18
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
2
|
+
import { otherNudgeSeenThisLoad, otherNudgeVisible, releaseNudge, resetNudgesForTest, retainNudge } from './activeNudges'
|
|
3
|
+
|
|
4
|
+
afterEach(resetNudgesForTest)
|
|
5
|
+
|
|
6
|
+
describe('active nudges', () => {
|
|
7
|
+
it('reports other nudges but not the caller', () => {
|
|
8
|
+
retainNudge('oss-poll')
|
|
9
|
+
expect(otherNudgeVisible('oss-poll')).toBe(false)
|
|
10
|
+
retainNudge('update-notice')
|
|
11
|
+
expect(otherNudgeVisible('oss-poll')).toBe(true)
|
|
12
|
+
releaseNudge('update-notice')
|
|
13
|
+
expect(otherNudgeVisible('oss-poll')).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('remembers any other nudge shown this load, after it closes', () => {
|
|
17
|
+
retainNudge('oss-poll')
|
|
18
|
+
expect(otherNudgeSeenThisLoad('oss-poll')).toBe(false)
|
|
19
|
+
retainNudge('whats-new')
|
|
20
|
+
releaseNudge('whats-new')
|
|
21
|
+
expect(otherNudgeVisible('oss-poll')).toBe(false)
|
|
22
|
+
expect(otherNudgeSeenThisLoad('oss-poll')).toBe(true)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('keeps a nudge mounted in two places visible until both hide', () => {
|
|
26
|
+
retainNudge('update-notice')
|
|
27
|
+
retainNudge('update-notice')
|
|
28
|
+
releaseNudge('update-notice')
|
|
29
|
+
expect(otherNudgeVisible('oss-poll')).toBe(true)
|
|
30
|
+
releaseNudge('update-notice')
|
|
31
|
+
expect(otherNudgeVisible('oss-poll')).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('ignores a release with nothing retained', () => {
|
|
35
|
+
releaseNudge('usage-prompt')
|
|
36
|
+
expect(otherNudgeVisible('oss-poll')).toBe(false)
|
|
37
|
+
expect(otherNudgeSeenThisLoad('oss-poll')).toBe(false)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useEffect, useSyncExternalStore } from 'react'
|
|
2
|
+
|
|
3
|
+
// Nudges (the star callout, the update notice, the usage-stats card, the
|
|
4
|
+
// Product Hunt card, What's New and the poll) each mark
|
|
5
|
+
// themselves visible here. Low-priority nudges read it and stay away while
|
|
6
|
+
// anything else is showing, so a person never gets two asks at once.
|
|
7
|
+
// Counted per id: one nudge can be mounted in two places (the update notice
|
|
8
|
+
// floats in the corner and sits in Settings), and one instance hiding must not
|
|
9
|
+
// clear the other.
|
|
10
|
+
const active = new Map<string, number>()
|
|
11
|
+
// Nudges seen since this page loaded, visible or not now. A low-priority nudge
|
|
12
|
+
// stays away for the rest of a load in which another one appeared.
|
|
13
|
+
const seenThisLoad = new Set<string>()
|
|
14
|
+
const listeners = new Set<() => void>()
|
|
15
|
+
let version = 0
|
|
16
|
+
|
|
17
|
+
function emit() {
|
|
18
|
+
version++
|
|
19
|
+
listeners.forEach((l) => l())
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function subscribe(listener: () => void) {
|
|
23
|
+
listeners.add(listener)
|
|
24
|
+
return () => {
|
|
25
|
+
listeners.delete(listener)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Marks one instance of a nudge visible. Pair every call with releaseNudge. */
|
|
30
|
+
export function retainNudge(id: string) {
|
|
31
|
+
seenThisLoad.add(id)
|
|
32
|
+
const count = active.get(id) ?? 0
|
|
33
|
+
active.set(id, count + 1)
|
|
34
|
+
if (count === 0) emit()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Marks one instance of a nudge hidden again. */
|
|
38
|
+
export function releaseNudge(id: string) {
|
|
39
|
+
const count = active.get(id) ?? 0
|
|
40
|
+
if (count <= 1) {
|
|
41
|
+
if (active.delete(id)) emit()
|
|
42
|
+
} else active.set(id, count - 1)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Marks a nudge visible while `visible` is true, and clears it on unmount. */
|
|
46
|
+
export function useNudgeVisible(id: string, visible: boolean) {
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (!visible) return
|
|
49
|
+
retainNudge(id)
|
|
50
|
+
return () => releaseNudge(id)
|
|
51
|
+
}, [id, visible])
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** True when any nudge other than `self` is showing. */
|
|
55
|
+
export function otherNudgeVisible(self?: string): boolean {
|
|
56
|
+
for (const id of active.keys()) if (id !== self) return true
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useOtherNudgeVisible(self?: string): boolean {
|
|
61
|
+
useSyncExternalStore(subscribe, () => version)
|
|
62
|
+
return otherNudgeVisible(self)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** True when a nudge other than `self` has shown since this page loaded. */
|
|
66
|
+
export function otherNudgeSeenThisLoad(self?: string): boolean {
|
|
67
|
+
for (const id of seenThisLoad) if (id !== self) return true
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Test-only reset. */
|
|
72
|
+
export function resetNudgesForTest() {
|
|
73
|
+
active.clear()
|
|
74
|
+
seenThisLoad.clear()
|
|
75
|
+
emit()
|
|
76
|
+
}
|