Skip to content

Commit 9303ee1

Browse files
catalog: add filter info to URL
fix ast-grep#689
1 parent ac275e5 commit 9303ee1

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

website/src/catalog/data.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { data as allRules } from '../../_data/catalog.data'
22
export type { RuleMeta } from '../../_data/catalog.data'
3+
import { atou, utoa } from '../utils'
34

45
export function intersect(a: string[], b: string[]) {
56
return a.some(x => b.includes(x))
@@ -84,4 +85,25 @@ export const features = [
8485
'transform',
8586
'constraints',
8687
'utils'
87-
]
88+
]
89+
90+
export function serialize(data: Filter): string {
91+
const allEmpty = Object.values(data).every(x => !x.length)
92+
if (allEmpty) {
93+
return ''
94+
}
95+
return utoa(JSON.stringify(data))
96+
}
97+
98+
export function deserialize(str: string): Filter {
99+
try {
100+
return JSON.parse(atou(str))
101+
} catch {
102+
return {
103+
selectedLanguages: [],
104+
selectedRules: [],
105+
selectedFeatures: [],
106+
selectedTypes: [],
107+
}
108+
}
109+
}

website/src/catalog/index.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<script setup lang="ts">
2-
import { ref } from 'vue'
2+
import { ref, watch } from 'vue'
33
import RuleFilter from './RuleFilter.vue'
44
import RuleList from './RuleList.vue'
55
import type { Filter } from './data';
6+
import { serialize, deserialize } from './data';
67
7-
const filter = ref<Filter>({
8-
selectedLanguages: [],
9-
selectedRules: [],
10-
selectedFeatures: [],
11-
selectedTypes: [],
8+
const filter = ref<Filter>(deserialize(
9+
location.hash.slice(1) || ''
10+
))
11+
12+
watch(filter, (value) => {
13+
const hash = serialize(value)
14+
history.replaceState({}, '', hash ? `#${hash}` : '/catalog')
15+
}, {
16+
deep: true,
17+
immediate: true,
1218
})
1319
1420
function reset() {

website/src/components/astGrep/state.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SupportedLang } from "./lang"
22
import { shallowReactive, toRefs, watch, provide } from 'vue'
33
import type { InjectionKey, ToRefs, ShallowReactive } from 'vue'
4+
import { utoa, atou } from '../../utils'
45

56
export enum Mode {
67
Patch = 'Patch',
@@ -18,16 +19,6 @@ export type State = {
1819
lang: SupportedLang,
1920
}
2021

21-
// prefer old unicode hacks for backward compatibility
22-
// https://base64.guru/developers/javascript/examples/unicode-strings
23-
function utoa(data: string): string {
24-
return btoa(unescape(encodeURIComponent(data)))
25-
}
26-
27-
function atou(base64: string): string {
28-
return decodeURIComponent(escape(atob(base64)))
29-
}
30-
3122
export function serialize(state: State): string {
3223
return utoa(JSON.stringify(state))
3324
}

website/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// prefer old unicode hacks for backward compatibility
2+
// https://base64.guru/developers/javascript/examples/unicode-strings
3+
export function utoa(data: string): string {
4+
return btoa(unescape(encodeURIComponent(data)))
5+
}
6+
7+
export function atou(base64: string): string {
8+
return decodeURIComponent(escape(atob(base64)))
9+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy