Skip to content

Build your personal knowledge base with Trilium Notes

client/options/sync: switch to the new card component

Adorian Doranfef5618efaa5
diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.css
index f88b987c7a..8ad1c567e0 100644
--- a/apps/client/src/widgets/react/Card.css
+++ b/apps/client/src/widgets/react/Card.css
@@ -86,6 +86,17 @@
}
}
+.tn-card-option-stacked {
+ flex-direction: column;
+ align-items: stretch;
+
+ /* Given a line of its own, the control uses all of it — including a slider, which otherwise
+ carries a width of its own (200px) that `stretch` cannot override. */
+ > * {
+ width: 100%;
+ }
+}
+
/* On a phone, a control too wide to sit beside the text takes the line below it. A switch is narrow
enough to stay put; a button labelled with a sentence is not, and left the description beside it
reading a word to the line. */
diff --git a/apps/client/src/widgets/react/Card.tsx b/apps/client/src/widgets/react/Card.tsx
index 0ad9fd3337..5fc6dbd65b 100644
--- a/apps/client/src/widgets/react/Card.tsx
+++ b/apps/client/src/widgets/react/Card.tsx
@@ -111,6 +111,11 @@ export interface CardOptionProps extends CardSectionProps {
* child can be bound, which covers the usual case of one toggle or one input per option.
*/
name?: string;
+ /**
+ * Puts the control on the line below the label rather than beside it, for one that needs the
+ * whole width — a URL, a path, anything read as well as typed.
+ */
+ stacked?: boolean;
/** The controls the option is operated with, placed on the trailing edge. */
children?: ComponentChildren;
}
@@ -120,11 +125,13 @@ export interface CardOptionProps extends CardSectionProps {
* trailing one, with the sentence explaining it below the label.
*/
export function CardOption(props: CardOptionProps) {
- const {label, description, name, children, className, ...rest} = props;
+ const {label, description, name, stacked, children, className, ...rest} = props;
const id = useUniqueName(name);
const bound = !!name && isValidElement(children);
- return <CardSection className={clsx("tn-card-option", className)} {...rest}>
+ return <CardSection className={clsx("tn-card-option", className, {
+ "tn-card-option-stacked": stacked
+ })} {...rest}>
<label className="tn-card-option-label" for={bound ? id : undefined}>
{label}
{description && <small className="tn-card-option-description">{description}</small>}
diff --git a/apps/client/src/widgets/type_widgets/options/sync.css b/apps/client/src/widgets/type_widgets/options/sync.css
new file mode 100644
index 0000000000..e94841f0fb
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/options/sync.css
@@ -0,0 +1,5 @@
+/* The width a duration box was given while the page framed its own cards (`.options-section-card` in
+ style.css), which a Card does not carry. */
+.sync-timeout input[type="number"] {
+ width: 7em;
+}
diff --git a/apps/client/src/widgets/type_widgets/options/sync.tsx b/apps/client/src/widgets/type_widgets/options/sync.tsx
index d0b1d93d3e..a885607362 100644
--- a/apps/client/src/widgets/type_widgets/options/sync.tsx
+++ b/apps/client/src/widgets/type_widgets/options/sync.tsx
@@ -1,14 +1,16 @@
+import "./sync.css";
+
import { SyncTestResponse } from "@triliumnext/commons";
import { useEffect, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
import server from "../../../services/server";
import toast from "../../../services/toast";
+import Button from "../../react/Button";
+import { Card, CardOption } from "../../react/Card";
import FormTextBox from "../../react/FormTextBox";
import { useTriliumOption } from "../../react/hooks";
import OptionsPageHeader from "./components/OptionsPageHeader";
-import OptionsRow, { OptionsRowWithButton } from "./components/OptionsRow";
-import OptionsSection from "./components/OptionsSection";
import TimeSelector from "./components/TimeSelector";
export default function SyncOptions() {
@@ -20,6 +22,13 @@ export default function SyncOptions() {
);
}
+/**
+ * Where to sync and how patiently, followed by the one thing on this page that acts rather than sets.
+ *
+ * The test stands on a card of its own, so that what it does is not read as one more setting; it is
+ * still driven from here because it saves what has been typed before it runs, and so needs the values
+ * still sitting in the boxes rather than the ones already stored.
+ */
export function SyncConfiguration() {
const [syncServerHost, setSyncServerHost] = useTriliumOption("syncServerHost");
const [syncProxy, setSyncProxy] = useTriliumOption("syncProxy");
@@ -29,53 +38,83 @@ export function SyncConfiguration() {
useEffect(() => setLocalHost(syncServerHost), [syncServerHost]);
useEffect(() => setLocalProxy(syncProxy), [syncProxy]);
- return (
- <OptionsSection>
- <OptionsRow name="sync-server-host" label={t("sync_2.server_address")} description={t("sync_2.server_address_description")} stacked>
- <FormTextBox
- placeholder="https://<host>:<port>"
- currentValue={localHost}
- onChange={setLocalHost}
- onBlur={setSyncServerHost}
- />
- </OptionsRow>
+ async function testConnection() {
+ await Promise.all([
+ setSyncServerHost(localHost),
+ setSyncProxy(localProxy)
+ ]);
+ const result = await server.post<SyncTestResponse>("sync/test");
+
+ if (result.success && result.message) {
+ toast.showMessage(result.message);
+ } else {
+ toast.showError(t("sync_2.handshake_failed", { message: result.message }));
+ }
+ }
- <OptionsRow name="sync-proxy" label={t("sync_2.proxy_label")} description={t("sync_2.proxy_description")} stacked>
- <FormTextBox
- placeholder="https://<host>:<port>"
- currentValue={localProxy}
- onChange={setLocalProxy}
- onBlur={setSyncProxy}
- />
- </OptionsRow>
+ return (
+ <>
+ <div className="options-section">
+ <Card heading={t("sync_2.config_title")}>
+ <CardOption
+ name="sync-server-host"
+ label={t("sync_2.server_address")}
+ description={t("sync_2.server_address_description")}
+ stacked
+ >
+ <FormTextBox
+ placeholder="https://<host>:<port>"
+ currentValue={localHost}
+ onChange={setLocalHost}
+ onBlur={setSyncServerHost}
+ />
+ </CardOption>
- <OptionsRow name="sync-server-timeout" label={t("sync_2.timeout")} description={t("sync_2.timeout_description")}>
- <TimeSelector
- name="sync-server-timeout"
- optionValueId="syncServerTimeout"
- optionTimeScaleId="syncServerTimeoutTimeScale"
- minimumSeconds={1}
- />
- </OptionsRow>
+ <CardOption
+ name="sync-proxy"
+ label={t("sync_2.proxy_label")}
+ description={t("sync_2.proxy_description")}
+ stacked
+ >
+ <FormTextBox
+ placeholder="https://<host>:<port>"
+ currentValue={localProxy}
+ onChange={setLocalProxy}
+ onBlur={setSyncProxy}
+ />
+ </CardOption>
- <OptionsRowWithButton
- label={t("sync_2.test_title")}
- description={t("sync_2.test_description")}
- buttonText={t("sync_2.test_button")}
- onClick={async () => {
- await Promise.all([
- setSyncServerHost(localHost),
- setSyncProxy(localProxy)
- ]);
- const result = await server.post<SyncTestResponse>("sync/test");
+ <CardOption
+ className="sync-timeout"
+ name="sync-server-timeout"
+ label={t("sync_2.timeout")}
+ description={t("sync_2.timeout_description")}
+ >
+ <TimeSelector
+ name="sync-server-timeout"
+ optionValueId="syncServerTimeout"
+ optionTimeScaleId="syncServerTimeoutTimeScale"
+ minimumSeconds={1}
+ />
+ </CardOption>
+ </Card>
+ </div>
- if (result.success && result.message) {
- toast.showMessage(result.message);
- } else {
- toast.showError(t("sync_2.handshake_failed", { message: result.message }));
- }
- }}
- />
- </OptionsSection>
+ <div className="options-section">
+ <Card>
+ <CardOption
+ label={t("sync_2.test_title")}
+ description={t("sync_2.test_description")}
+ >
+ <Button
+ name="test-sync-button"
+ text={t("sync_2.test_button")}
+ size="micro"
+ onClick={() => void testConnection()}
+ />
+ </CardOption>
+ </Card>
+ </div>
+ </>
);
}