diff --git a/src/core/api/endpoints.ts b/src/core/api/endpoints.ts index 5301249..849a477 100644 --- a/src/core/api/endpoints.ts +++ b/src/core/api/endpoints.ts @@ -12,18 +12,27 @@ import { ApiError } from './errors'; import type { GiteaClient, RequestOptions } from './client'; import type { + AccessToken, + ActionRun, + ActionRunJob, + ActionWorkflow, Activity, Branch, + BranchProtection, ChangedFile, CombinedStatus, Comment, Commit, ContentsEntry, + GpgKey, Issue, Label, Milestone, NotificationCount, NotificationThread, + Organization, + Package, + PublicKey, PullRequest, PullReview, Release, @@ -33,6 +42,7 @@ import type { Subscription, Tag, User, + Webhook, } from './types'; export interface PageParams { @@ -250,6 +260,366 @@ export function fetchRelease( return client.get(`${repoPath(owner, repo)}/releases/${id}`, options); } +export interface ReleaseInput { + tag_name: string; + target_commitish?: string; + name?: string; + body?: string; + draft?: boolean; + prerelease?: boolean; +} + +export function createRelease( + client: GiteaClient, + owner: string, + repo: string, + input: ReleaseInput, + options?: RequestOptions, +): Promise { + return client.post(`${repoPath(owner, repo)}/releases`, input, options); +} + +export function updateRelease( + client: GiteaClient, + owner: string, + repo: string, + id: number, + input: Partial, + options?: RequestOptions, +): Promise { + return client.patch(`${repoPath(owner, repo)}/releases/${id}`, input, options); +} + +export function deleteRelease( + client: GiteaClient, + owner: string, + repo: string, + id: number, + options?: RequestOptions, +): Promise { + return client.delete(`${repoPath(owner, repo)}/releases/${id}`, options); +} + +// --------------------------------------------------------------------------- +// Repository management, collaborators, webhooks, branch protections +// --------------------------------------------------------------------------- + +export interface CreateRepoInput { + name: string; + description?: string; + private?: boolean; + auto_init?: boolean; + default_branch?: string; + gitignores?: string; + license?: string; + readme?: string; +} + +export function createRepo( + client: GiteaClient, + input: CreateRepoInput, + options?: RequestOptions, +): Promise { + return client.post('/user/repos', input, options); +} + +export interface UpdateRepoInput { + name?: string; + description?: string; + website?: string; + private?: boolean; + default_branch?: string; + has_issues?: boolean; + has_pull_requests?: boolean; + has_wiki?: boolean; + has_releases?: boolean; + has_projects?: boolean; + has_packages?: boolean; + has_actions?: boolean; + archived?: boolean; +} + +export function updateRepo( + client: GiteaClient, + owner: string, + repo: string, + input: UpdateRepoInput, + options?: RequestOptions, +): Promise { + return client.patch(repoPath(owner, repo), input, options); +} + +export function deleteRepo( + client: GiteaClient, + owner: string, + repo: string, + options?: RequestOptions, +): Promise { + return client.delete(repoPath(owner, repo), options); +} + +export type CollaboratorPermission = 'read' | 'write' | 'admin'; + +export function addCollaborator( + client: GiteaClient, + owner: string, + repo: string, + collaborator: string, + permission: CollaboratorPermission = 'write', + options?: RequestOptions, +): Promise { + return client.put( + `${repoPath(owner, repo)}/collaborators/${enc(collaborator)}`, + { permission }, + options, + ); +} + +export function removeCollaborator( + client: GiteaClient, + owner: string, + repo: string, + collaborator: string, + options?: RequestOptions, +): Promise { + return client.delete(`${repoPath(owner, repo)}/collaborators/${enc(collaborator)}`, options); +} + +export function listWebhooks( + client: GiteaClient, + owner: string, + repo: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/hooks`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export interface CreateWebhookInput { + type: string; + config: Record; + events?: string[]; + active?: boolean; + branch_filter?: string; +} + +export function createWebhook( + client: GiteaClient, + owner: string, + repo: string, + input: CreateWebhookInput, + options?: RequestOptions, +): Promise { + return client.post(`${repoPath(owner, repo)}/hooks`, input, options); +} + +export function deleteWebhook( + client: GiteaClient, + owner: string, + repo: string, + id: number, + options?: RequestOptions, +): Promise { + return client.delete(`${repoPath(owner, repo)}/hooks/${id}`, options); +} + +export function listBranchProtections( + client: GiteaClient, + owner: string, + repo: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/branch_protections`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export interface CreateBranchProtectionInput { + branch_name: string; + required_approvals?: number; + enable_push?: boolean; + enable_status_check?: boolean; + status_check_contexts?: string[]; + require_signed_commits?: boolean; +} + +export function createBranchProtection( + client: GiteaClient, + owner: string, + repo: string, + input: CreateBranchProtectionInput, + options?: RequestOptions, +): Promise { + return client.post(`${repoPath(owner, repo)}/branch_protections`, input, options); +} + +export function deleteBranchProtection( + client: GiteaClient, + owner: string, + repo: string, + name: string, + options?: RequestOptions, +): Promise { + return client.delete(`${repoPath(owner, repo)}/branch_protections/${enc(name)}`, options); +} + +// --------------------------------------------------------------------------- +// Gitea Actions +// --------------------------------------------------------------------------- + +export function listWorkflows( + client: GiteaClient, + owner: string, + repo: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/actions/workflows`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export interface ListActionRunsParams extends PageParams { + event?: string; + branch?: string; + status?: string; + actor?: string; +} + +export function listActionRuns( + client: GiteaClient, + owner: string, + repo: string, + params: ListActionRunsParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/actions/runs`, { + ...options, + query: { + page: params.page ?? 1, + limit: params.limit ?? 20, + event: params.event, + branch: params.branch, + status: params.status, + actor: params.actor, + }, + }); +} + +export function fetchActionRun( + client: GiteaClient, + owner: string, + repo: string, + id: number, + options?: RequestOptions, +): Promise { + return client.get(`${repoPath(owner, repo)}/actions/runs/${id}`, options); +} + +export function listActionRunJobs( + client: GiteaClient, + owner: string, + repo: string, + run: number, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/actions/runs/${run}/jobs`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +/** Fetch a single job's log as plain text (falls back to the browser when unavailable). */ +export function fetchActionJobLogs( + client: GiteaClient, + owner: string, + repo: string, + jobId: number, + options?: RequestOptions, +): Promise { + return client.getText(`${repoPath(owner, repo)}/actions/jobs/${jobId}/logs`, options); +} + +// --------------------------------------------------------------------------- +// Organizations, packages, user credentials +// --------------------------------------------------------------------------- + +export function listUserOrgs( + client: GiteaClient, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw('/user/orgs', { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 30 }, + }); +} + +export function listOrgRepos( + client: GiteaClient, + org: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`/orgs/${enc(org)}/repos`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 20 }, + }); +} + +export function listPackages( + client: GiteaClient, + owner: string, + params: PageParams & { type?: string; q?: string } = {}, + options?: RequestOptions, +) { + return client.raw(`/packages/${enc(owner)}`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 20, type: params.type, q: params.q }, + }); +} + +export function listUserKeys( + client: GiteaClient, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw('/user/keys', { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export function listUserGpgKeys( + client: GiteaClient, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw('/user/gpg_keys', { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export function listUserTokens( + client: GiteaClient, + username: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`/users/${enc(username)}/tokens`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + // --------------------------------------------------------------------------- // Contents, README, branches, tags // --------------------------------------------------------------------------- diff --git a/src/core/api/query-keys.ts b/src/core/api/query-keys.ts index 69b52d9..671ba52 100644 --- a/src/core/api/query-keys.ts +++ b/src/core/api/query-keys.ts @@ -56,6 +56,45 @@ export const queryKeys = { release: (accountId: string, owner: string, name: string, id: number) => [...queryKeys.repo(accountId, owner, name), 'release', id] as const, + // --- repository settings ------------------------------------------------ + repoWebhooks: (accountId: string, owner: string, name: string) => + [...queryKeys.repo(accountId, owner, name), 'webhooks'] as const, + + repoProtections: (accountId: string, owner: string, name: string) => + [...queryKeys.repo(accountId, owner, name), 'branch-protections'] as const, + + // --- actions ------------------------------------------------------------- + actionsWorkflows: (accountId: string, owner: string, name: string) => + [...queryKeys.repo(accountId, owner, name), 'actions', 'workflows'] as const, + + actionsRuns: (accountId: string, owner: string, name: string, params: Record) => + [...queryKeys.repo(accountId, owner, name), 'actions', 'runs', params] as const, + + actionRun: (accountId: string, owner: string, name: string, id: number) => + [...queryKeys.repo(accountId, owner, name), 'actions', 'run', id] as const, + + actionJobs: (accountId: string, owner: string, name: string, run: number) => + [...queryKeys.repo(accountId, owner, name), 'actions', 'run', run, 'jobs'] as const, + + actionJobLogs: (accountId: string, owner: string, name: string, jobId: number) => + [...queryKeys.repo(accountId, owner, name), 'actions', 'job', jobId, 'logs'] as const, + + // --- account-level: orgs, packages, credentials ------------------------- + orgs: (accountId: string) => [...queryKeys.account(accountId), 'orgs'] as const, + + orgRepos: (accountId: string, org: string, params: Record) => + [...queryKeys.account(accountId), 'org', org, 'repos', params] as const, + + packages: (accountId: string, owner: string, params: Record) => + [...queryKeys.account(accountId), 'packages', owner, params] as const, + + userKeys: (accountId: string) => [...queryKeys.account(accountId), 'keys'] as const, + + userGpgKeys: (accountId: string) => [...queryKeys.account(accountId), 'gpg-keys'] as const, + + userTokens: (accountId: string, username: string) => + [...queryKeys.account(accountId), 'tokens', username] as const, + contents: (accountId: string, owner: string, name: string, path: string, ref?: string) => [...queryKeys.repo(accountId, owner, name), 'contents', { path, ref }] as const, diff --git a/src/core/api/types.ts b/src/core/api/types.ts index 6813084..d410ffa 100644 --- a/src/core/api/types.ts +++ b/src/core/api/types.ts @@ -382,6 +382,182 @@ export interface Subscription { ignored?: boolean; } +// --------------------------------------------------------------------------- +// Gitea Actions +// --------------------------------------------------------------------------- + +export interface ActionWorkflow { + id?: number; + name?: string; + path?: string; + state?: string; + html_url?: string; + badge_url?: string; +} + +export interface ActionRunStep { + name?: string; + status?: string; + conclusion?: string; + number?: number; + started_at?: string; + stopped_at?: string; +} + +export interface ActionRun { + id?: number; + title?: string; + status?: string; + conclusion?: string; + workflow_id?: string; + index?: number; + event?: string; + display_title?: string; + head_branch?: string; + head_sha?: string; + run_number?: number; + run_attempt?: number; + url?: string; + html_url?: string; + started_at?: string; + stopped_at?: string; + created_at?: string; + updated_at?: string; + actor?: User; + trigger_user?: User; + repository?: Repository; +} + +export interface ActionRunJob { + id?: number; + name?: string; + run_id?: number; + status?: string; + conclusion?: string; + started_at?: string; + stopped_at?: string; + runner_id?: number; + labels?: string[]; + steps?: ActionRunStep[]; + html_url?: string; +} + +// --------------------------------------------------------------------------- +// Repository settings +// --------------------------------------------------------------------------- + +export interface BranchProtection { + branch_name?: string; + rule_name?: string; + enable_push?: boolean; + enable_push_whitelist?: boolean; + enable_merge_whitelist?: boolean; + enable_status_check?: boolean; + status_check_contexts?: string[]; + required_approvals?: number; + enable_approvals_whitelist?: boolean; + require_signed_commits?: boolean; + block_on_rejected_reviews?: boolean; + block_on_outdated_branch?: boolean; + enable_force_push?: boolean; + priority?: number; + created_at?: string; + updated_at?: string; +} + +export interface Webhook { + id?: number; + type?: string; + name?: string; + active?: boolean; + events?: string[]; + config?: Record; + branch_filter?: string; + created_at?: string; + updated_at?: string; +} + +export interface DeployKey { + id?: number; + title?: string; + key?: string; + fingerprint?: string; + read_only?: boolean; + created_at?: string; +} + +// --------------------------------------------------------------------------- +// Organizations & packages +// --------------------------------------------------------------------------- + +export interface Organization { + id?: number; + name?: string; + username?: string; + full_name?: string; + avatar_url?: string; + description?: string; + location?: string; + website?: string; + visibility?: string; +} + +export interface Package { + id?: number; + name?: string; + type?: string; + version?: string; + owner?: User; + creator?: User; + repository?: Repository; + html_url?: string; + created_at?: string; +} + +// --------------------------------------------------------------------------- +// User credentials (read-only listings) +// --------------------------------------------------------------------------- + +export interface PublicKey { + id?: number; + title?: string; + key?: string; + fingerprint?: string; + key_type?: string; + read_only?: boolean; + created_at?: string; + last_used_at?: string; + url?: string; + user?: User; +} + +export interface GpgKeyEmail { + email?: string; + verified?: boolean; +} + +export interface GpgKey { + id?: number; + key_id?: string; + primary_key_id?: string; + public_key?: string; + emails?: GpgKeyEmail[]; + can_sign?: boolean; + can_certify?: boolean; + created_at?: string; + expires_at?: string; + verified?: boolean; +} + +export interface AccessToken { + id?: number; + name?: string; + token_last_eight?: string; + scopes?: string[]; + created_at?: string; + last_used_at?: string; +} + export interface Activity { id?: number; user_id?: number; diff --git a/src/features/actions/queries.ts b/src/features/actions/queries.ts new file mode 100644 index 0000000..ecd89cf --- /dev/null +++ b/src/features/actions/queries.ts @@ -0,0 +1,99 @@ +/** Gitea Actions query hooks (workflows, runs, jobs, logs). */ + +import { + keepPreviousData, + useInfiniteQuery, + useQuery, + type InfiniteData, +} from '@tanstack/react-query'; + +import type { RawResponse } from '@/core/api/client'; +import { + fetchActionJobLogs, + fetchActionRun, + listActionRunJobs, + listActionRuns, + listWorkflows, + type ListActionRunsParams, +} from '@/core/api/endpoints'; +import { queryKeys } from '@/core/api/query-keys'; +import type { ActionRun, ActionRunJob, ActionWorkflow } from '@/core/api/types'; +import { useSession } from '@/features/accounts/use-session'; + +export function useWorkflows(owner: string, name: string) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + + return useQuery({ + queryKey: queryKeys.actionsWorkflows(accountId, owner, name), + enabled: Boolean(client) && Boolean(accountId), + queryFn: ({ signal }) => + listWorkflows(client!, owner, name, { limit: 50 }, { signal }).then((r) => r.data), + staleTime: 60_000, + }); +} + +export function useActionRuns( + owner: string, + name: string, + filters: Pick = {}, + limit = 20, +) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + const params = { ...filters, limit }; + + return useInfiniteQuery< + RawResponse, + Error, + InfiniteData>, + readonly unknown[], + number + >({ + queryKey: queryKeys.actionsRuns(accountId, owner, name, params), + enabled: Boolean(client) && Boolean(accountId), + initialPageParam: 1, + queryFn: ({ pageParam, signal }) => + listActionRuns(client!, owner, name, { ...params, page: pageParam }, { signal }), + getNextPageParam: (last, all) => (last.hasMore ? all.length + 1 : undefined), + placeholderData: keepPreviousData, + }); +} + +export function useActionRun(owner: string, name: string, id: number) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + + return useQuery({ + queryKey: queryKeys.actionRun(accountId, owner, name, id), + enabled: Boolean(client) && Boolean(accountId) && id > 0, + queryFn: ({ signal }) => fetchActionRun(client!, owner, name, id, { signal }), + staleTime: 30_000, + }); +} + +export function useActionRunJobs(owner: string, name: string, run: number) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + + return useQuery({ + queryKey: queryKeys.actionJobs(accountId, owner, name, run), + enabled: Boolean(client) && Boolean(accountId) && run > 0, + queryFn: ({ signal }) => + listActionRunJobs(client!, owner, name, run, { limit: 50 }, { signal }).then((r) => r.data), + staleTime: 30_000, + }); +} + +export function useActionJobLogs(owner: string, name: string, jobId: number, enabled = true) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + + return useQuery({ + queryKey: queryKeys.actionJobLogs(accountId, owner, name, jobId), + enabled: Boolean(client) && Boolean(accountId) && jobId > 0 && enabled, + queryFn: ({ signal }) => fetchActionJobLogs(client!, owner, name, jobId, { signal }), + staleTime: 60_000, + retry: false, + }); +} diff --git a/src/features/profile/queries.ts b/src/features/profile/queries.ts new file mode 100644 index 0000000..563af6f --- /dev/null +++ b/src/features/profile/queries.ts @@ -0,0 +1,91 @@ +/** Account-level data: organizations, packages and credential listings. */ + +import { useInfiniteQuery, type InfiniteData } from '@tanstack/react-query'; + +import type { RawResponse } from '@/core/api/client'; +import { + listOrgRepos, + listPackages, + listUserGpgKeys, + listUserKeys, + listUserOrgs, + listUserTokens, +} from '@/core/api/endpoints'; +import { queryKeys } from '@/core/api/query-keys'; +import type { AccessToken, GpgKey, Organization, Package, PublicKey, Repository } from '@/core/api/types'; +import { useSession } from '@/features/accounts/use-session'; + +function useAccountList( + key: readonly unknown[], + load: ( + page: number, + signal: AbortSignal, + ) => Promise>, +) { + return useInfiniteQuery< + RawResponse, + Error, + InfiniteData>, + readonly unknown[], + number + >({ + queryKey: key, + initialPageParam: 1, + queryFn: ({ pageParam, signal }) => load(pageParam, signal), + getNextPageParam: (last, all) => (last.hasMore ? all.length + 1 : undefined), + }); +} + +export function useUserOrgs(limit = 30) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + [...queryKeys.orgs(accountId), { limit }], + (page, signal) => listUserOrgs(client!, { page, limit }, { signal }), + ); +} + +export function useOrgRepos(org: string, limit = 20) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + queryKeys.orgRepos(accountId, org, { limit }), + (page, signal) => listOrgRepos(client!, org, { page, limit }, { signal }), + ); +} + +export function usePackages(owner: string, limit = 20) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + queryKeys.packages(accountId, owner, { limit }), + (page, signal) => listPackages(client!, owner, { page, limit }, { signal }), + ); +} + +export function useUserKeys(limit = 50) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + [...queryKeys.userKeys(accountId), { limit }], + (page, signal) => listUserKeys(client!, { page, limit }, { signal }), + ); +} + +export function useUserGpgKeys(limit = 50) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + [...queryKeys.userGpgKeys(accountId), { limit }], + (page, signal) => listUserGpgKeys(client!, { page, limit }, { signal }), + ); +} + +export function useUserTokens(username: string, limit = 50) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useAccountList( + queryKeys.userTokens(accountId, username), + (page, signal) => listUserTokens(client!, username, { page, limit }, { signal }), + ); +} diff --git a/src/features/profile/screens/credentials-screen.tsx b/src/features/profile/screens/credentials-screen.tsx new file mode 100644 index 0000000..0b1c4e7 --- /dev/null +++ b/src/features/profile/screens/credentials-screen.tsx @@ -0,0 +1,136 @@ +import { StyleSheet, View } from 'react-native'; +import { useTranslation } from 'react-i18next'; +import type { ReactNode } from 'react'; + +import { Screen } from '@/components/layout/screen'; +import { AppText } from '@/components/ui/app-text'; +import { Badge } from '@/components/ui/badge'; +import { Card } from '@/components/ui/card'; +import { EmptyState, ErrorState, LoadingView } from '@/components/ui/state-views'; +import { apiErrorMessage } from '@/core/api/error-text'; +import { flattenPages } from '@/core/utils/infinite'; +import { formatRelativeTime } from '@/core/utils/format'; +import { useSession } from '@/features/accounts/use-session'; +import { useUserGpgKeys, useUserKeys, useUserTokens } from '@/features/profile/queries'; +import { fontFamily, spacing } from '@/theme/tokens'; + +/** Read-only listings of the account's SSH keys, GPG keys and access tokens. */ +export function CredentialsScreen() { + const { t } = useTranslation(); + const { account, client } = useSession(); + const keys = useUserKeys(); + const gpgKeys = useUserGpgKeys(); + const tokens = useUserTokens(account?.login ?? ''); + + if (keys.isError || gpgKeys.isError || tokens.isError) { + const error = keys.error ?? gpgKeys.error ?? tokens.error; + return ( + { + void keys.refetch(); + void gpgKeys.refetch(); + void tokens.refetch(); + }} + /> + ); + } + + if (!client || (keys.isPending && gpgKeys.isPending && tokens.isPending)) { + return ; + } + + const keyItems = flattenPages(keys.data); + const gpgItems = flattenPages(gpgKeys.data); + const tokenItems = flattenPages(tokens.data); + + return ( + +
+ {keyItems.length === 0 ? ( + + ) : ( + keyItems.map((key) => ( + + + {key.title} + + + {key.fingerprint ?? key.key} + + {key.read_only ? {t('credentials.readOnly')} : null} + + )) + )} +
+ +
+ {gpgItems.length === 0 ? ( + + ) : ( + gpgItems.map((key) => ( + + + + {key.key_id} + + {key.verified ? {t('commits.verified')} : null} + + {key.emails && key.emails.length > 0 ? ( + + {key.emails.map((email) => email.email).filter(Boolean).join(', ')} + + ) : null} + + )) + )} +
+ +
+ {tokenItems.length === 0 ? ( + + ) : ( + tokenItems.map((token) => ( + + + {token.name} + + + + ••••{token.token_last_eight ?? ''} + + {token.last_used_at ? ( + + {formatRelativeTime(token.last_used_at, t)} + + ) : null} + + + )) + )} +
+ + + {t('credentials.notice')} + +
+ ); +} + +const styles = StyleSheet.create({ + section: { gap: spacing.sm }, + row: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm }, + keyId: { flex: 1, fontFamily: fontFamily.mono }, + metaRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, flexWrap: 'wrap' }, +}); + +function Section({ title, children }: { title: string; children: ReactNode }) { + return ( + + {title} + {children} + + ); +} diff --git a/src/features/profile/screens/org-repos-screen.tsx b/src/features/profile/screens/org-repos-screen.tsx new file mode 100644 index 0000000..39180fc --- /dev/null +++ b/src/features/profile/screens/org-repos-screen.tsx @@ -0,0 +1,41 @@ +import { useRoute, type RouteProp } from '@react-navigation/native'; +import { useTranslation } from 'react-i18next'; + +import { RepoListItem } from '@/components/git/repo-list-item'; +import { QueryList } from '@/components/layout/query-list'; +import { EmptyState } from '@/components/ui/state-views'; +import type { Repository } from '@/core/api/types'; +import { flattenPages } from '@/core/utils/infinite'; +import { useOrgRepos } from '@/features/profile/queries'; +import type { SettingsStackParamList } from '@/navigation/types'; +import { useRootNavigation } from '@/navigation/use-root-navigation'; + +/** Repositories belonging to one organization. */ +export function OrgReposScreen() { + const { t } = useTranslation(); + const route = useRoute>(); + const navigation = useRootNavigation(); + const { org } = route.params; + const query = useOrgRepos(org); + const repos: Repository[] = flattenPages(query.data); + + return ( + + query={query} + items={repos} + keyExtractor={(repo) => String(repo.id)} + renderItem={(repo) => ( + + navigation.navigate('Repository', { + screen: 'RepoOverview', + params: { owner: repo.owner?.login ?? org, name: repo.name }, + }) + } + /> + )} + empty={} + /> + ); +} diff --git a/src/features/profile/screens/organizations-screen.tsx b/src/features/profile/screens/organizations-screen.tsx new file mode 100644 index 0000000..7a8aaf5 --- /dev/null +++ b/src/features/profile/screens/organizations-screen.tsx @@ -0,0 +1,65 @@ +import { StyleSheet, View } from 'react-native'; +import { useTranslation } from 'react-i18next'; +import { useNavigation } from '@react-navigation/native'; +import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; + +import { QueryList } from '@/components/layout/query-list'; +import { AppText } from '@/components/ui/app-text'; +import { Avatar } from '@/components/ui/avatar'; +import { EmptyState } from '@/components/ui/state-views'; +import type { Organization } from '@/core/api/types'; +import { flattenPages } from '@/core/utils/infinite'; +import { useUserOrgs } from '@/features/profile/queries'; +import { spacing } from '@/theme/tokens'; +import type { SettingsStackParamList } from '@/navigation/types'; + +/** Organizations the signed-in user belongs to. */ +export function OrganizationsScreen() { + const { t } = useTranslation(); + const navigation = useNavigation>(); + const query = useUserOrgs(); + const orgs: Organization[] = flattenPages(query.data); + + return ( + + query={query} + items={orgs} + keyExtractor={(org) => String(org.id ?? org.username)} + renderItem={(org) => { + const login = org.username ?? org.name ?? ''; + return ( + + + + navigation.navigate('OrgRepos', { org: login })} + accessibilityRole="button" + > + {login} + + {org.description ? ( + + {org.description} + + ) : null} + + + ); + }} + empty={} + /> + ); +} + +const styles = StyleSheet.create({ + row: { + flexDirection: 'row', + alignItems: 'center', + gap: spacing.md, + paddingHorizontal: spacing.lg, + paddingVertical: spacing.md, + }, + body: { flex: 1, gap: 2 }, +}); diff --git a/src/features/profile/screens/packages-screen.tsx b/src/features/profile/screens/packages-screen.tsx new file mode 100644 index 0000000..26fc7bb --- /dev/null +++ b/src/features/profile/screens/packages-screen.tsx @@ -0,0 +1,58 @@ +import { StyleSheet, View } from 'react-native'; +import { useTranslation } from 'react-i18next'; + +import { QueryList } from '@/components/layout/query-list'; +import { AppText } from '@/components/ui/app-text'; +import { Badge } from '@/components/ui/badge'; +import { EmptyState } from '@/components/ui/state-views'; +import type { Package } from '@/core/api/types'; +import { flattenPages } from '@/core/utils/infinite'; +import { formatRelativeTime } from '@/core/utils/format'; +import { usePackages } from '@/features/profile/queries'; +import { useSession } from '@/features/accounts/use-session'; +import { spacing } from '@/theme/tokens'; + +/** Packages published by the authenticated user. */ +export function PackagesScreen() { + const { t } = useTranslation(); + const { account } = useSession(); + const query = usePackages(account?.login ?? ''); + const packages: Package[] = flattenPages(query.data); + + return ( + + query={query} + items={packages} + keyExtractor={(pkg) => `${pkg.type ?? ''}-${pkg.name ?? ''}-${pkg.version ?? ''}`} + renderItem={(pkg) => ( + + + + {pkg.name} + + + {pkg.type ? {pkg.type} : null} + {pkg.version ? ( + + {pkg.version} + + ) : null} + {pkg.created_at ? ( + + {formatRelativeTime(pkg.created_at, t)} + + ) : null} + + + + )} + empty={} + /> + ); +} + +const styles = StyleSheet.create({ + row: { paddingHorizontal: spacing.lg, paddingVertical: spacing.md }, + body: { gap: spacing.xs }, + metaRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, flexWrap: 'wrap' }, +}); diff --git a/src/features/repositories/queries.ts b/src/features/repositories/queries.ts index 3a75c8e..db46f0b 100644 --- a/src/features/repositories/queries.ts +++ b/src/features/repositories/queries.ts @@ -29,8 +29,12 @@ import { unstarRepo, unwatchRepo, watchRepo, + createRelease, + updateRelease, + deleteRelease, type ForkRepoInput, type ListMyReposParams, + type ReleaseInput, type SearchReposParams, } from '@/core/api/endpoints'; import type { RawResponse } from '@/core/api/client'; @@ -236,3 +240,45 @@ export function useRelease(owner: string, name: string, id: number) { queryFn: ({ signal }) => fetchRelease(client!, owner, name, id, { signal }), }); } + +function invalidateReleases( + queryClient: ReturnType, + accountId: string, + owner: string, + name: string, +) { + void queryClient.invalidateQueries({ queryKey: queryKeys.repo(accountId, owner, name) }); +} + +export function useCreateRelease(owner: string, name: string) { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + + return useMutation({ + mutationFn: (input: ReleaseInput) => createRelease(client!, owner, name, input), + onSuccess: () => invalidateReleases(queryClient, accountId, owner, name), + }); +} + +export function useUpdateRelease(owner: string, name: string, id: number) { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + + return useMutation({ + mutationFn: (input: Partial) => updateRelease(client!, owner, name, id, input), + onSuccess: () => invalidateReleases(queryClient, accountId, owner, name), + }); +} + +export function useDeleteRelease(owner: string, name: string, id: number) { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + + return useMutation({ + mutationFn: () => deleteRelease(client!, owner, name, id), + onSuccess: () => invalidateReleases(queryClient, accountId, owner, name), + }); +} diff --git a/src/features/repositories/screens/create-repo-screen.tsx b/src/features/repositories/screens/create-repo-screen.tsx new file mode 100644 index 0000000..2665635 --- /dev/null +++ b/src/features/repositories/screens/create-repo-screen.tsx @@ -0,0 +1,122 @@ +import { useState } from 'react'; +import { Alert, StyleSheet, Switch, View } from 'react-native'; +import { useTranslation } from 'react-i18next'; + +import { Screen } from '@/components/layout/screen'; +import { AppText } from '@/components/ui/app-text'; +import { Button } from '@/components/ui/button'; +import { Card } from '@/components/ui/card'; +import { Field } from '@/components/ui/field'; +import { apiErrorMessage } from '@/core/api/error-text'; +import { useCreateRepo } from '@/features/repositories/settings-queries'; +import { useTheme } from '@/theme/theme-provider'; +import { spacing } from '@/theme/tokens'; +import type { RepositoriesStackParamList } from '@/navigation/types'; +import type { ScreenProps } from '@/navigation/typing'; + +type Props = ScreenProps; + +/** Create a repository under the authenticated user. */ +export function CreateRepoScreen({ navigation }: Props) { + const { t } = useTranslation(); + const { colors } = useTheme(); + const create = useCreateRepo(); + + const [name, setName] = useState(''); + const [description, setDescription] = useState(''); + const [isPrivate, setIsPrivate] = useState(false); + const [autoInit, setAutoInit] = useState(true); + const [submitted, setSubmitted] = useState(false); + + const nameError = submitted && name.trim().length === 0 ? t('createRepo.nameRequired') : undefined; + + const submit = () => { + setSubmitted(true); + if (name.trim().length === 0) return; + create.mutate( + { + name: name.trim(), + description: description.trim() || undefined, + private: isPrivate, + auto_init: autoInit, + }, + { + onSuccess: () => navigation.goBack(), + onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), + }, + ); + }; + + return ( + + + + + + + {t('repositories.private')} + + + + + + {t('createRepo.autoInit')} + + {t('createRepo.autoInitHelp')} + + + + + + + {create.isError ? ( + + {apiErrorMessage(create.error, t)} + + ) : null} + +