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
250 changes: 231 additions & 19 deletions src/api/schema.d.ts

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export type ChannelContentSort = Schemas["ChannelContentSort"];
export type ContentSort = Schemas["ContentSort"];
export type ConnectionFilter = Schemas["ConnectionFilter"];
export type UserTier = Schemas["UserTier"];
export type GroupSort = Schemas["GroupSort"];
export type Metadata = Schemas["Metadata"];
export type MetadataInput = Schemas["MetadataInput"];

export type PresignedFile = Schemas["PresignedFile"];
export type PaginationMeta = Schemas["PaginationMeta"];
Expand Down
15 changes: 13 additions & 2 deletions src/commands/add.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Box, Text } from "ink";
import { client, getData } from "../api/client";
import type { Metadata } from "../api/types";
import { readStdin } from "../lib/args";
import { Spinner } from "../components/Spinner";
import { useCommand } from "../hooks/use-command";
Expand All @@ -13,6 +14,8 @@ interface Props {
originalSourceUrl?: string;
originalSourceTitle?: string;
insertAt?: number;
metadata?: Metadata;
connectionMetadata?: Metadata;
}

export function AddCommand({
Expand All @@ -24,6 +27,8 @@ export function AddCommand({
originalSourceUrl,
originalSourceTitle,
insertAt,
metadata,
connectionMetadata,
}: Props) {
const { data, error, loading } = useCommand(async () => {
const resolvedValue = valueProp ?? (await readStdin());
Expand All @@ -38,13 +43,19 @@ export function AddCommand({
client.POST("/v3/blocks", {
body: {
value: resolvedValue,
channel_ids: [ch.id],
channels: [
{
id: ch.id,
position: insertAt,
metadata: connectionMetadata,
},
],
title,
description,
alt_text: altText,
original_source_url: originalSourceUrl,
original_source_title: originalSourceTitle,
insert_at: insertAt,
metadata,
},
}),
);
Expand Down
6 changes: 4 additions & 2 deletions src/commands/block.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, Text } from "ink";
import { client, getData } from "../api/client";
import type { ConnectionSort } from "../api/types";
import type { ConnectionSort, MetadataInput } from "../api/types";
import { BlockContent } from "../components/BlockContent";
import { Spinner } from "../components/Spinner";
import { useCommand } from "../hooks/use-command";
Expand Down Expand Up @@ -28,18 +28,20 @@ export function BlockUpdateCommand({
description,
content,
altText,
metadata,
}: {
id: number;
title?: string;
description?: string;
content?: string;
altText?: string;
metadata?: MetadataInput;
}) {
const { data, error, loading } = useCommand(() =>
getData(
client.PUT("/v3/blocks/{id}", {
params: { path: { id } },
body: { title, description, content, alt_text: altText },
body: { title, description, content, alt_text: altText, metadata },
}),
),
);
Expand Down
19 changes: 16 additions & 3 deletions src/commands/channel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Box, Text, useApp } from "ink";
import { client, getData } from "../api/client";
import type { ChannelContentSort, Visibility } from "../api/types";
import type {
ChannelContentSort,
Metadata,
MetadataInput,
Visibility,
} from "../api/types";
import { BlockItem } from "../components/BlockItem";
import { ChannelBlockViewer } from "../components/ChannelBlockViewer";
import {
Expand All @@ -11,6 +16,7 @@ import { Spinner } from "../components/Spinner";
import { useCommand } from "../hooks/use-command";
import { useStackNavigator } from "../hooks/useStackNavigator";
import { plural, timeAgo } from "../lib/format";
import { formatMetadata } from "../lib/metadata";
import { clearTerminalViewport } from "../lib/terminalViewport";
import { visibilityLabel } from "../lib/theme";

Expand Down Expand Up @@ -147,6 +153,9 @@ function StaticChannelView({
{plural(channel.counts.contents, "block")}
</Text>
<Text dimColor>Updated {timeAgo(channel.updated_at)}</Text>
{channel.metadata && (
<Text dimColor>Metadata {formatMetadata(channel.metadata)}</Text>
)}
</Box>

<Box flexDirection="column">
Expand Down Expand Up @@ -228,16 +237,18 @@ export function ChannelCreateCommand({
visibility,
description,
groupId,
metadata,
}: {
title: string;
visibility?: Visibility;
description?: string;
groupId?: number;
metadata?: Metadata;
}) {
const { data, error, loading } = useCommand(() =>
getData(
client.POST("/v3/channels", {
body: { title, visibility, description, group_id: groupId },
body: { title, visibility, description, group_id: groupId, metadata },
}),
),
);
Expand All @@ -264,17 +275,19 @@ export function ChannelUpdateCommand({
title,
visibility,
description,
metadata,
}: {
slug: string;
title?: string;
visibility?: Visibility;
description?: string;
metadata?: MetadataInput;
}) {
const { data, error, loading } = useCommand(() =>
getData(
client.PUT("/v3/channels/{id}", {
params: { path: { id: slug } },
body: { title, visibility, description },
body: { title, visibility, description, metadata },
}),
),
);
Expand Down
7 changes: 4 additions & 3 deletions src/commands/connect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, Text } from "ink";
import { client, getData } from "../api/client";
import type { ConnectableType } from "../api/types";
import type { ConnectableType, Metadata } from "../api/types";
import { Spinner } from "../components/Spinner";
import { useCommand } from "../hooks/use-command";

Expand All @@ -9,13 +9,15 @@ interface Props {
channel: string;
connectableType?: ConnectableType;
position?: number;
metadata?: Metadata;
}

export function ConnectCommand({
blockId,
channel,
connectableType = "Block",
position,
metadata,
}: Props) {
const { data, error, loading } = useCommand(async () => {
const ch = await getData(
Expand All @@ -28,8 +30,7 @@ export function ConnectCommand({
body: {
connectable_id: blockId,
connectable_type: connectableType,
channel_ids: [ch.id],
position,
channels: [{ id: ch.id, position, metadata }],
},
});
return { channel: ch };
Expand Down
39 changes: 38 additions & 1 deletion src/commands/connection.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Box, Text } from "ink";
import { client, getData } from "../api/client";
import type { ConnectionFilter, ConnectionSort, Movement } from "../api/types";
import type {
ConnectionFilter,
ConnectionSort,
MetadataInput,
Movement,
} from "../api/types";
import { Spinner } from "../components/Spinner";
import { useCommand } from "../hooks/use-command";
import { plural } from "../lib/format";
import { formatMetadata } from "../lib/metadata";
import { channelColor, INDICATORS } from "../lib/theme";

export function ConnectionGetCommand({ id }: { id: number }) {
Expand All @@ -29,6 +35,37 @@ export function ConnectionGetCommand({ id }: { id: number }) {
{data.connected_by && (
<Text dimColor>Connected by {data.connected_by.name}</Text>
)}
{data.metadata && (
<Text dimColor>Metadata {formatMetadata(data.metadata)}</Text>
)}
</Box>
);
}

export function ConnectionUpdateCommand({
id,
metadata,
}: {
id: number;
metadata: MetadataInput;
}) {
const { data, error, loading } = useCommand(() =>
getData(
client.PUT("/v3/connections/{id}", {
params: { path: { id } },
body: { metadata },
}),
),
);

if (loading) return <Spinner label="Updating connection" />;
if (error) return <Text color="red">✕ {error}</Text>;
if (!data) return null;

return (
<Box>
<Text color="green">✓ </Text>
<Text>Updated connection {data.id}</Text>
</Box>
);
}
Expand Down
41 changes: 41 additions & 0 deletions src/commands/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ContentSort,
ContentTypeFilter,
FollowableType,
GroupSort,
} from "../api/types";
import { BlockItem } from "../components/BlockItem";
import { Spinner } from "../components/Spinner";
Expand Down Expand Up @@ -172,3 +173,43 @@ export function UserFollowingCommand({
</Box>
);
}

export function UserGroupsCommand({
slug,
page = 1,
per,
sort,
}: {
slug: string;
page?: number;
per?: number;
sort?: GroupSort;
}) {
const { data, error, loading } = useCommand(() =>
getData(
client.GET("/v3/users/{id}/groups", {
params: { path: { id: slug }, query: { page, per, sort } },
}),
),
);

if (loading) return <Spinner label="Loading groups" />;
if (error) return <Text color="red">✕ {error}</Text>;
if (!data) return null;

if (data.data.length === 0) return <Text dimColor>No groups</Text>;

return (
<Box flexDirection="column">
{data.data.map((group) => (
<Text key={group.id}>
{group.name} <Text dimColor>@{group.slug}</Text>
</Text>
))}
<Text dimColor>
{"\n"}Page {data.meta.current_page}/{data.meta.total_pages} ·{" "}
{plural(data.meta.total_count, "group")}
</Text>
</Box>
);
}
2 changes: 2 additions & 0 deletions src/components/BlockContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from "react";
import { Box, Text } from "ink";
import type { Block } from "../api/types";
import { formatFileSize, timeAgo } from "../lib/format";
import { formatMetadata } from "../lib/metadata";
import { blockTextColor } from "../lib/theme";
import { TerminalImage } from "./TerminalImage";

Expand Down Expand Up @@ -74,6 +75,7 @@ export function BlockContent({
value: `${block.updated_at} (${timeAgo(block.updated_at)})`,
},
{ label: "By", value: block.user.name },
{ label: "Metadata", value: formatMetadata(block.metadata) },
...(previewImage
? [
{ label: "Image", value: previewImage.filename },
Expand Down
54 changes: 54 additions & 0 deletions src/lib/metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from "node:test";
import assert from "node:assert/strict";
import { metadataInputFlag, parseMetadata } from "./metadata";

test("parseMetadata parses key=value scalars", () => {
assert.deepEqual(parseMetadata("status=reviewed,score=0.95,featured=true"), {
status: "reviewed",
score: 0.95,
featured: true,
});
});

test("parseMetadata parses JSON object values", () => {
assert.deepEqual(
parseMetadata('{"status":"reviewed","score":1,"featured":false}'),
{
status: "reviewed",
score: 1,
featured: false,
},
);
});

test("metadataInputFlag allows null values for merge removal", () => {
assert.deepEqual(metadataInputFlag({ metadata: "status=null" }), {
status: null,
});
});

test("parseMetadata rejects nested values", () => {
assert.throws(
() => parseMetadata('{"tags":["nested"]}'),
/Invalid metadata value/,
);
});

test("parseMetadata rejects invalid keys", () => {
assert.throws(() => parseMetadata("bad-key=value"), /Invalid metadata key/);
});

test("parseMetadata preserves strings that don't round-trip as numbers", () => {
assert.deepEqual(parseMetadata("sku=00123,zip=05401,decimal=1.10"), {
sku: "00123",
zip: "05401",
decimal: "1.10",
});
});

test("parseMetadata supports backslash-escaped commas in values", () => {
assert.deepEqual(parseMetadata("note=hello\\, world,status=ok"), {
note: "hello, world",
status: "ok",
});
});
Loading
Loading