Skip to content

Commit 9a1f1a3

Browse files
authored
Merge pull request bluesky-social#26 from bluesky-social/14-refactor-accountview
Refactor AccountView
2 parents 9cc3bee + 25ff695 commit 9a1f1a3

File tree

3 files changed

+91
-62
lines changed

3 files changed

+91
-62
lines changed

Diff for: app/repositories/[id]/page.tsx

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,62 @@
11
'use client'
22
import { AccountView } from '../../../components/repositories/AccountView'
3+
import client from '../../../lib/client'
4+
import { useQuery } from '@tanstack/react-query'
5+
import { createReport } from '../../../components/repositories/createReport'
36

47
export default function Repository({ params }: { params: { id: string } }) {
5-
return <AccountView id={decodeURIComponent(params.id)} />
8+
const { id } = params
9+
10+
const {
11+
error,
12+
data: { repo, profile } = {},
13+
refetch,
14+
} = useQuery({
15+
queryKey: ['accountView', { id }],
16+
queryFn: async () => {
17+
const getRepo = async () => {
18+
let did
19+
if (id.startsWith('did:')) {
20+
did = id
21+
} else {
22+
const { data: resolved } =
23+
await client.api.com.atproto.handle.resolve({ handle: id })
24+
did = resolved.did
25+
}
26+
const { data: repo } = await client.api.com.atproto.admin.getRepo(
27+
{ did },
28+
{ headers: client.adminHeaders() },
29+
)
30+
return repo
31+
}
32+
const getProfile = async () => {
33+
try {
34+
const { data: profile } = await client.api.app.bsky.actor.getProfile({
35+
actor: id,
36+
})
37+
return profile
38+
} catch (err) {
39+
if (err?.['error'] === 'AccountTakedown') {
40+
return undefined
41+
}
42+
throw err
43+
}
44+
}
45+
const [repo, profile] = await Promise.all([getRepo(), getProfile()])
46+
return { repo, profile }
47+
},
48+
})
49+
50+
return (
51+
<AccountView
52+
repo={repo}
53+
profile={profile}
54+
onSubmit={async (vals) => {
55+
await createReport(vals)
56+
refetch()
57+
}}
58+
error={error}
59+
id={id}
60+
/>
61+
)
662
}

Diff for: components/repositories/AccountView.tsx

+16-61
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import { AuthorFeed } from '../common/feeds/AuthorFeed'
1919
import { Json } from '../common/Json'
2020
import { classNames } from '../../lib/util'
2121
import client from '../../lib/client'
22-
import { ReportFormValues, ReportPanel } from '../reports/ReportPanel'
22+
import { ReportPanel } from '../reports/ReportPanel'
2323
import { ReportsTable } from '../reports/ReportsTable'
24+
import React from 'react'
2425

2526
enum Views {
2627
Details,
@@ -30,60 +31,29 @@ enum Views {
3031
Reports,
3132
}
3233

33-
export function AccountView({ id }: { id: string }) {
34+
export function AccountView({
35+
repo,
36+
profile,
37+
error,
38+
id,
39+
onSubmit,
40+
}: {
41+
id: string
42+
repo?: GetRepo.OutputSchema
43+
profile?: GetProfile.OutputSchema
44+
error?: unknown
45+
onSubmit: (vals: any) => Promise<void>
46+
}) {
3447
const [currentView, setCurrentView] = useState<Views>(Views.Details)
3548
const [reportUri, setReportUri] = useState<string>()
3649

37-
const {
38-
error,
39-
data: { repo, profile } = {},
40-
refetch,
41-
} = useQuery({
42-
queryKey: ['accountView', { id }],
43-
queryFn: async () => {
44-
const getRepo = async () => {
45-
let did
46-
if (id.startsWith('did:')) {
47-
did = id
48-
} else {
49-
const { data: resolved } =
50-
await client.api.com.atproto.handle.resolve({ handle: id })
51-
did = resolved.did
52-
}
53-
const { data: repo } = await client.api.com.atproto.admin.getRepo(
54-
{ did },
55-
{ headers: client.adminHeaders() },
56-
)
57-
return repo
58-
}
59-
const getProfile = async () => {
60-
try {
61-
const { data: profile } = await client.api.app.bsky.actor.getProfile({
62-
actor: id,
63-
})
64-
return profile
65-
} catch (err) {
66-
if (err?.['error'] === 'AccountTakedown') {
67-
return undefined
68-
}
69-
throw err
70-
}
71-
}
72-
const [repo, profile] = await Promise.all([getRepo(), getProfile()])
73-
return { repo, profile }
74-
},
75-
})
76-
7750
return (
7851
<div className="flex h-full bg-white">
7952
<ReportPanel
8053
open={!!reportUri}
8154
onClose={() => setReportUri(undefined)}
8255
subject={reportUri}
83-
onSubmit={async (vals) => {
84-
await createReport(vals)
85-
refetch()
86-
}}
56+
onSubmit={onSubmit}
8757
/>
8858
<div className="flex min-w-0 flex-1 flex-col overflow-hidden">
8959
<div className="relative z-0 flex flex-1 overflow-hidden">
@@ -475,18 +445,3 @@ function AccountsGrid({
475445
</div>
476446
)
477447
}
478-
479-
async function createReport(vals: ReportFormValues) {
480-
await client.api.com.atproto.report.create({
481-
...vals,
482-
subject: vals.subject.startsWith('at://')
483-
? {
484-
$type: 'com.atproto.repo.recordRef',
485-
uri: vals.subject,
486-
}
487-
: {
488-
$type: 'com.atproto.repo.repoRef',
489-
did: vals.subject,
490-
},
491-
})
492-
}

Diff for: components/repositories/createReport.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use client'
2+
import client from '../../lib/client'
3+
import { ReportFormValues } from '../reports/ReportPanel'
4+
5+
export async function createReport(vals: ReportFormValues) {
6+
await client.api.com.atproto.report.create({
7+
...vals,
8+
subject: vals.subject.startsWith('at://')
9+
? {
10+
$type: 'com.atproto.repo.recordRef',
11+
uri: vals.subject,
12+
}
13+
: {
14+
$type: 'com.atproto.repo.repoRef',
15+
did: vals.subject,
16+
},
17+
})
18+
}

0 commit comments

Comments
 (0)