Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ui/sidebar): ensure compatibility with new client types #1008

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ui/artalk-sidebar/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare global {
const customRef: typeof import('vue')['customRef']
const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
const defineComponent: typeof import('vue')['defineComponent']
const defineLoader: typeof import('vue-router/auto')['defineLoader']
const definePage: typeof import('unplugin-vue-router/runtime')['definePage']
const effectScope: typeof import('vue')['effectScope']
const getCurrentInstance: typeof import('vue')['getCurrentInstance']
const getCurrentScope: typeof import('vue')['getCurrentScope']
Expand Down
36 changes: 17 additions & 19 deletions ui/artalk-sidebar/src/artalk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Artalk from 'artalk'
import Artalk, { type Context } from 'artalk'
import { Router } from 'vue-router'
import { bootParams } from './global'
import { useUserStore } from './stores/user'
Expand All @@ -16,22 +16,20 @@ export function setupArtalk() {
pageKey: bootParams.pageKey,
site: bootParams.site,
darkMode: bootParams.darkMode,
useBackendConf: true,
pvAdd: false,
remoteConfModifier: (conf) => {
conf.noComment = `<div class="atk-sidebar-no-content"></div>`
conf.flatMode = true
conf.pagination = {
pageSize: 20,
readMore: false,
autoLoad: false,
}
conf.listUnreadHighlight = true
noComment: `<div class="atk-sidebar-no-content"></div>`,
flatMode: true,
pagination: {
pageSize: 20,
readMore: false,
autoLoad: false,
},
listUnreadHighlight: true,
fetchCommentsOnInit: false,
})
}

export async function syncArtalkUser(artalk: Artalk, router: Router) {
export async function syncArtalkUser(artalk: Context, router: Router) {
const user = useUserStore()
const logout = () => {
user.logout()
Expand All @@ -43,7 +41,7 @@ export async function syncArtalkUser(artalk: Artalk, router: Router) {
// Access from open sidebar or directly by url
if (bootParams.user?.email) {
// sync user from sidebar to artalk
artalk.ctx.get('user').update(bootParams.user)
artalk.getUser().update(bootParams.user)
} else {
// Sync user from artalk to sidebar
try {
Expand All @@ -58,27 +56,27 @@ export async function syncArtalkUser(artalk: Artalk, router: Router) {
checkUser(artalk, logout)
}

function checkUser(artalk: Artalk, logout: () => void) {
function checkUser(artalk: Context, logout: () => void) {
// Get user info from artalk
const { name, email } = artalk.ctx.get('user').getData()
const { name, email } = artalk.getUser().getData()

// Remove login failed dialog if sidebar
artalk.ctx.getApiHandlers().remove('need_login')
artalk.ctx.getApiHandlers().add('need_login', async () => {
artalk.getApiHandlers().remove('need_login')
artalk.getApiHandlers().add('need_login', async () => {
logout()
throw new Error('Need login')
})

// Check user status
artalk.ctx
artalk
.getApi()
.user.getUserStatus({ email, name })
.then((res) => {
if (res.data.is_admin && !res.data.is_login) {
logout()
} else {
// Mark all notifications as read
artalk.ctx.getApi().notifies.markAllNotifyRead({ email, name })
artalk.getApi().notifies.markAllNotifyRead({ email, name })
}
})
}
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/components/PreferenceGrp.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import settings, { type OptionNode } from '../lib/settings'
import type { OptionNode } from '../lib/settings'

const props = defineProps<{
node: OptionNode
Expand Down
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/components/UserEditor.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { ArtalkType } from 'artalk'
import { artalk, bootParams } from '../global'
import { artalk } from '../global'

const { t } = useI18n()

Expand Down
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const artalkLoader = () =>
app.use(pinia)

// user sync from artalk to sidebar
await syncArtalkUser(artalk, router)
await syncArtalkUser(artalk.ctx, router)

app.mount('#app')
})()
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/pages/comments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ onMounted(() => {

artalk!.reload()

const $el = artalk!.ctx.get('list')!.$el
const $el = artalk!.ctx.inject('list').getEl()

$el.querySelector<HTMLElement>('.atk-list-header')!.style.display = 'none'
$el.querySelector<HTMLElement>('.atk-list-footer')!.style.display = 'none'
Expand Down
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function login(username?: string) {
password: userForm.value.password,
})
.then((res) => {
artalk.ctx.get('user').update({
artalk.ctx.getUser().update({
...res.data.user,
token: res.data.token,
})
Expand Down
2 changes: 1 addition & 1 deletion ui/artalk-sidebar/src/pages/users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { ArtalkType } from 'artalk'
import { storeToRefs } from 'pinia'
import { useNavStore } from '../stores/nav'
import { artalk, bootParams } from '../global'
import { artalk } from '../global'
import Pagination from '../components/Pagination.vue'

const nav = useNavStore()
Expand Down
4 changes: 2 additions & 2 deletions ui/artalk-sidebar/src/stores/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const useUserStore = defineStore('user', {
actions: {
logout() {
this.$reset()
getArtalk()?.ctx.get('user').logout()
getArtalk()?.ctx.getUser().logout()
},
sync() {
const user = getArtalk()?.ctx.get('user')
const user = getArtalk()?.ctx.getUser()
if (!user) throw new Error('Artalk is not initialized')
if (!user.checkHasBasicUserInfo()) throw new Error('User is not logged in')
this.$patch({ ...user.getData(), site: '' })
Expand Down
Loading