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
5 changes: 5 additions & 0 deletions .changeset/sticky-banner-shadcn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
freeweight: patch
---

Restore the full-width sticky demo banner and replace native browser controls (confirm/prompt dialogs, details/summary) with shadcn-svelte components, including a new confirm-dialog UI component.
17 changes: 16 additions & 1 deletion src/lib/components/app/demo-banner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@
document.removeEventListener('visibilitychange', update);
};
});

let el: HTMLDivElement;
$effect(() => {
if (!el) return;
const set = () =>
document.documentElement.style.setProperty('--demo-banner-h', `${el.offsetHeight}px`);
set();
const ro = new ResizeObserver(set);
ro.observe(el);
return () => {
ro.disconnect();
document.documentElement.style.removeProperty('--demo-banner-h');
};
});
</script>

<div
class="absolute top-14 left-1/2 z-50 flex -translate-x-1/2 flex-wrap items-center justify-center gap-x-3 gap-y-1 rounded-full bg-primary/95 px-4 py-1.5 text-center text-xs text-primary-foreground shadow-lg backdrop-blur sm:top-2"
bind:this={el}
class="sticky top-0 z-50 flex w-full flex-wrap items-center justify-center gap-x-3 gap-y-1 bg-primary px-4 py-1.5 text-center text-xs text-primary-foreground"
>
<span>
Private demo · {remaining > 0 ? 'Session ends in' : 'Session ended'}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/app/page-header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<svelte:head><title>{title} · {appName}</title></svelte:head>

<header
class="sticky top-0 z-10 flex min-h-14 shrink-0 flex-wrap items-center gap-2 border-b bg-background/80 px-4 py-1.5 backdrop-blur"
class="sticky top-[var(--demo-banner-h,0px)] z-10 flex min-h-14 shrink-0 flex-wrap items-center gap-2 border-b bg-background/80 px-4 py-1.5 backdrop-blur"
>
<Sidebar.Trigger class="-ml-1" />
<Separator orientation="vertical" class="mr-1 data-[orientation=vertical]:h-4" />
Expand Down
149 changes: 149 additions & 0 deletions src/lib/components/ui/confirm-dialog/confirm-dialog.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<script lang="ts" module>
class ConfirmDialogState {
open = $state(false);
inputText = $state('');
options = $state<ConfirmDialogOptions | null>(null);
loading = $state(false);

constructor() {
this.confirm = this.confirm.bind(this);
this.cancel = this.cancel.bind(this);
}

newConfirmation(options: ConfirmDialogOptions) {
this.reset();
this.options = options;
this.inputText = options.input?.defaultValue ?? '';
this.open = true;
}

replaceConfirmation(options: ConfirmDialogOptions) {
this.options = options;
this.inputText = options.input?.defaultValue ?? '';
this.loading = false;
this.open = true;
}

reset() {
this.open = false;
this.inputText = '';
this.options = null;
}

confirm() {
if (this.loading) return;
if (this.options?.input?.confirmationText) {
if (this.inputText !== this.options.input.confirmationText) {
return;
}
}

const options = this.options;
this.loading = true;
Promise.resolve(options?.onConfirm(this.inputText))
.then(() => {
// onConfirm may have opened a follow-up dialog; only close if still ours
if (this.options === options) this.open = false;
})
.finally(() => {
this.loading = false;
});
}

cancel() {
this.options?.onCancel?.();
this.open = false;
}
}

const dialogState = new ConfirmDialogState();

export type ConfirmDialogOptions = {
title: string;
description?: string;
input?: {
label?: string;
placeholder?: string;
defaultValue?: string;
confirmationText?: string;
};
confirm?: {
text?: string;
};
cancel?: {
text?: string;
};
onConfirm: (value: string) => Promise<unknown> | unknown;
onCancel?: () => void;
};

export function confirmDialog(options: ConfirmDialogOptions) {
dialogState.newConfirmation(options);
}

/** Swap the open dialog's contents in place — used for chained multi-step questions. */
export function replaceConfirmDialog(options: ConfirmDialogOptions) {
dialogState.replaceConfirmation(options);
}
</script>

<script lang="ts">
import * as AlertDialog from '#lib/components/ui/alert-dialog/index.js';
import { Input } from '#lib/components/ui/input/index.js';
import { Label } from '#lib/components/ui/label/index.js';
import { Spinner } from '#lib/components/ui/spinner/index.js';
</script>

<AlertDialog.Root bind:open={dialogState.open}>
<AlertDialog.Content>
<form
method="POST"
onsubmit={(e) => {
e.preventDefault();
dialogState.confirm();
}}
class="flex flex-col gap-4"
>
<AlertDialog.Header>
<AlertDialog.Title>
{dialogState.options?.title}
</AlertDialog.Title>
{#if dialogState.options?.description}
<AlertDialog.Description class="wrap-anywhere">
{dialogState.options.description}
</AlertDialog.Description>
{/if}
</AlertDialog.Header>
{#if dialogState.options?.input}
<div class="flex flex-col gap-1.5">
{#if dialogState.options.input.label}
<Label>{dialogState.options.input.label}</Label>
{/if}
<Input
bind:value={dialogState.inputText}
placeholder={dialogState.options.input.placeholder ??
(dialogState.options.input.confirmationText
? `Enter "${dialogState.options.input.confirmationText}" to confirm.`
: '')}
onkeydown={(e) => {
if (e.key === 'Enter') {
// for some reason without this the form will submit and the dialog will close immediately
e.preventDefault();
dialogState.confirm();
}
}}
/>
</div>
{/if}
<AlertDialog.Footer>
<AlertDialog.Cancel type="button" onclick={dialogState.cancel}>
{dialogState.options?.cancel?.text ?? 'Cancel'}
</AlertDialog.Cancel>
<AlertDialog.Action type="submit" disabled={dialogState.loading}>
{#if dialogState.loading}<Spinner />{/if}
{dialogState.options?.confirm?.text ?? 'Confirm'}
</AlertDialog.Action>
</AlertDialog.Footer>
</form>
</AlertDialog.Content>
</AlertDialog.Root>
7 changes: 7 additions & 0 deletions src/lib/components/ui/confirm-dialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ConfirmDialog, {
confirmDialog,
replaceConfirmDialog,
type ConfirmDialogOptions
} from './confirm-dialog.svelte';

export { ConfirmDialog, confirmDialog, replaceConfirmDialog, type ConfirmDialogOptions };
4 changes: 3 additions & 1 deletion src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { setContext } from 'svelte';
import { contrastText } from '#lib/format.ts';
import type { LayoutProps } from './$types';
import { ConfirmDialog } from '#lib/components/ui/confirm-dialog/index.js';

let { children, data }: LayoutProps = $props();
// White-label brand for PageHeader's <title> and any brand-aware component.
Expand Down Expand Up @@ -37,11 +38,12 @@
deployUrl="https://github.com/maxffarrell/freeweight#quick-start"
/>
{/if}
<main class="min-w-0 flex-1 {data.demo && data.demoExpiresAt ? 'max-sm:pt-10' : ''}">
<main class="min-w-0 flex-1">
{@render children()}
</main>
</Sidebar.Inset>
</Sidebar.Provider>
</div>
<CommandPalette bind:open={searchOpen} />
<OnboardingTour bind:open={tour.open} orgName={data.org.name} />
<ConfirmDialog />
126 changes: 93 additions & 33 deletions src/routes/(app)/billing/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import Gift from '@lucide/svelte/icons/gift';
import Trash2 from '@lucide/svelte/icons/trash-2';
import { toCsv } from '#lib/csv.ts';
import { confirmDialog, replaceConfirmDialog } from '#lib/components/ui/confirm-dialog/index.js';

const currency = await getCurrency();
const memberList = await getMembers();
Expand Down Expand Up @@ -223,8 +224,15 @@
}

let remindingAll = $state(false);
async function emailAllDebtors() {
if (!confirm(`Email a payment request to all ${aging.debtors} members owing?`)) return;
function emailAllDebtors() {
confirmDialog({
title: `Email a payment request to all ${aging.debtors} members owing?`,
confirm: { text: 'Send' },
onConfirm: () => emailAllDebtorsNow()
});
}

async function emailAllDebtorsNow() {
remindingAll = true;
try {
const r = await requestAllPayments();
Expand All @@ -235,8 +243,16 @@
remindingAll = false;
}

async function refund(paymentId: string, amount: number) {
if (!confirm(`Refund ${formatMoney(amount, currency)}? The charge it covered reopens.`)) return;
function refund(paymentId: string, amount: number) {
confirmDialog({
title: `Refund ${formatMoney(amount, currency)}?`,
description: 'The charge it covered reopens.',
confirm: { text: 'Refund' },
onConfirm: () => refundNow(paymentId)
});
}

async function refundNow(paymentId: string) {
try {
await refundPayment(paymentId);
payments = await getPayments();
Expand Down Expand Up @@ -310,10 +326,17 @@
toast.error(errMsg(err, 'Could not update'));
}
}
async function deleteRecurring(r: { rec: { id: string; description: string } }) {
if (!confirm(`Delete the recurring "${r.rec.description}"? Past charges stay.`)) return;
function deleteRecurring(r: { rec: { id: string; description: string } }) {
confirmDialog({
title: `Delete the recurring "${r.rec.description}"?`,
description: 'Past charges stay.',
onConfirm: () => deleteRecurringNow(r.rec.id)
});
}

async function deleteRecurringNow(id: string) {
try {
await removeRecurring(r.rec.id);
await removeRecurring(id);
recurring = await getRecurring();
} catch (err) {
toast.error(errMsg(err, 'Could not delete'));
Expand Down Expand Up @@ -436,9 +459,16 @@

let selectedCharges = $state<string[]>([]);
let voidingMany = $state(false);
async function voidSelected() {
function voidSelected() {
if (!selectedCharges.length) return;
if (!confirm(`Void ${selectedCharges.length} open charge(s)?`)) return;
confirmDialog({
title: `Void ${selectedCharges.length} open charge(s)?`,
confirm: { text: 'Void' },
onConfirm: () => voidSelectedNow()
});
}

async function voidSelectedNow() {
voidingMany = true;
try {
for (const id of selectedCharges) await updateCharge({ id, status: 'void' });
Expand All @@ -460,8 +490,16 @@
toast.error(errMsg(err, 'Could not void'));
}
}
async function writeOffCharge(id: string) {
if (!confirm("Write this charge off as bad debt? It leaves the member's open balance.")) return;
function writeOffCharge(id: string) {
confirmDialog({
title: 'Write this charge off as bad debt?',
description: "It leaves the member's open balance.",
confirm: { text: 'Write off' },
onConfirm: () => writeOffNow(id)
});
}

async function writeOffNow(id: string) {
try {
await updateCharge({ id, status: 'written_off' });
await refreshCharges();
Expand Down Expand Up @@ -526,29 +564,51 @@
lowStockAt: number | null;
cost: number | null;
}) {
const raw = prompt(
`${item.name} — units on hand (blank = unlimited)`,
String(item.stock ?? '')
);
if (raw === null) return;
const n = raw.trim() === '' ? null : Math.max(0, Math.floor(Number(raw)));
if (n !== null && !Number.isFinite(n)) return toast.error('Not a number');
const rawLow = prompt(
`${item.name} — alert staff when stock drops to (blank = never)`,
String(item.lowStockAt ?? '')
);
if (rawLow === null) return;
const low = rawLow.trim() === '' ? null : Math.max(0, Math.floor(Number(rawLow)));
if (low !== null && !Number.isFinite(low)) return toast.error('Not a number');
const rawCost = prompt(
`${item.name} — unit cost in cents (blank = untracked)`,
String(item.cost ?? '')
);
if (rawCost === null) return;
const cost = rawCost.trim() === '' ? null : Math.max(0, Math.floor(Number(rawCost)));
if (cost !== null && !Number.isFinite(cost)) return toast.error('Not a number');
const num = (v: string) => (v.trim() === '' ? null : Math.max(0, Math.floor(Number(v))));
confirmDialog({
title: `${item.name} — units on hand`,
input: { label: 'Units (blank = unlimited)', defaultValue: String(item.stock ?? '') },
confirm: { text: 'Next' },
onConfirm: (raw) => {
const n = num(raw);
if (n !== null && !Number.isFinite(n)) return toast.error('Not a number');
replaceConfirmDialog({
title: `${item.name} — low-stock alert`,
input: {
label: 'Alert staff when stock drops to (blank = never)',
defaultValue: String(item.lowStockAt ?? '')
},
confirm: { text: 'Next' },
onConfirm: (rawLow) => {
const low = num(rawLow);
if (low !== null && !Number.isFinite(low)) return toast.error('Not a number');
replaceConfirmDialog({
title: `${item.name} — unit cost`,
input: {
label: 'Unit cost in cents (blank = untracked)',
defaultValue: String(item.cost ?? '')
},
confirm: { text: 'Save' },
onConfirm: (rawCost) => {
const cost = num(rawCost);
if (cost !== null && !Number.isFinite(cost)) return toast.error('Not a number');
return saveShopStock(item.id, n, low, cost);
}
});
}
});
}
});
}

async function saveShopStock(
id: string,
n: number | null,
low: number | null,
cost: number | null
) {
try {
await updateShopItem({ id: item.id, stock: n, lowStockAt: low, cost });
await updateShopItem({ id, stock: n, lowStockAt: low, cost });
shopItems = await getShopItems();
} catch (err) {
toast.error(errMsg(err, 'Could not update stock'));
Expand Down
Loading
Loading