@gmickel/gno 0.33.3 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ import type { BrowseTreeNode } from "../../browse-tree";
2
+
3
+ export interface BrowseDocument {
4
+ docid: string;
5
+ uri: string;
6
+ title: string | null;
7
+ collection: string;
8
+ relPath: string;
9
+ sourceExt: string;
10
+ }
11
+
12
+ export interface DocsResponse {
13
+ documents: BrowseDocument[];
14
+ total: number;
15
+ limit: number;
16
+ offset: number;
17
+ pathPrefix?: string;
18
+ directChildrenOnly?: boolean;
19
+ availableDateFields: string[];
20
+ sortField: string;
21
+ sortOrder: "asc" | "desc";
22
+ }
23
+
24
+ export interface BrowseTreeResponse {
25
+ collections: BrowseTreeNode[];
26
+ totalCollections: number;
27
+ totalDocuments: number;
28
+ }
29
+
30
+ export function parseBrowseLocation(search: string): {
31
+ collection: string;
32
+ path: string;
33
+ } {
34
+ const params = new URLSearchParams(search);
35
+ return {
36
+ collection: params.get("collection") ?? "",
37
+ path: (params.get("path") ?? "")
38
+ .replaceAll("\\", "/")
39
+ .replace(/^\/+|\/+$/g, ""),
40
+ };
41
+ }
42
+
43
+ export function buildBrowseLocation(
44
+ collection?: string,
45
+ path?: string
46
+ ): string {
47
+ const normalizedCollection = collection?.trim() ?? "";
48
+ const normalizedPath = (path ?? "")
49
+ .replaceAll("\\", "/")
50
+ .replace(/^\/+|\/+$/g, "");
51
+
52
+ if (!normalizedCollection) {
53
+ return "/browse";
54
+ }
55
+
56
+ const params = new URLSearchParams({
57
+ collection: normalizedCollection,
58
+ });
59
+ if (normalizedPath) {
60
+ params.set("path", normalizedPath);
61
+ }
62
+ return `/browse?${params.toString()}`;
63
+ }
64
+
65
+ export function buildBrowseCrumbs(collection: string, path: string) {
66
+ const crumbs = [
67
+ {
68
+ label: collection,
69
+ location: buildBrowseLocation(collection),
70
+ },
71
+ ];
72
+
73
+ if (!path) {
74
+ return crumbs;
75
+ }
76
+
77
+ const parts = path.split("/").filter(Boolean);
78
+ let currentPath = "";
79
+ for (const part of parts) {
80
+ currentPath = currentPath ? `${currentPath}/${part}` : part;
81
+ crumbs.push({
82
+ label: part,
83
+ location: buildBrowseLocation(collection, currentPath),
84
+ });
85
+ }
86
+ return crumbs;
87
+ }
88
+
89
+ export function formatDateFieldLabel(field: string) {
90
+ return field
91
+ .split("_")
92
+ .filter((token) => token.length > 0)
93
+ .map((token) => token.charAt(0).toUpperCase() + token.slice(1))
94
+ .join(" ");
95
+ }
96
+
97
+ export function getExtBadgeVariant(ext: string) {
98
+ switch (ext.toLowerCase()) {
99
+ case ".md":
100
+ case ".markdown":
101
+ return "default";
102
+ case ".pdf":
103
+ return "destructive";
104
+ case ".docx":
105
+ case ".doc":
106
+ return "secondary";
107
+ default:
108
+ return "outline";
109
+ }
110
+ }
@@ -2,6 +2,7 @@ export interface WorkspaceTab {
2
2
  id: string;
3
3
  label: string;
4
4
  location: string;
5
+ browseState?: WorkspaceTabBrowseState;
5
6
  }
6
7
 
7
8
  export interface WorkspaceState {
@@ -9,6 +10,10 @@ export interface WorkspaceState {
9
10
  activeTabId: string;
10
11
  }
11
12
 
13
+ export interface WorkspaceTabBrowseState {
14
+ expandedNodeIds: string[];
15
+ }
16
+
12
17
  export interface WorkspaceStorageLike {
13
18
  getItem(key: string): string | null;
14
19
  setItem(key: string, value: string): void;
@@ -43,6 +48,13 @@ function getLocationLabel(location: string): string {
43
48
  return "Search";
44
49
  case "/browse": {
45
50
  const collection = params.get("collection");
51
+ const browsePath = (params.get("path") ?? "")
52
+ .replace(/^\/+|\/+$/g, "")
53
+ .trim();
54
+ if (collection && browsePath) {
55
+ const leaf = browsePath.split("/").at(-1) ?? browsePath;
56
+ return `Browse: ${collection} / ${leaf}`;
57
+ }
46
58
  return collection ? `Browse: ${collection}` : "Browse";
47
59
  }
48
60
  case "/ask":
@@ -68,6 +80,19 @@ function getLocationLabel(location: string): string {
68
80
  }
69
81
  }
70
82
 
83
+ function isWorkspaceTabBrowseState(
84
+ value: unknown
85
+ ): value is WorkspaceTabBrowseState {
86
+ if (!value || typeof value !== "object") {
87
+ return false;
88
+ }
89
+ const candidate = value as Record<string, unknown>;
90
+ return (
91
+ Array.isArray(candidate.expandedNodeIds) &&
92
+ candidate.expandedNodeIds.every((entry) => typeof entry === "string")
93
+ );
94
+ }
95
+
71
96
  function isWorkspaceTab(value: unknown): value is WorkspaceTab {
72
97
  if (!value || typeof value !== "object") {
73
98
  return false;
@@ -76,7 +101,9 @@ function isWorkspaceTab(value: unknown): value is WorkspaceTab {
76
101
  return (
77
102
  typeof candidate.id === "string" &&
78
103
  typeof candidate.label === "string" &&
79
- typeof candidate.location === "string"
104
+ typeof candidate.location === "string" &&
105
+ (candidate.browseState === undefined ||
106
+ isWorkspaceTabBrowseState(candidate.browseState))
80
107
  );
81
108
  }
82
109
 
@@ -187,6 +214,37 @@ export function createWorkspaceTab(
187
214
  };
188
215
  }
189
216
 
217
+ export function updateActiveTabBrowseState(
218
+ state: WorkspaceState,
219
+ nextBrowseState:
220
+ | WorkspaceTabBrowseState
221
+ | ((current: WorkspaceTabBrowseState) => WorkspaceTabBrowseState)
222
+ ): WorkspaceState {
223
+ const nextTabs = state.tabs.map((tab) => {
224
+ if (tab.id !== state.activeTabId) {
225
+ return tab;
226
+ }
227
+
228
+ const currentBrowseState: WorkspaceTabBrowseState = tab.browseState ?? {
229
+ expandedNodeIds: [],
230
+ };
231
+ const resolvedBrowseState =
232
+ typeof nextBrowseState === "function"
233
+ ? nextBrowseState(currentBrowseState)
234
+ : nextBrowseState;
235
+
236
+ return {
237
+ ...tab,
238
+ browseState: resolvedBrowseState,
239
+ };
240
+ });
241
+
242
+ return {
243
+ tabs: nextTabs,
244
+ activeTabId: state.activeTabId,
245
+ };
246
+ }
247
+
190
248
  export function activateWorkspaceTab(
191
249
  state: WorkspaceState,
192
250
  tabId: string