refactor: update composable names

This commit is contained in:
Anthony Fu 2022-11-18 05:32:03 +08:00
parent 787a55bea7
commit 4fc6e405e0
9 changed files with 46 additions and 34 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
const { currentUser } = $(useAppStore()) const { currentUser } = $(useClientState())
const account = $computed(() => currentUser?.account) const account = $computed(() => currentUser?.account)
</script> </script>

View file

@ -1,9 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
const isLogin = useLoginState()
</script> </script>
<template> <template>
<div px6 py2 flex="~ col gap6" text-lg> <div px6 py2 flex="~ col gap6" text-lg>
<template v-if="isLogin">
<NuxtLink flex gap2 items-center to="/home" active-class="text-primary"> <NuxtLink flex gap2 items-center to="/home" active-class="text-primary">
<div i-ri:home-5-line /> <div i-ri:home-5-line />
<span>Home</span> <span>Home</span>
@ -12,6 +13,7 @@
<div i-ri:notification-4-line /> <div i-ri:notification-4-line />
<span>Notifications</span> <span>Notifications</span>
</NuxtLink> </NuxtLink>
</template>
<NuxtLink flex gap2 items-center to="/explore" active-class="text-primary"> <NuxtLink flex gap2 items-center to="/explore" active-class="text-primary">
<div i-ri:hashtag /> <div i-ri:hashtag />
<span>Explore</span> <span>Explore</span>
@ -24,6 +26,7 @@
<div i-ri:earth-line /> <div i-ri:earth-line />
<span>Federated</span> <span>Federated</span>
</NuxtLink> </NuxtLink>
<template v-if="isLogin">
<NuxtLink flex gap2 items-center active-class="text-primary"> <NuxtLink flex gap2 items-center active-class="text-primary">
<div i-ri:at-line /> <div i-ri:at-line />
<span>Messages</span> <span>Messages</span>
@ -36,5 +39,6 @@
<div i-ri:bookmark-line /> <div i-ri:bookmark-line />
<span>Bookmarks</span> <span>Bookmarks</span>
</NuxtLink> </NuxtLink>
</template>
</div> </div>
</template> </template>

View file

@ -1,10 +1,10 @@
import type { MastoClient } from 'masto' import type { MastoClient } from 'masto'
import type { AppStore } from '~~/plugins/store.client' import type { ClientState } from '~/plugins/store.client'
export function useMasto() { export function useMasto() {
return inject('masto') as Promise<MastoClient> return useNuxtApp().$masto as Promise<MastoClient>
} }
export function useAppStore() { export function useClientState() {
return inject('app-store') as AppStore return useNuxtApp().$clientState as ClientState
} }

View file

@ -9,3 +9,8 @@ export function useAppCookies() {
token, token,
} }
} }
export function useLoginState() {
const token = useCookie('nuxtodon-token')
return computed(() => !!token.value)
}

View file

@ -1,6 +1,6 @@
<template> <template>
<div h-full of-hidden> <div h-full of-hidden>
<main grid="~ lg:cols-[1fr_40rem_1fr]" max-w-70rem mxa h-full> <main grid="~ lg:cols-[1fr_40rem_1fr]" max-w-80rem mxa h-full>
<div> <div>
<slot name="left"> <slot name="left">
<AccountMe /> <AccountMe />

View file

@ -1,6 +1,6 @@
{ {
"private": true,
"type": "module", "type": "module",
"private": true,
"packageManager": "pnpm@7.9.0", "packageManager": "pnpm@7.9.0",
"scripts": { "scripts": {
"build": "nuxi build", "build": "nuxi build",

View file

@ -2,7 +2,7 @@
const { query } = useRoute() const { query } = useRoute()
onMounted(async () => { onMounted(async () => {
const { login } = useAppStore() const { login } = useClientState()
await login(query as any) await login(query as any)
await nextTick() await nextTick()
await nextTick() await nextTick()

View file

@ -5,7 +5,8 @@ export default defineNuxtPlugin((nuxt) => {
const masto = login({ const masto = login({
url: `https://${server.value}`, url: `https://${server.value}`,
accessToken: token.value, accessToken: token.value || undefined,
}) })
nuxt.vueApp.provide('masto', masto)
nuxt.$masto = masto
}) })

View file

@ -1,10 +1,12 @@
import { login as loginMasto } from 'masto' import { login as loginMasto } from 'masto'
import type { UserLogin } from '~/types' import type { UserLogin } from '~/types'
function createStore() { function createClientState() {
const { server, token } = useAppCookies() const { server, token } = useAppCookies()
const accounts = useLocalStorage<UserLogin[]>('nuxtodon-accounts', [], { deep: true }) const accounts = useLocalStorage<UserLogin[]>('nuxtodon-accounts', [], { deep: true })
const currentId = useLocalStorage<string>('nuxtodon-current-user', '') const currentId = useLocalStorage<string>('nuxtodon-current-user', '')
const currentUser = computed<UserLogin | undefined>(() => { const currentUser = computed<UserLogin | undefined>(() => {
let user: UserLogin | undefined let user: UserLogin | undefined
if (currentId.value) { if (currentId.value) {
@ -48,8 +50,8 @@ function createStore() {
} }
} }
export type AppStore = ReturnType<typeof createStore> export type ClientState = ReturnType<typeof createClientState>
export default defineNuxtPlugin((nuxt) => { export default defineNuxtPlugin((nuxt) => {
nuxt.vueApp.provide('app-store', createStore()) nuxt.$clientState = createClientState()
}) })