Skip to content

Commit

Permalink
Merge pull request kodadot#5594 from kodadot/feat/set-prefix-to-local…
Browse files Browse the repository at this point in the history
…storage

fix: set prefix to localStorage
  • Loading branch information
yangwao committed Apr 12, 2023
2 parents 662272c + b283f53 commit ad289c4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 27 deletions.
3 changes: 2 additions & 1 deletion components/navbar/ChainSelectDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import { getChainNameByPrefix } from '@/utils/chain'
const { availableChains } = useChain()
const { $store } = useNuxtApp()
const { urlPrefix } = usePrefix()
const router = useRouter()
const selected = computed({
get: () => $store.getters.getSettings['urlPrefix'],
get: () => urlPrefix.value,
set: (value) => {
$store.dispatch('setUrlPrefix', value)
router.push({ path: `/${value}` })
Expand Down
5 changes: 3 additions & 2 deletions components/navbar/NavbarExploreOptions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
</template>

<script lang="ts" setup>
const { $router } = useNuxtApp()
const { $router, $store } = useNuxtApp()
const { urlPrefix } = usePrefix()
const { availableChains } = useChain()
const filteredChains = computed(() => {
return availableChains?.value.filter((chain) => {
return ['ksm', 'bsx', 'rmrk'].includes(chain.value)
return ['ksm', 'bsx', 'rmrk'].includes(chain.value as string)
})
})
const setSelectedChain = (value) => {
$store.dispatch('setUrlPrefix', value)
$router.push({ path: `/${value}/explore` })
}
</script>
15 changes: 13 additions & 2 deletions composables/usePrefix.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { DEFAULT_PREFIX } from '@kodadot1/static'
import { getKusamaAssetId } from '@/utils/api/bsx/query'
import type { Prefix } from '@kodadot1/static'

export default function () {
const { $store } = useNuxtApp()
const route = useRoute()
const storage = useLocalStorage('urlPrefix', { selected: DEFAULT_PREFIX })

const prefix = ref(route.params.prefix || $store.getters.currentUrlPrefix)
const urlPrefix = computed<string>(() => prefix.value || 'bsx')
const prefix = ref(
route.params.prefix ||
route.path.split('/')[1] ||
storage.value.selected ||
$store.getters.currentUrlPrefix
)
const urlPrefix = computed<Prefix>(() => {
storage.value = { selected: prefix.value }
return prefix.value
})

const client = computed<string>(() => {
return urlPrefix.value === 'rmrk' ? 'subsquid' : urlPrefix.value
Expand Down
2 changes: 2 additions & 0 deletions libs/static/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const CHAINS: Config<ChainProperties> = {
glmr: toChainProperty(1284, 18, 'GLMR', 'https://moonbeam.subscan.io/'),
}

export const DEFAULT_PREFIX: Prefix = 'bsx'

export const chainPrefixes: Prefix[] = [
'bsx',
'rmrk',
Expand Down
9 changes: 4 additions & 5 deletions middleware/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { chainPrefixes } from '@kodadot1/static'
export const rmrk2ChainPrefixesInHostname = ['rmrk2', 'rmrk']

export default function ({ store, route }): void {
const prefix = route.params.prefix || route.path.split('/')[1]
const chains = [...chainPrefixes]
const isAnyChainPrefixInPath = chains.some((prefix) =>
route.path.includes(prefix)
)
const { urlPrefix } = usePrefix()

const prefix = urlPrefix.value
const isAnyChainPrefixInPath = chainPrefixes.includes(prefix)
const rmrk2ChainPrefixInHostname = rmrk2ChainPrefixesInHostname.find(
(prefix) => location.hostname.startsWith(`${prefix}.`)
)
Expand Down
25 changes: 9 additions & 16 deletions pages/_prefix/explore/index.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
<script lang="ts">
import { Component, Vue } from 'nuxt-property-decorator'
<template>
<!-- empty template, to remove warning "[Vue warn]: Failed to mount component: template or render function not defined." -->
<div></div>
</template>

@Component({
components: {},
})
export default class LandingPage extends Vue {
layout() {
return 'full-width-layout'
}
middleware({ store, redirect }) {
// If the user is not authenticated
const prefix = store.getters.currentUrlPrefix
<script lang="ts" setup>
const router = useRouter()
setTimeout(() => redirect(`/${prefix}/explore/collectibles`))
}
}
onBeforeMount(() => {
router.replace(`${window.location.pathname}/collectibles`)
})
</script>
2 changes: 1 addition & 1 deletion plugins/vuex-persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import VuexPersistence from 'vuex-persist'
export default ({ store }): void => {
new VuexPersistence({
key: 'setting',
storage: window.sessionStorage,
storage: window.localStorage,
modules: ['identity'],
}).plugin(store)
}

0 comments on commit ad289c4

Please sign in to comment.