Skip to content
Open
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
133 changes: 132 additions & 1 deletion website/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,18 @@ section {
/* Footer */
.site-footer {
border-top: 1px solid var(--hairline);
padding: 32px 0 40px;
/* The bottom leaves room for the chat bubble (84 px; 64 px on narrow screens), so the last lines are never under it. */
padding: 32px 0 112px;
color: var(--muted);
font-size: 0.95rem;
}

@media (max-width: 480px) {
.site-footer {
padding-bottom: 88px;
}
}

.footer-links {
display: flex;
flex-wrap: wrap;
Expand Down Expand Up @@ -396,3 +403,127 @@ section {
padding: 80px 0;
}
}

/* Chat bubble (components/ChatBubble.tsx), as on showrecorder.app: our own launcher on every page, in the spot and at
the size of Chatwoot's (64 px, 20 px from the right and bottom; 48 px and 16 px on narrow screens), in the site's
button green. It loads the chat only when pressed, then hides for Chatwoot's own. */
.chat-bubble {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 60;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 12px;
}

.chat-bubble-button {
display: grid;
place-items: center;
width: 64px;
height: 64px;
border: 0;
border-radius: 50%;
background: var(--button-bg);
color: var(--button-ink);
box-shadow: var(--shadow);
cursor: pointer;
}

/* Narrow screens: a smaller bubble (still well over the 44 px tap target) that covers less text. */
@media (max-width: 480px) {
.chat-bubble {
right: 16px;
bottom: 16px;
}

.chat-bubble-button {
width: 48px;
height: 48px;
}

.chat-bubble-button svg {
width: 22px;
height: 22px;
}

.chat-bubble-note {
max-width: calc(100vw - 32px);
}
}

.chat-bubble-button[aria-busy="true"] {
cursor: progress;
/* A cue that needs no motion: the spinner only turns when motion is allowed. */
opacity: 0.8;
}

.chat-bubble-button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}

@media (prefers-reduced-motion: no-preference) {
.chat-bubble-button {
transition: transform 150ms ease;
}

.chat-bubble-button:hover {
transform: scale(1.05);
}

.chat-bubble-spinner {
animation: chat-bubble-spin 0.9s linear infinite;
}
}

@keyframes chat-bubble-spin {
to {
transform: rotate(360deg);
}
}

.chat-bubble-note {
display: flex;
align-items: flex-start;
gap: 8px;
max-width: min(320px, calc(100vw - 40px));
padding: 12px 12px 12px 16px;
border: 1px solid var(--hairline);
border-radius: 12px;
background: var(--bg-raised);
color: var(--ink);
font-size: 0.9375rem;
line-height: 1.4;
}

.chat-bubble-note a {
color: var(--link);
text-decoration: underline;
overflow-wrap: anywhere;
}

.chat-bubble-dismiss {
flex: none;
display: grid;
place-items: center;
width: 28px;
height: 28px;
border: 0;
border-radius: 6px;
background: transparent;
color: var(--muted);
cursor: pointer;
}

.chat-bubble-dismiss:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 1px;
}

@media print {
.chat-bubble {
display: none;
}
}
3 changes: 3 additions & 0 deletions website/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata, Viewport } from "next";
import Script from "next/script";
import { ChatBubble } from "@/components/ChatBubble";
import { posthogInitScript } from "@/lib/analytics";
import { OG_IMAGE } from "@/lib/metadata";
import { SITE_DESCRIPTION, SITE_NAME, SITE_URL } from "@/lib/site";
Expand Down Expand Up @@ -47,6 +48,8 @@ export default function RootLayout({ children }: { children: React.ReactNode })
Skip to content
</a>
{children}
{/* Click-to-load support chat on every page; nothing loads from the chat server until it is pressed. */}
<ChatBubble />
{/* Cookieless visit counts; see lib/analytics.ts and the privacy section. */}
<Script id="posthog-init" strategy="afterInteractive">
{posthogInitScript()}
Expand Down
6 changes: 6 additions & 0 deletions website/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export default function Home() {
The beta list is kept on Buttondown. It holds your email address and that you asked about
SoundcheckPlayer. Every email has a one-click unsubscribe.
</p>
<p>
The chat button in the corner loads the support chat only when you press it. Nothing from the chat loads
before that. The chat is Chatwoot, run by the developer at chatwoot.ericdahl.dev. Once opened, it stores
an identifier in your browser (a cookie and local storage) to keep your conversation, and keeps what you
type, and any email address you give, so the developer can reply.
</p>
</div>
</section>
</main>
Expand Down
72 changes: 72 additions & 0 deletions website/components/ChatBubble.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use client";

import { usePathname } from "next/navigation";
import { useState } from "react";
import { bubbleVisible, useChat } from "@/lib/chat";
import { CONTACT_EMAIL } from "@/lib/site";

// The chat launcher in the bottom-right corner of every page (rendered once by app/layout.tsx), as on showrecorder.app.
// It is our own static button, not Chatwoot's: nothing from the chat server loads, and nothing is stored, until it is
// pressed (lib/chat.ts).
// It sits where Chatwoot's launcher goes (64 px, 20 px from the right and bottom; smaller on narrow screens), and once
// the chat has loaded, from here or from "Chat with us" on /support, it hides itself and Chatwoot's launcher takes over.
// No click event goes to PostHog: this site counts only Get the beta and sign-ups, as its privacy section says.
export function ChatBubble() {
const chat = useChat("bubble");
const pathname = usePathname();
const [dismissed, setDismissed] = useState(false);
if (chat.status === "loaded") return null;
// Not on /support, which has its own "Chat with us" button.
if (!bubbleVisible(pathname)) return null;

const loading = chat.status === "loading" && chat.mine;
const failed = chat.status === "failed" && chat.mine && !dismissed;

return (
<div className="chat-bubble">
<div className="chat-bubble-live" aria-live="polite">
{loading && <span className="sr-only">Loading chat…</span>}
{failed && (
<div className="chat-bubble-note">
<p>
The chat didn&apos;t load. Email{" "}
<a href={`mailto:${CONTACT_EMAIL}`}>
{CONTACT_EMAIL}
</a>{" "}
instead.
</p>
<button type="button" className="chat-bubble-dismiss" aria-label="Dismiss" onClick={() => setDismissed(true)}>
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
<path d="M4 4l8 8M12 4l-8 8" stroke="currentColor" strokeWidth="2" strokeLinecap="round" fill="none" />
</svg>
</button>
</div>
)}
</div>
<button
type="button"
className="chat-bubble-button"
aria-label="Chat with SoundcheckPlayer support"
aria-busy={loading}
onClick={() => {
setDismissed(false);
chat.open();
}}
>
{loading ? (
<svg className="chat-bubble-spinner" viewBox="0 0 24 24" width="28" height="28" aria-hidden="true">
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeOpacity="0.3" strokeWidth="2.5" fill="none" />
<path d="M21 12a9 9 0 0 0-9-9" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" fill="none" />
</svg>
) : (
<svg viewBox="0 0 24 24" width="28" height="28" aria-hidden="true">
<path
d="M12 3.5c-5 0-9 3.4-9 7.6 0 2.3 1.2 4.4 3.1 5.8L5.4 20.5l4.2-2.2c.8.2 1.6.3 2.4.3 5 0 9-3.4 9-7.5S17 3.5 12 3.5z"
fill="currentColor"
/>
</svg>
)}
</button>
</div>
);
}
122 changes: 122 additions & 0 deletions website/lib/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { useSyncExternalStore } from "react";

// Click-to-load support chat (Chatwoot, self-hosted at chatwoot.ericdahl.dev), ported from showrecorder.app's
// lib/chat.ts. Shared by the chat bubble on every page (components/ChatBubble.tsx) and the "Chat with us" button on
// /support (components/ChatButton.tsx). Nothing from the chat server loads, and nothing is stored in the browser, until
// the visitor presses one of them: no prefetch, no preconnect, no preload. Chatwoot then keeps a visitor ID in a cookie
// and local storage, which the privacy page says. The website token is the "SoundcheckPlayer Website" inbox's
// (showrecorder.app has its own); it is public by design. The CSP in nginx.conf allows the chat host for script-src,
// frame-src, connect-src and img-src.
//
// One loader for the whole page: the SDK script is injected at most once, whichever control is pressed first. After
// that, either control just opens the chat. Chatwoot shows its own launcher (hideMessageBubble: false) in the same
// spot as our bubble, which hides itself once the chat has loaded, so there is only ever one launcher.
export const CHATWOOT_BASE_URL = "https://chatwoot.ericdahl.dev";
export const CHATWOOT_WEBSITE_TOKEN = "GDx6KmSEEKkVTFPT2eXdqiy6";
export const CHATWOOT_SETTINGS = { hideMessageBubble: false, position: "right", type: "standard" } as const;
// If the widget has not said it is ready by then (blocked, offline, server down), offer email instead.
export const READY_TIMEOUT_MS = 15000;

/** Pages that have their own "Chat with us" button, so the floating bubble would only repeat it. */
const PAGES_WITH_OWN_CHAT_BUTTON = ["/support"];

/** Whether the floating bubble shows on `pathname`; true while the path is not known yet. */
export function bubbleVisible(pathname: string | null): boolean {
if (pathname === null) return true;
return !PAGES_WITH_OWN_CHAT_BUTTON.includes(pathname.replace(/\/+$/, "") || "/");
}

export type ChatSource = "bubble" | "support";
export type ChatStatus = "idle" | "loading" | "loaded" | "failed";
export type ChatState = { status: ChatStatus; source: ChatSource | null };

type ChatwootWindow = Window & {
chatwootSettings?: Record<string, unknown>;
chatwootSDK?: { run: (options: { websiteToken: string; baseUrl: string }) => void };
$chatwoot?: { toggle: (state?: "open" | "close") => void };
};

const IDLE: ChatState = { status: "idle", source: null };
let state: ChatState = IDLE;
const listeners = new Set<() => void>();
let script: HTMLScriptElement | null = null;
let timer: number | undefined;
let listening = false;

function set(next: ChatState) {
state = next;
for (const listener of listeners) listener();
}

function chatwoot(): ChatwootWindow {
return window as ChatwootWindow;
}

function onReady() {
window.clearTimeout(timer);
const waiting = state.status === "loading";
set({ ...state, status: "loaded" });
// Open it for the click that asked. A widget that turns up after the timeout just becomes available.
if (waiting) chatwoot().$chatwoot?.toggle("open");
}

function onFail() {
if (state.status === "loaded") return;
window.clearTimeout(timer);
set({ ...state, status: "failed" });
}

/** Loads the chat on the first call (from a click) and opens it; later calls just open it. */
export function openChat(source: ChatSource): void {
if (state.status === "loaded") {
chatwoot().$chatwoot?.toggle("open");
return;
}
if (state.status === "loading") return;
set({ status: "loading", source });

if (!listening) {
window.addEventListener("chatwoot:ready", onReady);
window.addEventListener("chatwoot:error", onFail);
listening = true;
}
timer = window.setTimeout(onFail, READY_TIMEOUT_MS);
// A try that timed out leaves its script on the page: wait for it again rather than inject a second one.
if (script) return;

const w = chatwoot();
w.chatwootSettings = { ...CHATWOOT_SETTINGS };
script = document.createElement("script");
script.src = `${CHATWOOT_BASE_URL}/packs/js/sdk.js`;
script.async = true;
script.onload = () => {
try {
w.chatwootSDK?.run({ websiteToken: CHATWOOT_WEBSITE_TOKEN, baseUrl: CHATWOOT_BASE_URL });
if (!w.chatwootSDK) onFail();
} catch {
onFail();
}
};
script.onerror = () => {
// Blocked or offline: drop it so a later click can try again.
script?.remove();
script = null;
onFail();
};
document.body.appendChild(script);
}

export function subscribeChat(listener: () => void): () => void {
listeners.add(listener);
return () => listeners.delete(listener);
}

export function chatState(): ChatState {
return state;
}

/** The shared chat state for a component, plus openChat bound to that component. */
export function useChat(source: ChatSource): ChatState & { open: () => void; mine: boolean } {
const current = useSyncExternalStore(subscribeChat, chatState, () => IDLE);
return { ...current, open: () => openChat(source), mine: current.source === source };
}
3 changes: 3 additions & 0 deletions website/lib/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const SITE_URL = "https://soundcheckplayer.app";
export const SITE_DESCRIPTION =
"Play your multitrack Stems from an iPhone, iPad or Mac back into the mixer over USB, for a virtual soundcheck without the band.";

// SoundcheckPlayer's support mailbox: the email fallback when the chat does not load, and the contact on /support.
export const CONTACT_EMAIL = "soundcheckplayer@ericdahl.dev";

// The public TestFlight link. While it is null, "Get the beta" goes to the email sign-up on the page.
// Fill it in once SoundcheckPlayer goes to External testers (issue #80).
export const TESTFLIGHT_URL = null as string | null;
Expand Down
5 changes: 3 additions & 2 deletions website/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ server {
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Frame-Options "DENY" always;
# Next's static export inlines its bootstrap scripts, hence 'unsafe-inline' for scripts. PostHog counts visits
# (lib/analytics.ts); the sign-up form posts to Buttondown.
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://us-assets.i.posthog.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self' https://us.i.posthog.com https://us-assets.i.posthog.com; object-src 'none'; base-uri 'self'; form-action 'self' https://buttondown.com; frame-ancestors 'none'" always;
# (lib/analytics.ts); the sign-up form posts to Buttondown. chatwoot.ericdahl.dev is the support chat
# (lib/chat.ts): it loads only when the visitor presses the chat button, the same as on showrecorder.app.
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://us-assets.i.posthog.com https://chatwoot.ericdahl.dev; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://chatwoot.ericdahl.dev; font-src 'self'; connect-src 'self' https://us.i.posthog.com https://us-assets.i.posthog.com https://chatwoot.ericdahl.dev wss://chatwoot.ericdahl.dev; frame-src https://chatwoot.ericdahl.dev; object-src 'none'; base-uri 'self'; form-action 'self' https://buttondown.com; frame-ancestors 'none'" always;

error_page 404 /404.html;

Expand Down
Loading
Loading