Shared Services, Hooks, and Utilities Feature Context
Source: .agents/references/features/shared-services-hooks-and-utilities.md
Content
Shared Services, Hooks, and Utilities Feature Context
Purpose
Shared services, hooks, and utilities are cross-cutting infrastructure used by most product domains: service helpers, request wrappers, analytics, localization, storage wrappers, Sentry/PostHog, uploads, date/time helpers, route helpers, browser utilities, image sizing, safe rendering, and reusable hooks. Changes here have broad blast radius and require extra care.
User-Facing Workflows
- Most user-facing workflows indirectly rely on shared helpers for data fetching, auth, localization, analytics, uploads, error rendering, URL construction, and storage.
- Shared hooks power pagination, query params, outside click detection, page leave warnings, uploads, unsplash search, chat widgets, and common public page data.
- Utilities can run in browser, server, middleware, or test contexts. Verify runtime before using browser globals or Node/Bun APIs.
Key Entrypoints
- Services:
src/services/agentDiscovery/pageContextService.tssrc/services/helpers/src/services/commonService.jssrc/services/uploadService.jssrc/services/uploadImageService.tssrc/services/videoUploadService.jssrc/services/sessionAnalytics/sessionAnalyticsService.jssrc/services/notificationService.tssrc/services/unsplashService.ts
- Hooks:
src/hooks/useApi.tssrc/hooks/usePagination.tssrc/hooks/useQueryParams.tssrc/hooks/usePresignedImageUpload.tssrc/hooks/useUnsplashSearch.tssrc/hooks/usePageLeaveWarning.tssrc/hooks/useOutsideComponentClickDetect.jssrc/hooks/useRemoveScrollDuringOverlay.tssrc/hooks/useLogEvent.jssrc/hooks/useLocale.jssrc/hooks/useSentryReplayOnPage.js
- Utilities:
src/utility/webMcpPageContext.tssrc/utility/webMcpPublicPageRegistry.tssrc/utility/webMcpRouteFamilyContext.tssrc/utility/analytics.jssrc/utility/analyticsConsts.jssrc/utility/browserUtils.jssrc/utility/config.jssrc/utility/cookieService.tssrc/utility/dateHelper/src/utility/domHelpers.jssrc/utility/getImageUrlWithSizing.jssrc/utility/helpers.tssrc/utility/intl.tssrc/utility/localStorageService.jssrc/utility/localizationHelpers.jssrc/utility/parseJSON.jssrc/utility/routesHelper.tssrc/utility/safeErrorRender.tsxsrc/utility/sentryService.tssrc/utility/sessionStorageService.jssrc/utility/stringHelper.jssrc/utility/urlHelpers.jssrc/utility/validationHelper.ts
Data Flow and Service Boundaries
- Public page-context discovery is a read-only shared surface. The official hosted
MCP endpoint is
https://mcp.nas.com/mcp: discovery and supported onboarding tools are anonymous, while protected tools trigger lazy OAuth throughhttps://auth.nas.com. Use livetools/listfor its catalog. - The English-only MCP installation guide has one canonical route:
/mcp. Keep it inNO_LOCALE_PATHSso untranslated locale-prefixed variants redirect to/mcprather than creating duplicate indexable pages. src/pages/api/mcp.page.tsremains an internal JSON-RPC compatibility resource for registered public resources/templates and reads public page context throughgetPublicPageAgentContext; public docs must not advertise it as the Nas.com MCP endpoint.- Page-context discovery flows through
src/services/agentDiscovery/pageContextService.ts, registered static docs insrc/utility/webMcpPublicPageRegistry.ts, route-family fallbacks insrc/utility/webMcpRouteFamilyContext.ts, and path/URI helpers insrc/utility/webMcpPageContext.ts. - Search indexing and agent discovery are separate contracts. Exact
/get-startedis indexable and an explicit read-only agent suggestion target. Exact/subscribe-planremainsnoindexbut is also an explicit read-only agent suggestion target. Nested variants remain blocked so agent context cannot expose private onboarding, subscription, payment, or checkout state. - Request helper changes can affect all service modules. Inspect callers and run lint/tests broadly when
changing
src/services/helpers/. - Browser storage must go through existing storage utilities; direct storage access violates repo rules and makes SSR/middleware behavior fragile.
- Upload hooks/services often depend on presigned URL signatures, image/video compression, and external APIs. Preserve content type, file size, and error handling.
- Analytics helpers should not expose secrets, tokens, or PII. Preserve event naming and action constants unless the task explicitly changes instrumentation.
- Date/time helpers affect timezone-sensitive UI. Check tests in
src/utility/dateHelper/and adjacent utility tests when changing formatting or timezone logic. - Shared hooks should be runtime-safe and avoid unnecessary effects. If an effect is required for browser subscriptions or DOM APIs, keep cleanup explicit.
- Shared editable Lexical editors treat a click below the final rendered top-level node as an intent to continue writing. Reuse an existing trailing empty paragraph or append exactly one; clicks within rendered content must retain Lexical's native selection behavior.
Conventions and Gotchas
- Prefer TypeScript for new shared files. For existing JavaScript utilities, keep the extension unless a migration is requested.
- Avoid
any, unsafeas, and non-null assertions in shared TypeScript. Fix types at boundaries. - For public discovery changes, keep
public/openapi.json,public/.well-known/agent.json,public/.well-known/public-pages.json,public/sitemap.xml,public/schemamap.xml, Link headers innext.config.js, hosted MCP discovery manifests, and registered MCP/page-context docs aligned. - Public discovery guards must block private/authenticated/API internals and framework/static assets.
Private/member-only examples are covered by focused tests in
src/services/agentDiscovery/pageContextService.test.tsandsrc/pages/api/mcp.page.test.ts. - Do not add thin index re-export files. Put implementation in the main file.
- Do not introduce generic abstractions unless they remove real duplication across current callers.
- Keep helpers pure where possible; side effects should be explicit in service/hook boundaries.
- Shared changes should include focused tests when existing tests exist or when behavior is easy to cover.
- Sentry expected-error suppression should prefer source-level skips for already handled user/provider outcomes, such as checkout availability messages, payment-provider next-action declines, and captcha widget callbacks. Keep global ignore patterns narrow for variants that have no source-owned capture site.
Related Standards
.agents/rules/clean-typescript.mdc.agents/rules/prefer-typescript-files.mdc.agents/rules/no-type-assertion-as.mdc.agents/rules/no-try-catch-service-api.mdc.agents/rules/web-security.mdc.agents/rules/bun-first.mdc.agents/references/coding-standard/useeffect-guidance.md
Useful Graph Queries and Fallback Searches
Use the terms below with search_graph/search_code, then run inbound and outbound trace_path for any
shared symbol being changed. Run these shell searches only as documented fallbacks.
rtk rg "authorizedRequest|publicAxiosRequest|localStorageService|sessionStorageService|showErrorToast|analytics|Sentry|PostHog|presigned|upload" src/services src/hooks src/utility
rtk rg "getPublicPageAgentContext|REGISTERED_PUBLIC_PAGE_CONTEXTS|getPublicRouteFamilyContext|getMcpResourceUri|resources/templates/list|tools/list" src/services/agentDiscovery src/pages/api src/utility public
rtk find src/services/helpers src/hooks src/utility -maxdepth 3 -type f
rtk rg "describe\\(|it\\(|test\\(" src/utility src/hooks src/services
Update Triggers
Update this reference when request helpers, storage wrappers, analytics utilities, upload services, shared hooks, shared editor behavior, localization helpers, date/time utilities, Web MCP resource discovery, or public page-context helpers change.