-
Notifications
You must be signed in to change notification settings - Fork 205
Feat: multi-terminology support #2413
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
base: 8.x
Are you sure you want to change the base?
Changes from 23 commits
64e5a79
fa714ae
32077e3
337af8f
e02c4e2
bd8c9d2
a56be7a
a8bdb86
372b110
39e9783
2efea53
3287d5b
bf11f29
5a80c06
62cd183
2d97852
dbe1fbb
92716d2
029c57d
acaff6f
a6d6223
4e0649f
1a81cb8
b1988cc
f637513
6e783c2
f7f3abc
93accb9
b620823
0c3d29a
32bf16b
04a1b2e
83d1310
9555ef5
a9f551e
754c1ad
9423fc9
2f91d97
25a1432
63668b2
7bd46f4
e93999c
731209d
da7305b
2ed1d8c
81c4568
6b3409d
e2e208f
e662265
8a708ce
8b25d4f
c7fb232
6930968
d174af8
2632b0a
2920ccf
5a92fbe
6238502
0cbd9ef
973c576
c08225c
e0836fb
4d440b2
345acee
570316d
fcd523b
81c53a4
f1d40ae
8946e7f
667cc6c
553587e
273488f
a5c9df2
9e8c3e2
7407bbd
0a07c0a
e0d881d
bab1c5a
08989b7
ca24b61
2b20b68
5b141ec
bd669c3
ceef3d7
78f1950
9cbf8d2
1ebb7c4
586da02
6abce07
523d3d7
86ec87d
b352dcb
f230946
ae4ed9c
1c61fde
4b6e174
1a0ea48
d58bc8f
cb856cb
751b3cd
b354f45
a9385fb
abbe7e1
4a95f3a
e2c8613
4371c8f
b592255
c51cbe9
4feb151
237ae93
5ccfd08
08508ab
b175e08
4cc00c9
7d24475
6f76fbc
d4a56ad
7a00fac
07cfb72
3ce9054
71cad9b
c09e82d
8505391
6b29e04
4ec08c1
75992fd
ff4ff0c
90fb24e
75d7523
c1f0d2e
0fa57b3
277e06a
2d4ecb1
707019a
e6f8578
59f7152
fc11a90
7d10c30
f6b29f2
fe7fdeb
b3b19df
3b172e7
e977a00
fa9c9d4
ea3216d
e79bffb
f7a5bfc
4f386f6
20874f5
20e9ef9
93708e7
4531063
d3bd71e
2653a93
97afec4
5eac7d7
1ab59eb
76f4ec1
231613a
1b7299e
6344127
71db5d6
3cd0f12
38cf37d
4f19450
2803000
577410f
44898e5
1503e21
4e07f21
a499ae5
c278a79
04a7ef0
6c838e3
978bd4d
e26e9bb
55ad9a6
e714a46
eabf5c0
a56bf0b
82bc4a5
558f921
cd7ee26
9de0afb
e7eaad6
09651e0
a80a211
557a0cd
f54f660
b92b94f
212d2ce
c09eb5d
7269eff
c4d7d64
9f05f73
ba54f8d
4059ee3
f455327
454cd7b
94e07ac
da3dfcf
0802cfb
1a01859
0ba7a6b
b1976e0
88df9e9
569b43e
957617a
7c08691
a1a95c7
bc5837c
02e15a7
952cf85
900ea90
849ced0
8838922
a2667f8
3a2bdcd
b178461
c0f5b62
2271b9a
51eb30b
721f7d3
59d4a4b
2c8e4e7
87854c5
580e5bf
e57444f
d913262
1fabaa5
6173cb1
b18a769
66dafc6
6cac26f
3735257
4f4855a
4af5e83
72ddec4
c2c9a40
cd161af
5d0fe28
4425d99
e446f7f
b10a000
4d7cb51
442e16f
baf47f8
220dc97
1eea12f
26871a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { resolve } from '$app/paths'; | ||
| import { goto } from '$app/navigation'; | ||
| import type { Pathname, RouteId, RouteParams } from '$app/types'; | ||
|
|
||
| // taken directly from svelte's source! | ||
| type ResolveArgs<T extends RouteId | Pathname> = T extends RouteId | ||
| ? RouteParams<T> extends Record<string, never> | ||
| ? [route: T] | ||
| : [route: T, params: RouteParams<T>] | ||
| : [route: T]; | ||
|
|
||
| export function withPath(base: string, ...parts: string[]) { | ||
| return [base.replace(/\/+$/, ''), ...parts].join('/'); | ||
| } | ||
|
|
||
| export function resolveRoute<T extends RouteId>(route: T, params?: Record<string, string>) { | ||
| // type cast is necessary here! | ||
| const resolveArgs = params ? ([route, params] as [T, RouteParams<T>]) : [route]; | ||
|
|
||
| return resolve(...(resolveArgs as ResolveArgs<T>)); | ||
| } | ||
ItzNotABug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function navigate<T extends RouteId>( | ||
| route: T, | ||
| params?: Record<string, string> | ||
| ): Promise<void> { | ||
| // type cast is necessary here! | ||
| return goto(resolveRoute(route, params)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { Page } from '@sveltejs/kit'; | ||
|
|
||
| import { useTerminology } from './terminology'; | ||
| import { Submit, Click } from '$lib/actions/analytics'; | ||
| import type { AnalyticsResult, TerminologyResult, TerminologyShape } from './types'; | ||
|
|
||
| export function useAnalytics(pageOrTerms: Page | TerminologyResult): AnalyticsResult { | ||
| // source is in `TerminologyResult`. | ||
| const terminology = 'source' in pageOrTerms ? pageOrTerms : useTerminology(pageOrTerms); | ||
|
|
||
| const createSubmitHandler = <TAction extends string>(termType: keyof TerminologyShape) => { | ||
| return (action: TAction) => { | ||
| const term = terminology.source[termType]; | ||
| if (!term) { | ||
| throw new Error(`No ${termType} terminology found`); | ||
| } | ||
| const enumKey = `${term.title.singular}${action}`; | ||
| return Submit[enumKey as keyof typeof Submit]; | ||
abnegate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| }; | ||
|
|
||
| const createClickHandler = <TAction extends string>(termType: keyof TerminologyShape) => { | ||
| return (action: TAction) => { | ||
| const term = terminology.source[termType]; | ||
| if (!term) { | ||
| throw new Error(`No ${termType} terminology found`); | ||
| } | ||
| const enumKey = `Database${term.title.singular}${action}`; | ||
| return Click[enumKey as keyof typeof Click]; | ||
abnegate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| }; | ||
|
|
||
| const result: AnalyticsResult = { submit: {}, click: {} }; | ||
|
|
||
| if (terminology.entity) { | ||
| result.click.entity = createClickHandler('entity'); | ||
| result.submit.entity = createSubmitHandler('entity'); | ||
| } | ||
|
|
||
| if (terminology.field) { | ||
| result.click.field = createClickHandler('field'); | ||
| result.submit.field = createSubmitHandler('field'); | ||
| } | ||
|
|
||
| if (terminology.record) { | ||
| result.click.record = createClickHandler('record'); | ||
| result.submit.record = createSubmitHandler('record'); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { Page } from '@sveltejs/kit'; | ||
|
|
||
| import { useTerminology } from './terminology'; | ||
| import { Dependencies } from '$lib/constants'; | ||
| import type { DependenciesResult, Term, TerminologyResult } from './types'; | ||
|
|
||
| export function useDependencies(pageOrTerms: Page | TerminologyResult): DependenciesResult { | ||
| // source is in `TerminologyResult`. | ||
| const terminology = 'source' in pageOrTerms ? pageOrTerms : useTerminology(pageOrTerms); | ||
|
|
||
| const getDependencies = (term: { title: Term }) => ({ | ||
| singular: Dependencies[term.title.singular.toUpperCase() as keyof typeof Dependencies], | ||
| plural: Dependencies[term.title.plural.toUpperCase() as keyof typeof Dependencies] | ||
| }); | ||
|
|
||
| return { | ||
| entity: terminology.entity ? getDependencies(terminology.entity) : undefined, | ||
| field: terminology.field ? getDependencies(terminology.field) : undefined, | ||
| record: terminology.record ? getDependencies(terminology.record) : undefined | ||
| }; | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be in a "helpers" context? Can't it just be defined for all entities
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would just mix up the logic with stranded TS files while the views has its own directory structure. Mainly for organizing TS ops in a specific directory so its easier to manage. Maybe |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './init'; | ||
| export * from './types'; | ||
| export * from './analytics'; | ||
| export * from './terminology'; | ||
| export * from './dependencies'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { Page } from '@sveltejs/kit'; | ||
| import { getContext, setContext } from 'svelte'; | ||
| import { | ||
| type AnalyticsResult, | ||
| type DependenciesResult, | ||
| type TerminologyResult, | ||
| useAnalytics, | ||
| useDependencies, | ||
| useTerminology | ||
| } from '$database/(entity)'; | ||
|
|
||
| const TERMINOLOGIES_KEY = Symbol('terminologies'); | ||
|
|
||
| export type Terminologies = { | ||
| analytics: AnalyticsResult; | ||
| terminology: TerminologyResult; | ||
| dependencies: DependenciesResult; | ||
| }; | ||
|
|
||
| export function getTerminologies(): Terminologies { | ||
| return getContext<Terminologies>(TERMINOLOGIES_KEY); | ||
| } | ||
|
|
||
| export function setTerminologies(page: Page) { | ||
| setContext(TERMINOLOGIES_KEY, buildTerminologies(page)); | ||
| } | ||
|
|
||
| function buildTerminologies(page: Page) { | ||
| const terminology = useTerminology(page); | ||
| return { | ||
| terminology, | ||
| analytics: useAnalytics(terminology), | ||
| dependencies: useDependencies(terminology) | ||
| }; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.