refactor: update composable names

zio/stable
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,17 +1,19 @@
<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>
<NuxtLink flex gap2 items-center to="/home" active-class="text-primary"> <template v-if="isLogin">
<div i-ri:home-5-line /> <NuxtLink flex gap2 items-center to="/home" active-class="text-primary">
<span>Home</span> <div i-ri:home-5-line />
</NuxtLink> <span>Home</span>
<NuxtLink flex gap2 items-center to="/notifications" active-class="text-primary"> </NuxtLink>
<div i-ri:notification-4-line /> <NuxtLink flex gap2 items-center to="/notifications" active-class="text-primary">
<span>Notifications</span> <div i-ri:notification-4-line />
</NuxtLink> <span>Notifications</span>
</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,17 +26,19 @@
<div i-ri:earth-line /> <div i-ri:earth-line />
<span>Federated</span> <span>Federated</span>
</NuxtLink> </NuxtLink>
<NuxtLink flex gap2 items-center active-class="text-primary"> <template v-if="isLogin">
<div i-ri:at-line /> <NuxtLink flex gap2 items-center active-class="text-primary">
<span>Messages</span> <div i-ri:at-line />
</NuxtLink> <span>Messages</span>
<NuxtLink flex gap2 items-center to="/favourites" active-class="text-primary"> </NuxtLink>
<div i-ri:heart-3-line /> <NuxtLink flex gap2 items-center to="/favourites" active-class="text-primary">
<span>Favorites</span> <div i-ri:heart-3-line />
</NuxtLink> <span>Favorites</span>
<NuxtLink flex gap2 items-center to="/bookmarks" active-class="text-primary"> </NuxtLink>
<div i-ri:bookmark-line /> <NuxtLink flex gap2 items-center to="/bookmarks" active-class="text-primary">
<span>Bookmarks</span> <div i-ri:bookmark-line />
</NuxtLink> <span>Bookmarks</span>
</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()
}) })