Dough Git

clove/system

src/routes/admin/mental/+page.svelte

back to src/routes/admin/mental

<!-- src/routes/admin/mental/+page.svelte
     Copyright (c) 2026 Clove Nytrix Doughmination Twilight
     Licensed under the DASL-1.0 Licence.
     See LICENCE.md in the project root for full licence information.
-->

<script lang="ts">
  import IcCheckCircle from "~icons/ic/outline-check-circle";
  import IcGppBad from "~icons/ic/outline-gpp-bad";
  import IcInfo from "~icons/ic/outline-info";
  import IcReportProblem from "~icons/ic/outline-report-problem";
  import IcWarningAmber from "~icons/ic/outline-warning-amber";
  import { onMount } from "svelte";
  import type { MentalState } from "#lib/types.ts";
  import { errorText, type Message as MessageType } from "#lib";
  import { client } from "#lib/api.ts";
  import PageHead from "#lib/components/PageHead.svelte";
  import Message from "#lib/components/Message.svelte";

  const OPTIONS = [
    {
      value: "safe",
      label: "Safe",
      icon: IcCheckCircle,
    },
    {
      value: "unstable",
      label: "Unstable",
      icon: IcReportProblem,
    },
    {
      value: "idealizing",
      label: "Idealizing",
      icon: IcInfo,
    },
    {
      value: "self-harming",
      label: "Self-Harming",
      icon: IcWarningAmber,
    },
    {
      value: "highly at risk",
      label: "Highly At Risk",
      icon: IcGppBad,
    },
  ];

  let current = $state<MentalState | null>(null);
  let level = $state("");
  let notes = $state("");
  let loading = $state(true);
  let saving = $state(false);
  let message = $state<MessageType>(null);

  const currentOption = $derived(OPTIONS.find((o) => o.value === current?.level));

  onMount(async () => {
    try {
      current = await client.getMentalState();
      level = current.level;
      notes = current.notes || "";
    } catch {
      // No state yet; the form still works
    } finally {
      loading = false;
    }
  });

  async function update() {
    saving = true;
    message = null;
    try {
      await client.setMentalState({
        level,
        notes: notes.trim() || undefined,
      });
      current = await client.getMentalState();
      message = {
        type: "success",
        content: "Mental state updated successfully.",
      };
    } catch (err) {
      message = {
        type: "error",
        content: errorText(err, "An unknown error occurred"),
      };
    } finally {
      saving = false;
    }
  }
</script>

<svelte:head>
  <title>Mental health · Doughmination System</title>
</svelte:head>

<div class="page narrow">
  <PageHead
    eyebrow="Admin"
    title="Mental health"
    subtitle="Update system mental state"
    back={{
      href: "/admin/dash",
      label: "Back to dashboard",
    }}
  />

  <Message {message} />

  {#if loading}
    <p class="muted">Loading…</p>
  {:else}
    {#if current && currentOption}
      <div class="mental mental-{current.level.replaceAll(' ', '-')}">
        <currentOption.icon width="32" height="32" />
        <div>
          <p class="mental-label">{currentOption.label}</p>
          {#if current.notes}<p>{current.notes}</p>{/if}
          <small class="muted">Last updated: {new Date(current.updated_at).toLocaleString()}</small>
        </div>
      </div>
    {/if}

    <section class="panel stack">
      <label class="field">
        <span>Mental state level</span>
        <select bind:value={level}>
          <option value="">-- Select mental state --</option>
          {#each OPTIONS as option (option.value)}
            <option value={option.value}>{option.label}</option>
          {/each}
        </select>
      </label>
      <label class="field">
        <span>Notes (optional)</span>
        <textarea class="notes" rows="3" bind:value={notes} placeholder="Add any additional notes about the current mental state..."></textarea>
      </label>
      <div>
        <button type="button" class="primary" disabled={saving || !level} onclick={update}>
          {saving ? "Updating..." : "Update mental state"}
        </button>
      </div>
    </section>
  {/if}
</div>

<style>
  /* Prose, not code: the shared textarea style is monospace */
  .notes {
    min-height: 6rem;
    font-family: inherit;
    font-size: 1rem;
  }
</style>