Skip to content
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
7 changes: 4 additions & 3 deletions apps/server/src/app/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { subscriptionRevocationMaintenance } from "@feeblo/domain/billing/revoca
import { PolarService } from "@feeblo/domain/billing/service";
import { isFeatureEnabled } from "@feeblo/domain/feature-flags";
import { HelpCenterArticleRepository } from "@feeblo/domain/help-center-article/repository";
import { helpCenterArticleViewRetention } from "@feeblo/domain/help-center-article/retention";
import { helpCenterRetention } from "@feeblo/domain/help-center-article/retention";
import { WebhookIntegrationConfig } from "@feeblo/domain/integration/config";
import { DiscordIntegrationConfig } from "@feeblo/domain/integration/discord/config";
import { ExternalResourceServiceLive } from "@feeblo/domain/integration/external-resource/live";
Expand Down Expand Up @@ -238,7 +238,8 @@ export const program = Effect.gen(function* () {

// The help center's read events age out on the same terms, for the same
// reason: the counter beside them is the number a customer reads and is never
// pruned, so the events only need to answer the recent window.
// pruned, so the events only need to answer the recent window. The same pass
// prunes the search buckets, whose key is the reader's text.
//
// Off means nothing writes a read event, so the sweep is not scheduled at
// all — the same rule the audit-log sweep below applies.
Expand All @@ -247,7 +248,7 @@ export const program = Effect.gen(function* () {
HelpCenterArticleRepository.layer
);

yield* helpCenterArticleViewRetention.pipe(
yield* helpCenterRetention.pipe(
Effect.provide(helpCenterTrafficRuntime),
Effect.forkScoped
);
Expand Down
90 changes: 90 additions & 0 deletions apps/web/src/dashboard/features/help-center/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import type {
THelpCenterAnalyticsPayload,
THelpCenterAnalyticsRange,
THelpCenterArticleSearch,
} from "@feeblo/domain/help-center-article/schema";
import * as Atom from "effect/reactivity/Atom";

import { DashboardClient, dashboardSWR } from "~/lib/atom-rpc";

/**
* The client's view of an operation's payload.
*
* Derived from the domain type with the branded id widened to `string`: the
* brand exists so a server route cannot pass one id where another belongs, and
* a React prop is not that boundary. Every other field comes from the
* operation, so a field it gains is a field this atom sends rather than one it
* silently omits.
*/
type ClientPayload<T extends { readonly organizationId: string }> = Readonly<
Omit<T, "organizationId"> & { readonly organizationId: string }
>;

/**
* The editor's single-article read is Effect Atom, not `useQuery`.
*
Expand Down Expand Up @@ -61,3 +79,75 @@ export const updateHelpCenterArticleAtom = DashboardClient.mutation(
export const deleteHelpCenterArticleAtom = DashboardClient.mutation(
"DeleteHelpCenterArticle"
);

/**
* The analytics read's arguments, derived from the operation's payload.
*
* `Codec.Encoded` rather than the payload's decoded type: this is what the
* client actually sends, and the decoded side brands `organizationId`, which a
* React prop is not. Deriving it means a field the operation gains is a field
* this panel sends, instead of one it silently leaves out.
*/
export type HelpCenterAnalyticsAtomArgs =
ClientPayload<THelpCenterAnalyticsPayload>;

/** The window the panel opens on. */
export const DEFAULT_HELP_CENTER_ANALYTICS_RANGE =
"30d" satisfies THelpCenterAnalyticsRange;

/**
* One window of help center traffic, verdicts and searches.
*
* Two nested families rather than one family taking `{ organizationId, range }`.
* `Atom.family` memoizes on its argument with `MutableHashMap`, which compares
* object keys by identity and string keys by value — so a single family given an
* object literal is a new atom on every render unless the caller memoizes it,
* and a caller that forgets is a request per render. Composing one string-keyed
* family per field makes the cache value-based: the same workspace and window
* always produce the same atom, and switching the toggle back is the cached
* answer rather than a refetch.
*
* It shares the article reactivity key because a verdict cast on the portal
* changes what this panel reports: the reader's own write should move the number
* they are looking at.
*/
const analyticsAtoms = Atom.family((organizationId: string) =>
Atom.family((range: THelpCenterAnalyticsRange) =>
DashboardClient.query(
"GetHelpCenterAnalytics",
{ organizationId, range },
{ reactivityKeys: helpCenterArticleReactivityKeys(organizationId) }
).pipe(dashboardSWR("60 seconds"), Atom.setIdleTTL("5 minutes"))
)
);

export const helpCenterAnalyticsAtom = (args: HelpCenterAnalyticsAtomArgs) =>
analyticsAtoms(args.organizationId)(args.range);

/** The same rule as the analytics args: derived from the operation. */
export type HelpCenterSearchAtomArgs = Pick<
ClientPayload<THelpCenterArticleSearch>,
"organizationId" | "query"
>;

/**
* The dashboard's article search.
*
* Nested families for the same reason as the analytics read: the query is a
* string key, so a keystroke is a cache lookup and a deleted character is an
* answer that already exists, with no `useMemo` to forget. It shares the article
* reactivity key: renaming an article should refresh a search that would have
* matched the old title.
*/
const searchAtoms = Atom.family((organizationId: string) =>
Atom.family((query: string) =>
DashboardClient.query(
"SearchHelpCenterArticles",
{ organizationId, query },
{ reactivityKeys: helpCenterArticleReactivityKeys(organizationId) }
).pipe(dashboardSWR("30 seconds"), Atom.setIdleTTL("1 minute"))
)
);

export const helpCenterSearchAtom = (args: HelpCenterSearchAtomArgs) =>
searchAtoms(args.organizationId)(args.query);
Loading
Loading