From 15e8ecf7a2c35b580acdcfdec99b430a15c3b89d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 12 Nov 2026 22:43:42 +0900 Subject: [PATCH] refac --- src/lib/components/chat/Tabs/GitBar.svelte | 65 ++- src/lib/components/chat/Tabs/GitIssues.svelte | 442 ++++++++++++++++++ .../chat/Tabs/GitPullRequests.svelte | 8 +- src/lib/components/chat/Tabs/gitPanel.ts | 23 + 4 files changed, 506 insertions(+), 32 deletions(-) create mode 100644 src/lib/components/chat/Tabs/GitIssues.svelte create mode 100644 src/lib/components/chat/Tabs/gitPanel.ts diff --git a/src/lib/components/chat/Tabs/GitBar.svelte b/src/lib/components/chat/Tabs/GitBar.svelte index f7e737f66f..bcc3877af3 100644 --- a/src/lib/components/chat/Tabs/GitBar.svelte +++ b/src/lib/components/chat/Tabs/GitBar.svelte @@ -21,6 +21,8 @@ import Tooltip from '../../common/Tooltip.svelte'; import GitPullRequests from './GitPullRequests.svelte'; import GitListDivider from './GitListDivider.svelte'; + import GitIssues from './GitIssues.svelte'; + import { countLabel, relativeTime } from './gitPanel'; export let target: TerminalTarget | null = null; export let directory = '.'; @@ -35,9 +37,11 @@ export const captureView = () => ({ expanded, summary, description, height, listWidth, view }); export let diffMode: 'unified' | 'split' = 'unified'; export let hideWhitespace = false; - let view: 'changes' | 'history' | 'pull requests' = initialView?.view ?? 'changes'; - let prCapabilities: { available: boolean; default_branch?: string } | null = null; + let view: 'changes' | 'history' | 'pull requests' | 'issues' = initialView?.view ?? 'changes'; + let prCapabilities: { available: boolean; default_branch?: string; issues?: boolean } | null = + null; let openPullRequests: number | null = null; + let openIssues: number | null = null; let prRoot = ''; let selected: { path: string; staged: boolean; untracked: boolean } | null = null; let selectedCommit: GitCommit | null = null; @@ -169,16 +173,6 @@ syncComplete = false; }; - const relativeTime = (date: string) => { - const seconds = Math.max(0, Math.floor((Date.now() - new Date(date).getTime()) / 1000)); - return seconds < 60 - ? 'now' - : seconds < 3600 - ? `${Math.floor(seconds / 60)}m` - : seconds < 86400 - ? `${Math.floor(seconds / 3600)}h` - : `${Math.floor(seconds / 86400)}d`; - }; const request = ( operation: string, params: Record = {}, @@ -196,15 +190,15 @@ status = next.is_repo ? next : null; if (next.root && next.root !== prRoot) { prRoot = next.root; - void request<{ available: boolean; default_branch?: string }>('pr/capabilities') + void request>('pr/capabilities') .then((data) => { if (current !== generation) return; prCapabilities = data; - openPullRequests = null; - if (data.available) void loadPullRequestCount(); + openPullRequests = openIssues = null; + if (data.available) void loadOpenCounts(); }) .catch(() => { - if (current === generation) prCapabilities = openPullRequests = null; + if (current === generation) prCapabilities = openPullRequests = openIssues = null; }); } if ( @@ -228,15 +222,23 @@ pending = task; return task; }; - const loadPullRequestCount = async () => { - const current = generation; + const openCount = async (operation: string) => { try { - const data = await request<{ items: unknown[] }>('pr/list', { state: 'open' }); - if (current === generation) openPullRequests = data.items.length; + return (await request<{ items: unknown[] }>(operation, { state: 'open' })).items.length; } catch { - if (current === generation) openPullRequests = null; + return null; } }; + const loadOpenCounts = async () => { + const current = generation; + const [pullRequests, issues] = await Promise.all([ + openCount('pr/list'), + prCapabilities?.issues ? openCount('issue/list') : null + ]); + if (current !== generation) return; + openPullRequests = pullRequests; + openIssues = issues; + }; const resetSelection = () => { ++diffGeneration; selected = null; @@ -271,7 +273,7 @@ summary = restoring?.summary ?? ''; description = restoring?.description ?? ''; commits = []; - prCapabilities = openPullRequests = null; + prCapabilities = openPullRequests = openIssues = null; prRoot = ''; view = restoring?.view ?? 'changes'; resetSelection(); @@ -738,7 +740,7 @@
- {#each ['changes', 'history', ...(prCapabilities?.available ? ['pull requests'] : [])] as section}{/each}
@@ -835,6 +841,15 @@ void refresh(true); }} />{/key} + {:else if view === 'issues' && target && status.root} + {#key status.root} (listWidth = width)} + {target} + directory={status.root} + onCount={(count) => (openIssues = count)} + />{/key} {:else}
+ import { getContext, onMount, onDestroy } from 'svelte'; + import { gitRequest } from '$lib/apis/terminal/git'; + import type { TerminalTarget } from '$lib/apis/terminal'; + import Icon from '../FileNav/Icon.svelte'; + import GitListDivider from './GitListDivider.svelte'; + import Dropdown from '../../common/Dropdown.svelte'; + import DropdownMenu from '../../common/DropdownMenu.svelte'; + import Tooltip from '../../common/Tooltip.svelte'; + import { primaryButton, disabledButton, secondaryButton, relativeTime } from './gitPanel'; + export let target: TerminalTarget; + + export let directory: string; + export let listWidth = 240; + export let maxListWidth = 240; + export let onResizeList: (width: number) => void = () => {}; + export let onCount: (count: number) => void = () => {}; + type Issue = { + number: number; + title: string; + state: string; + stateReason?: string; + author?: { login: string }; + labels?: { name: string }[]; + assignees?: { login?: string; name?: string }[]; + url: string; + updatedAt?: string; + createdAt?: string; + body?: string; + comments?: { id?: string; body: string; createdAt?: string; author?: { login: string } }[]; + }; + const i18n = getContext('i18n'); + let issues: Issue[] = []; + let selected: Issue | null = null; + let state = 'open'; + let search = ''; + let listLoading = false; + let loading = false; + let busy = false; + let listError = ''; + let error = ''; + let editing = false; + let creating = false; + let title = ''; + let body = ''; + let comment = ''; + let listGeneration = 0; + let generation = 0; + + $: states = [ + { value: 'open', label: $i18n.t('Open') }, + { value: 'closed', label: $i18n.t('Closed') }, + { value: 'all', label: $i18n.t('All') } + ]; + $: filtered = issues.filter((issue) => + `${issue.number} ${issue.title}`.toLowerCase().includes(search.toLowerCase()) + ); + $: showingDetail = !!selected || creating || editing; + + const status = (issue: Issue) => + issue.state === 'OPEN' + ? $i18n.t('Open') + : issue.stateReason === 'NOT_PLANNED' + ? $i18n.t('Not planned') + : issue.stateReason === 'DUPLICATE' + ? $i18n.t('Duplicate') + : $i18n.t('Closed'); + const people = (list?: { login?: string; name?: string }[]) => + list?.map((person) => person.login || person.name).join(', ') || $i18n.t('None'); + + const request = ( + operation: string, + params: Record = {}, + payload?: Record + ) => gitRequest(target, directory, `issue/${operation}`, params, payload); + const load = async () => { + const version = ++listGeneration; + listLoading = true; + listError = ''; + try { + const data = await request<{ items: Issue[] }>('list', { state }); + if (version !== listGeneration) return; + issues = data.items; + if (state !== 'closed') onCount(issues.filter((issue) => issue.state === 'OPEN').length); + } catch (reason) { + if (version === listGeneration) + listError = String(reason instanceof Error ? reason.message : reason); + } finally { + if (version === listGeneration) listLoading = false; + } + }; + const select = async (issue: Issue) => { + const version = ++generation; + selected = issue; + creating = editing = false; + loading = true; + error = ''; + try { + const data = await request('view', { number: String(issue.number) }); + if (version === generation) selected = data; + } catch (reason) { + if (version === generation) error = String(reason instanceof Error ? reason.message : reason); + } finally { + if (version === generation) loading = false; + } + }; + const mutate = async (action: string, reason = 'completed') => { + if (busy) return; + busy = true; + error = ''; + try { + await request( + action, + {}, + { + number: selected?.number ?? 0, + title, + body: ['create', 'edit'].includes(action) ? body : comment, + reason + } + ); + const wasCreating = creating; + creating = editing = false; + comment = ''; + void load(); + if (state === 'closed') + void request<{ items: Issue[] }>('list', { state: 'open' }) + .then((data) => onCount(data.items.length)) + .catch(() => {}); + if (selected && !wasCreating) await select(selected); + } catch (reason) { + error = String(reason instanceof Error ? reason.message : reason); + } finally { + busy = false; + } + }; + const startCreate = () => { + ++generation; + loading = false; + error = ''; + selected = null; + editing = false; + creating = true; + title = body = ''; + }; + const back = () => { + ++generation; + selected = null; + creating = editing = false; + error = ''; + loading = false; + }; + const cancelForm = () => { + if (editing) editing = false; + else back(); + }; + onMount(() => { + void load(); + }); + onDestroy(() => { + ++generation; + ++listGeneration; + }); + + +
+
+
+ + + +
+ + {#each states as option}{/each} + +
+
+ + + +
+ {#if listError}{/if} +
+ {#each filtered as issue (issue.number)}{/each} + {#if !filtered.length}
+ {$i18n.t(listLoading ? 'Loading...' : 'No issues')} +
{/if} +
+
+ +
+ {#if showingDetail} +
+ {selected && !creating + ? `#${selected.number} · ${selected.title}` + : $i18n.t('New issue')} + {#if selected && !creating && !editing} +
+ + + {#if selected.state === 'OPEN'}{:else}{/if} + {$i18n.t('Open in GitHub')} + +
{/if} +
+ {/if} + {#if error && showingDetail}{/if} + {#if creating || editing} +
mutate(editing ? 'edit' : 'create')} + > +
+ + +
+
+ + +
+
+ {:else if selected} + {#if loading}
+ {$i18n.t('Loading...')} +
+ {:else} +
+
+
+
{$i18n.t('Author')}
+
+ {selected.author?.login ?? $i18n.t('Unknown')} +
+
{$i18n.t('Status')}
+
{status(selected)}
+
{$i18n.t('Assignees')}
+
+ {people(selected.assignees)} +
+
{$i18n.t('Labels')}
+
+ {selected.labels?.map((item) => item.name).join(', ') || $i18n.t('None')} +
+ {#if selected.createdAt}
+ {$i18n.t('Opened')} +
+
+ {relativeTime(selected.createdAt)} +
{/if} +
+
+ {$i18n.t('Description')} +
+
+ {selected.body || $i18n.t('No description')} +
+
+ {#if selected.comments?.length}
+ {$i18n.t('Comments')} +
+ {#each selected.comments as item}
+
+ {item.author?.login ?? $i18n.t('Unknown')}{item.createdAt + ? ` · ${relativeTime(item.createdAt)}` + : ''} +
+

+ {item.body} +

+
{/each}{/if} +
+
mutate('comment')} + > + +
+ + +
+
+ {/if} + {:else}
+ {$i18n.t('Select an issue')} +
{/if} +
+
diff --git a/src/lib/components/chat/Tabs/GitPullRequests.svelte b/src/lib/components/chat/Tabs/GitPullRequests.svelte index 7d90968b3a..588318fc1a 100644 --- a/src/lib/components/chat/Tabs/GitPullRequests.svelte +++ b/src/lib/components/chat/Tabs/GitPullRequests.svelte @@ -9,6 +9,7 @@ import DropdownMenu from '../../common/DropdownMenu.svelte'; import ConfirmDialog from '../../common/ConfirmDialog.svelte'; import Tooltip from '../../common/Tooltip.svelte'; + import { primaryButton, disabledButton, secondaryButton } from './gitPanel'; export let target: TerminalTarget; export let directory: string; @@ -72,13 +73,6 @@ let listGeneration = 0; let generation = 0; - const primaryButton = - 'bg-gray-900 dark:bg-white text-white dark:text-black hover:bg-gray-800 dark:hover:bg-white/90'; - const disabledButton = - 'bg-gray-100 dark:bg-white/6 text-gray-400 dark:text-gray-600 cursor-default'; - const secondaryButton = - 'bg-gray-100 dark:bg-white/6 text-gray-600 dark:text-gray-300 hover:bg-gray-200/60 dark:hover:bg-white/10 disabled:text-gray-400 dark:disabled:text-gray-600 disabled:hover:bg-gray-100 dark:disabled:hover:bg-white/6'; - $: states = [ { value: 'open', label: $i18n.t('Open') }, { value: 'closed', label: $i18n.t('Closed') }, diff --git a/src/lib/components/chat/Tabs/gitPanel.ts b/src/lib/components/chat/Tabs/gitPanel.ts new file mode 100644 index 0000000000..7cc8a881e2 --- /dev/null +++ b/src/lib/components/chat/Tabs/gitPanel.ts @@ -0,0 +1,23 @@ +// Shared look and formatting for the Git panel's views. + +export const primaryButton = + 'bg-gray-900 dark:bg-white text-white dark:text-black hover:bg-gray-800 dark:hover:bg-white/90'; +export const disabledButton = + 'bg-gray-100 dark:bg-white/6 text-gray-400 dark:text-gray-600 cursor-default'; +export const secondaryButton = + 'bg-gray-100 dark:bg-white/6 text-gray-600 dark:text-gray-300 hover:bg-gray-200/60 dark:hover:bg-white/10 disabled:text-gray-400 dark:disabled:text-gray-600 disabled:hover:bg-gray-100 dark:disabled:hover:bg-white/6'; + +export const relativeTime = (date: string) => { + const seconds = Math.max(0, Math.floor((Date.now() - new Date(date).getTime()) / 1000)); + return seconds < 60 + ? 'now' + : seconds < 3600 + ? `${Math.floor(seconds / 60)}m` + : seconds < 86400 + ? `${Math.floor(seconds / 3600)}h` + : `${Math.floor(seconds / 86400)}d`; +}; + +// Pull request and issue lists return at most this many items. +export const LIST_LIMIT = 50; +export const countLabel = (count: number) => (count >= LIST_LIMIT ? `${LIST_LIMIT}+` : `${count}`);