mirror of
https://github.com/storytold/autocraft.git
synced 2026-11-28 09:09:57 +01:00
Discord channels: always start folded to in-use channels, with an empty state, and fold the full list by server
This commit is contained in:
1 parent
49ccbce859
commit
12be71faf1
2 files changed
+35
-24
No files matched your search
+12
-14
@@ -131,20 +131,18 @@ export function createMockApi(): Api {
|
||||
{ id: "l1", login: "mira-anders", linkedAt: new Date(Date.now() - 3 * 86_400_000).toISOString() },
|
||||
{ id: "l2", login: "tomas-vale", linkedAt: new Date(Date.now() - 40 * 3_600_000).toISOString() },
|
||||
],
|
||||
channels: [
|
||||
{ channelId: "9000000000000004", guildId: "9000000000000001", guildName: "Glasswing Community", name: "general", watched: false, defaultRepo: null, category: null },
|
||||
{ channelId: "9000000000000005", guildId: "9000000000000001", guildName: "Glasswing Community", name: "announcements", watched: false, defaultRepo: null, category: null },
|
||||
{ channelId: "9000000000000006", guildId: "9000000000000001", guildName: "Glasswing Community", name: "rules", watched: false, defaultRepo: null, category: null },
|
||||
{ channelId: "9000000000000002", guildId: "9000000000000001", guildName: "Glasswing Community", name: "dev-chat", watched: true, defaultRepo: "photocraft", category: "Development" },
|
||||
{ channelId: "9000000000000007", guildId: "9000000000000001", guildName: "Glasswing Community", name: "release-notes", watched: false, defaultRepo: null, category: "Development" },
|
||||
{ channelId: "9000000000000003", guildId: "9000000000000001", guildName: "Glasswing Community", name: "support", watched: true, defaultRepo: null, category: "Community" },
|
||||
{ channelId: "9000000000000008", guildId: "9000000000000001", guildName: "Glasswing Community", name: "help-desk", watched: false, defaultRepo: null, category: "Community" },
|
||||
{ channelId: "9100000000000011", guildId: "9100000000000001", guildName: "Open Tools Collective", name: "general", watched: false, defaultRepo: null, category: null },
|
||||
{ channelId: "9100000000000012", guildId: "9100000000000001", guildName: "Open Tools Collective", name: "showcase", watched: false, defaultRepo: null, category: "Community" },
|
||||
{ channelId: "9100000000000013", guildId: "9100000000000001", guildName: "Open Tools Collective", name: "bug-reports", watched: true, defaultRepo: "pdfcraft", category: "Support" },
|
||||
{ channelId: "9100000000000014", guildId: "9100000000000001", guildName: "Open Tools Collective", name: "off-topic", watched: false, defaultRepo: null, category: "Community" },
|
||||
{ channelId: "9100000000000015", guildId: "9100000000000001", guildName: "Open Tools Collective", name: "voice-lounge", watched: false, defaultRepo: null, category: "Community" },
|
||||
],
|
||||
channels: Array.from({ length: 70 }, (_, i) => {
|
||||
const first = i < 35;
|
||||
return {
|
||||
channelId: `${first ? "90" : "91"}0000000000${String(100 + i)}`,
|
||||
guildId: first ? "9000000000000001" : "9100000000000001",
|
||||
guildName: first ? "Glasswing Community" : "Open Tools Collective",
|
||||
name: `channel-${i + 1}`,
|
||||
watched: new URLSearchParams(location.search).get("inuse") === "1" && i === 3,
|
||||
defaultRepo: null,
|
||||
category: i % 10 < 5 ? "Community" : "Development",
|
||||
};
|
||||
}),
|
||||
};
|
||||
const find = (id: string) => {
|
||||
const d = decisions.find((x) => x.id === id);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Fragment, useEffect, useState } from "react";
|
||||
import { Fragment, useEffect, useState, type ReactNode } from "react";
|
||||
import { useApi, useQuery } from "../../api/client";
|
||||
import type { DiscordSetup } from "../../api/types";
|
||||
import type { ViewProps } from "../../registry/types";
|
||||
@@ -25,14 +25,30 @@ const STATE: Record<DiscordSetup["state"], [string, Tone]> = {
|
||||
off: ["Off", "neutral"], connecting: ["Connecting", "warn"], connected: ["Connected", "ok"], reconnecting: ["Reconnecting", "warn"], failed: ["Failed", "bad"],
|
||||
};
|
||||
|
||||
function GuildSection({ guildId, name, count, defaultOpen, children }: { guildId: string; name: string; count: number; defaultOpen: boolean; children: ReactNode }) {
|
||||
const [open, setOpen] = useState(defaultOpen);
|
||||
const id = `discord-guild-${guildId}`;
|
||||
return (
|
||||
<div className="border-b border-line last:border-0">
|
||||
<DisclosureButton open={open} onToggle={setOpen} controls={id} className="bg-ink/[0.03] text-ink">
|
||||
<span className="flex min-w-0 flex-1 flex-wrap items-baseline gap-x-2">
|
||||
{name}
|
||||
<span className="num text-2xs font-normal text-ink/45">{guildId}</span>
|
||||
</span>
|
||||
<span className="num shrink-0 text-xs font-normal text-ink/55">{count} {count === 1 ? "channel" : "channels"}</span>
|
||||
</DisclosureButton>
|
||||
{open && <div id={id}>{children}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DiscordView(_: ViewProps) {
|
||||
const { data, error, refetch } = useQuery("discord-setup", (api) => api.discord.setup());
|
||||
const api = useApi();
|
||||
const write = useWrite();
|
||||
const [showAll, setShowAll] = useRememberedOpen("discord-channels");
|
||||
const inUse = data ? data.channels.filter((c) => c.watched || c.defaultRepo !== null) : [];
|
||||
const allShown = showAll || inUse.length === 0;
|
||||
const shown = data ? (allShown ? data.channels : inUse) : [];
|
||||
const shown = data ? (showAll ? data.channels : inUse) : [];
|
||||
const guilds = [...new Set(shown.map((c) => c.guildId))];
|
||||
return (
|
||||
<Page title="Discord">
|
||||
@@ -51,13 +67,10 @@ export function DiscordView(_: ViewProps) {
|
||||
</Block>
|
||||
<Block className="mt-4" title="Channels" aside={`${data.channels.filter((c) => c.watched).length} watched`} flush>
|
||||
{data.channels.length === 0 && <div className="px-4 py-3 text-sm text-ink/55">The bot has reported no channels.</div>}
|
||||
{data.channels.length > 0 && !showAll && inUse.length === 0 && <div className="px-4 py-3 text-sm text-ink/55">No channel is watched or has a default repo.</div>}
|
||||
<div id="discord-channel-list">
|
||||
{guilds.map((g) => (
|
||||
<div key={g} className="border-b border-line last:border-0">
|
||||
<div className="flex flex-wrap items-baseline gap-x-2 bg-ink/[0.03] px-4 py-2 text-sm font-medium">
|
||||
{data.channels.find((c) => c.guildId === g)?.guildName ?? "Server"}
|
||||
<span className="num text-2xs font-normal text-ink/45">{g}</span>
|
||||
</div>
|
||||
<GuildSection key={`${g}:${showAll}`} guildId={g} name={data.channels.find((c) => c.guildId === g)?.guildName ?? "Server"} count={data.channels.filter((c) => c.guildId === g).length} defaultOpen={!showAll}>
|
||||
<ul>
|
||||
{shown.filter((c) => c.guildId === g).map((c, i, all) => (
|
||||
<Fragment key={c.channelId}>
|
||||
@@ -85,10 +98,10 @@ export function DiscordView(_: ViewProps) {
|
||||
</Fragment>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
</GuildSection>
|
||||
))}
|
||||
</div>
|
||||
{inUse.length > 0 && inUse.length < data.channels.length ? (
|
||||
{inUse.length < data.channels.length ? (
|
||||
<DisclosureButton open={showAll} onToggle={setShowAll} controls="discord-channel-list" className="border-t border-line">
|
||||
{showAll ? "Show fewer" : `Show all channels (${data.channels.length})`}
|
||||
</DisclosureButton>
|
||||
|
||||
Reference in new issue
Block a user