الويب
التثبيت
npm install @heysada/sanad
شرح الاستخدام أدناه بالإنجليزية، كما يظهر في سجل الحزمة، والشيفرة نفسها في كل لغة.
In-app support for the web (plan §7B, design, task 084). Your Help button opens a contact form. The user writes, sees a screenshot of the page they were on, hides or removes anything, and presses Send. The message becomes a ticket in desk.
Use it
import { configure, open } from "@heysada/sanad";
configure({
appKey: "pk_…", // sanad-support: POST /apps/{id}/keys (publishable)
apiUrl: "https://support.example.com", // where sanad-support serves /sdk/v1
appVersion: "4.2.0",
userId: currentUser?.id, // optional: your own id for the signed-in user
});
helpButton.addEventListener("click", () => open({ language: "ar" }));
Or with a <script> tag: dist/support.iife.js sets window.AppSupport (configure,
open, close).
The ES module (dist/support.js) is for a bundler: it imports the shared web core
(sdk/web/core, a dependency), so two SDKs on one page share one core. The <script> file has the
core inside it.
- List your page’s origin in the app’s
allowed_origins. A browser request from any other origin is refused (403 origin_not_allowed). - CSP: allow
connect-srctoapiUrl. The capture renders the page into an SVGforeignObject, which needsimg-src data: blob:. The sheet needs no'unsafe-inline': its styles are a constructed stylesheet in its shadow root. Understyle-src 'self'the capture’s scratch document logs one refused<style>frommodern-screenshot; the sheet still opens. - Mark what must never leave the device with
data-support-mask(card numbers, balances, personal details). Masked elements, password fields and every iframe are blacked out before the user sees the preview.
Opening from a failure, and the launcher
open({
topic: "payment_failed",
attributes: { order_id: "A-1042", error_code: "card_declined", amount: 12.5 },
message: "My payment didn't go through", // the form starts with it; the user can change it
});
The team sees the topic and attributes with the conversation. They are not sent to the corpus.
They are also listed under the form, so the user sees what goes. Keys are snake_case (at most
40 characters), values are flat (text up to 256 characters, a number, or true/false), and at most
20 are sent. Anything else is left out with a warning.
The user can drag the launcher from showLauncher(). When they let go it snaps to the nearer side
edge at that height, and it stays there until the page reloads (draggable: false keeps it still).
The launcher is never in the screenshot. The web has no shake and no screenshot event, because no
browser tells a page about either, so the launcher and your own button are its ways in. The mobile
SDKs have all three.
What it does, and what it never does
- It never opens itself and never throws into your page.
open()resolves and never rejects. Problems go toconsole.warn. - One still image of this page, taken from the DOM when Support opens, cropped to the viewport,
masked, and shown on the form. There is no screen-share prompt, and it never captures other tabs
or apps. The user can hide parts (burned into the pixels), remove it, or attach it (the
askmode). What the preview shows is exactly what is uploaded. - The image lives in memory only. It is never written to storage. When the service is unreachable, the
message (text only) is queued in
localStorage, at most 5 messages for up to 3 days, and sent when the browser is back online. The form says it will go without the screenshot. - Your conversations (task 108): the user’s conversations with the team, newest first, and each
thread with who wrote what (you, the team, the assistant marked as AI, a notice). The user replies
in the thread; a closed conversation reopens. While a list or a thread is open it hears news
from the live stream, and stops when the user leaves it or closes Support. With
userId, the list also holds the conversations the team started (say, after an error hit the user). - The launcher.
showLauncher({ side: "end", bottom: 20, color })afterconfigure()puts a round button in the page’s corner (endis the right in a left-to-right page, the left in an Arabic one) with the user’s unread count on it;hideLauncher()removes it, andunreadCount()reads the count for a badge of your own. The launcher only opens Support when the user taps it. - Live. While the launcher or a conversation is open, one server-sent stream
(
GET /sdk/v1/events, read with a streaming fetch since it needs the app key) brings replies, the team typing, “Seen”, and the badge. It reconnects by itself withLast-Event-ID. Allowconnect-srctoapiUrl(as for every call). - Images in a conversation, both ways: the user attaches a PNG or a JPEG (re-encoded by the
service, metadata dropped); images come through short-lived links, so allow
img-srcforapiUrl. - Home and help center. The sheet opens on the app’s greeting, its team’s photos and usual reply
time. The Help center tab lists the app’s articles by collection, searches them and reads one;
openArticle(id)deep-links to an article andopen({ conversation: id })to a thread. userToken: the signed-in user’s token from the platform’s identify (sent asX-User-Token). With it, the user’s conversations follow them to every device; without it they stay with this browser. Not yet: until the service runs on the end-user token verifier (chassis 0.38.0), a sent token is refused (401) and the conversations, home and badge stay empty; leaveuserTokenout until this note is gone.- Call me (task 108): the user leaves a number (with the country code), what it’s about and, if they like, a time that suits. It lands in desk’s inbox as a call-back.
userIdis sent asuser_idwith a message, the conversations list and a call-back. Leave it out for anonymous users.- The help assistant (when the app has help articles) opens first. It answers only from your articles and links the ones it used. It says so when it can’t answer, and “Talk to a person” stays under its answers and moves to the form with the conversation attached; the user can untick it.
- The app’s design (ADR 031) is the one source of the look: the form (message, email, Send,
Cancel) is the service’s
sheet_view, drawn with the shared view renderer and the app’s design bundle (GET /sdk/v1/design/bundle, kept by the shared view renderer’sDesignKeeperwith its ETag and asked again every minute while open); the header, tabs, assistant, conversations and call-back take the design’s colours. Send submits the ticket; Cancel closes. Still no inline styles. - Offline, the saved design. The last config (per app and language) and the last bundle read
are kept in
localStorage. Offline, or while the service is down, the sheet draws that form with that design, says it is offline, and queues what is sent. With nothing saved, or a view that can’t send, it says Support can’t be reached: never a form drawn another way. - Only the allowlisted context is sent (app version, build,
web, device class, language), and the form lists it under the message. - The install id (
support.install, random) is used only for rate limits.
Your privacy policy: template
When you contact support in this app, we send your message, and, if you leave them attached, a screenshot of the screen you were on and your conversation with our help assistant. We also send the app version and the device type. You see all of it before you press Send. Support messages are kept for 180 days after they are resolved.
Develop
./check.sh checks, in order:
- the wire types (
src/wire.ts) matchservices/sanad-support/openapi.json(npm run genregenerates them), andtest/wire.test.tschecks every call’s path, method, query and field names against the same file; - biome;
tsc;- vitest (jsdom);
- the build.
The desk panel (frontend/, VITE_SUPPORT_APP_KEY + VITE_SUPPORT_URL) dogfoods it through
open() only.