Skip to content

Commit

Permalink
update ui-script: add sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
cw1997 committed Dec 8, 2022
1 parent bcb10ec commit 4f71618
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/ui-scripts/src/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {stringify} from 'qs'
let ACCOUNTS_BASE = '/_/sso'
let BLOG_BASE = '/_/blog'
let ASKTUG_BASE = ''
let HOME_BASE = 'https://tidb.net'

declare global {
interface Window {
Expand Down Expand Up @@ -54,6 +55,8 @@ const accounts: Fetcher = (key: string) => {
switch (key) {
case 'me':
return fetch(`${ACCOUNTS_BASE}${meUrl}`, { credentials: 'include' }).then(processResponse)
case 'accounts.points.me':
return fetch(`${ACCOUNTS_BASE}/api/points/me`, { credentials: 'include' }).then(processResponse)
case 'accounts.points.top':
return fetch(`${ACCOUNTS_BASE}/api/points/top`, { credentials: 'include' }).then(processResponse)
default:
Expand Down Expand Up @@ -96,6 +99,12 @@ const asktug: Fetcher = (key: string, params: any) => {
case 'asktug.site':
// @ts-ignore
return fetch(`${ASKTUG_BASE}/site.json`, { headers: { accept: 'application/json' }, credentials: 'include' }).then(processResponse)
case 'asktug.badges':
// @ts-ignore
return fetch(`${ASKTUG_BASE}/badges.json`, { headers: { accept: 'application/json' }, credentials: 'include' }).then(processResponse)
case 'asktug.user.summary':
// @ts-ignore
return fetch(`${ASKTUG_BASE}/u/${params.username}/summary`, { headers: { accept: 'application/json' }, credentials: 'include' }).then(processResponse)
default:
throw new Error('not implemented')
}
Expand All @@ -114,6 +123,20 @@ const blog: Fetcher = (key: string, params: any) => {
return fetch(`${BLOG_BASE}/api/notifications/${params}/read`, { method: 'PATCH', credentials: 'include' })
case 'blog.getRecommend':
return fetch(`${BLOG_BASE}/api/posts/recommend`, {})
case 'blog.users.posts':
return fetch(`${BLOG_BASE}/blog/api/users/username/${params.username}/posts`, {})
default:
throw new Error('not implemented')
}
}

const home: Fetcher = (key: string, params: any) => {
if (typeof params === 'string') {
params = tryJson(params)
}
switch (key) {
case 'home.events':
return fetch(`${HOME_BASE}/next-api/cms/tidbio-homepage-main-activities`, {})
default:
throw new Error('not implemented')
}
Expand All @@ -129,5 +152,5 @@ const getAsktugCsrf = () => {
}

export default {
accounts, asktug, blog
accounts, asktug, blog, home
}
10 changes: 10 additions & 0 deletions packages/ui-scripts/src/index.asktug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Env,
Footer,
Header,
Sidebar,
Site,
SiteComponentsContext
} from '@pingcap-inc/tidb-community-site-components'
Expand Down Expand Up @@ -46,6 +47,14 @@ const context = {
fetchers,
}

const AsktugSidebar = () => {
const element = document.getElementById('asktug-sidebar')
if (!element) {
return null
}
return ReactDOM.createPortal(<Sidebar/>, element)
}

const AsktugHeader = () => {
const headerElem = document.getElementById('asktug-header')
if (!headerElem) {
Expand Down Expand Up @@ -95,6 +104,7 @@ const AsktugSite = () => {
<SiteComponentsContext.Provider value={context}>
<AsktugHeader/>
<AsktugFooter/>
<AsktugSidebar/>
</SiteComponentsContext.Provider>
)
}
Expand Down

0 comments on commit 4f71618

Please sign in to comment.