Skip to content
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

move declarations to bundle #3138

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
09a31d8
add bundle.declarations
samuelstroschein Sep 20, 2024
5369b03
fix: foreign key constraints
samuelstroschein Sep 20, 2024
733904e
disable foreign keys to avoid updating tests. (do foreign keys as sep…
samuelstroschein Sep 20, 2024
92115e3
refactor: add `bundle.declarations` and remove `message.declarations`
samuelstroschein Sep 20, 2024
84aacfa
Merge commit '21dbd8f95d355b006d2179eee66469850c8b0d2e' into mesdk-24…
samuelstroschein Sep 20, 2024
a996033
chore: remove unused dependency
samuelstroschein Sep 20, 2024
b6b8ce5
test: update bundle type
samuelstroschein Sep 20, 2024
4fcf4a4
chore: remove unused dependency
samuelstroschein Sep 20, 2024
496dcf3
fix: types can't be undefined
samuelstroschein Sep 20, 2024
437527a
refactor: move declarations to bundle level
samuelstroschein Sep 20, 2024
ba651b7
Merge pull request #3139 from opral/update-bundle-component
samuelstroschein Sep 20, 2024
0ca65e1
expose ChangeEventProps
samuelstroschein Sep 20, 2024
5d4e04a
refactor: move declarations to bundle
samuelstroschein Sep 20, 2024
0edbfc8
refactor: use `variable-reference`
samuelstroschein Sep 20, 2024
3b99b04
expose change event detail
samuelstroschein Sep 20, 2024
0b67023
chore: remove unused import
samuelstroschein Sep 20, 2024
ca57ecc
refactor: move declaration to bundle level
samuelstroschein Sep 20, 2024
c9ffb63
refactor: remove `message.declarations`
samuelstroschein Sep 20, 2024
835d6b9
chore: mark helper functions as deprecated
samuelstroschein Sep 20, 2024
32945ea
chore: remove declarations
samuelstroschein Sep 21, 2024
45b83c2
refactor: don't use `createMessage`
samuelstroschein Sep 21, 2024
953353e
chore: mark function as deprecated
samuelstroschein Sep 21, 2024
0fcbadd
chore: add type checking to test script
samuelstroschein Sep 21, 2024
882b3ff
refactor: use `variable-reference`
samuelstroschein Sep 21, 2024
9b72a31
refactor: move declarations to bundle level
samuelstroschein Sep 21, 2024
1bee589
Merge branch 'lix-integration' into mesdk-249-add-bundledeclarations-…
samuelstroschein Sep 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions inlang/source-code/bundle-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
"@storybook/web-components-vite": "^7.6.16",
"@types/chroma-js": "^2.4.4",
"@vitest/coverage-v8": "^0.33.0",
"i": "^0.3.7",
"lit": "^3.1.2",
"npm": "^10.8.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rollup": "3.29.1",
Expand Down
28 changes: 14 additions & 14 deletions inlang/source-code/bundle-component/src/helper/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

import type { Bundle, Message, Variant } from "@inlang/sdk2"

export type DispatchChangeInterface = {
type: "Bundle" | "Message" | "Variant"
operation: "create" | "update" | "delete"
newData: Bundle | Message | Variant | undefined
meta?: Record<string, any>
/**
* This event is dispatched when a change is made to a bundle, message or variant.
*
* - `entityId` is the id of the entity that was changed.
* - `entity` is the type of entity that was changed.
* - `newData` is the new data of the entity.
* - `newData` is `undefined` if the entity was deleted.
*/
export type ChangeEventDetail = {
entityId: string
entity: "bundle" | "message" | "variant"
newData?: Bundle | Message | Variant
}

export const createChangeEvent = (props: DispatchChangeInterface) => {
export const createChangeEvent = (detail: ChangeEventDetail) => {
const onChangeEvent = new CustomEvent("change", {
bubbles: true,
composed: true,
detail: {
argument: {
type: props.type,
operation: props.operation,
newData: props.newData,
meta: props.meta,
},
},
detail,
})
return onChangeEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("patternToString", () => {
{
type: "expression",
arg: {
type: "variable",
type: "variable-reference",
name: "name",
},
},
Expand All @@ -24,7 +24,7 @@ describe("patternToString", () => {

const text = patternToString({ pattern })

const correspondingText = "Hello, {{name}}!"
const correspondingText = "Hello, {name}!"

expect(text).toStrictEqual(correspondingText)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Pattern } from "@inlang/sdk2"
* @returns The pattern as a string.
*
* @example
* patternToString({ pattern: [{ value: "Hello" }, { type: "expression", arg: { type: "variable", name: "name" } }] }) -> "Hello {{name}}"
* patternToString({ pattern: [{ value: "Hello" }, { type: "expression", arg: { type: "variable-reference", name: "name" } }] }) -> "Hello {name}"
*/

const patternToString = (props: { pattern: Pattern }): string => {
Expand All @@ -19,10 +19,8 @@ const patternToString = (props: { pattern: Pattern }): string => {
.map((p) => {
if ("value" in p) {
return p.value
// @ts-ignore
} else if (p.type === "expression" && p.arg.type === "variable") {
// @ts-ignore
return `{{${p.arg.name}}}`
} else if (p.type === "expression" && p.arg.type === "variable-reference") {
return `{${p.arg.name}}`
}
return ""
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Pattern } from "@inlang/sdk2"

describe("stringToPattern", () => {
it("Should transform string to pattern", () => {
const text = "Hello, {{name}}!"
const text = "Hello, {name}!"

const pattern = stringToPattern({ text })

Expand All @@ -16,7 +16,7 @@ describe("stringToPattern", () => {
{
type: "expression",
arg: {
type: "variable",
type: "variable-reference",
name: "name",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { Pattern } from "@inlang/sdk2"
* @returns The pattern of the text.
*
* @example
* stringToPattern({ text: "Hello {{name}}" }) -> [{ value: "Hello" }, { type: "expression", arg: { type: "variable", name: "name" } }]
* stringToPattern({ text: "Hello {name}" }) -> [{ value: "Hello" }, { type: "expression", arg: { type: "variable-reference", name: "name" } }]
*/

const stringToPattern = (props: { text: string }): Pattern => {
const pattern: Pattern = []
const regex = /{{(.*?)}}/g
const regex = /{(.*?)}/g
let lastIndex = 0
let match

Expand All @@ -30,7 +30,7 @@ const stringToPattern = (props: { text: string }): Pattern => {
pattern.push({
type: "expression",
arg: {
type: "variable",
type: "variable-reference",
name: match[1],
},
})
Expand Down
3 changes: 3 additions & 0 deletions inlang/source-code/bundle-component/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export { default as InlangPatternEditor } from "./stories/pattern-editor/inlang-
//modals & actions
export { default as InlangBundleAction } from "./stories/actions/bundle-action/inlang-bundle-action.js"
export { default as InlangAddSelector } from "./stories/actions/add-selector/inlang-add-selector.js"

// types
export type { ChangeEventDetail } from "./helper/event.js"
50 changes: 26 additions & 24 deletions inlang/source-code/bundle-component/src/mock/messageBundle.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import type { BundleNested } from "@inlang/sdk2"
import type { Bundle, Message, Variant } from "@inlang/sdk2"

export const bundleWithoutSelectors: BundleNested = {
id: "message-bundle-id",
export const exampleWithoutSelectors: {
bundles: Bundle[]
messages: Message[]
variants: Variant[]
} = {
bundles: [
{
id: "message-bundle-id",
declarations: [],
},
],
messages: [
{
bundleId: "message-bundle-id",
id: "message-id-en",
locale: "en",
selectors: [],
declarations: [],
variants: [
{
messageId: "message-id-en",
id: "variant-id-en-*",
match: {},
pattern: [{ type: "text", value: "{count} new messages" }],
},
],
},
{
bundleId: "message-bundle-id",
id: "message-id-de",
locale: "de",
selectors: [],
declarations: [],
variants: [
{
messageId: "message-id-de",
id: "variant-id-de-*",
match: {},
pattern: [{ type: "text", value: "{count} neue Nachrichten" }],
},
],
},
],
// default: "frontend_button_text",
// ios: "frontendButtonText",
// },
variants: [
{
messageId: "message-id-en",
id: "variant-id-en-*",
match: {},
pattern: [{ type: "text", value: "{count} new messages" }],
},
{
messageId: "message-id-de",
id: "variant-id-de-*",
match: {},
pattern: [{ type: "text", value: "{count} neue Nachrichten" }],
},
],
}
149 changes: 149 additions & 0 deletions inlang/source-code/bundle-component/src/mock/pluralBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { Bundle, Message, Variant } from "@inlang/sdk2"

export const examplePlural: {
bundles: Bundle[]
messages: Message[]
variants: Variant[]
} = {
bundles: [
{
id: "mock_bundle_human_id",
declarations: [
{
type: "input-variable",
name: "numProducts",
},
{
type: "input-variable",
name: "count",
},
{
type: "input-variable",
name: "projectCount",
},
],
},
],
messages: [
{
bundleId: "mock_bundle_human_id",
id: "mock_message_id_de",
locale: "de",
selectors: [
{
type: "expression",
arg: {
type: "variable-reference",
name: "numProducts",
},
annotation: {
type: "function-reference",
name: "plural",
options: [],
},
},
],
},
{
bundleId: "mock_bundle_human_id",
id: "mock_message_id_en",
locale: "en",
selectors: [
{
type: "expression",
arg: {
type: "variable-reference",
name: "numProducts",
},
annotation: {
type: "function-reference",
name: "plural",
options: [],
},
},
],
},
],
variants: [
{
messageId: "mock_message_id_de",
id: "mock_variant_id_de_zero",
match: { numProducts: "zero" },
pattern: [
{
type: "text",
value: "Keine Produkte",
},
],
},
{
messageId: "mock_message_id_de",
id: "mock_variant_id_de_one",
match: { numProducts: "one" },
pattern: [
{
type: "text",
value: "Ein Produkt",
},
],
},
{
messageId: "mock_message_id_de",
id: "mock_variant_id_de_other",
match: { numProducts: "other" },
pattern: [
{
type: "expression",
arg: {
type: "variable-reference",
name: "numProducts",
},
},
{
type: "text",
value: " Produkte",
},
],
},
{
messageId: "mock_message_id_en",
id: "mock_variant_id_en_zero",
match: { numProducts: "zero" },
pattern: [
{
type: "text",
value: "No Products",
},
],
},
{
messageId: "mock_message_id_en",
id: "mock_variant_id_en_one",
match: { numProducts: "one" },
pattern: [
{
type: "text",
value: "A product",
},
],
},
{
messageId: "mock_message_id_en",
id: "mock_variant_id_en_other",
match: { numProducts: "other" },
pattern: [
{
type: "expression",
arg: {
type: "variable-reference",
name: "numProducts",
},
},
{
type: "text",
value: " products",
},
],
},
],
}
36 changes: 36 additions & 0 deletions inlang/source-code/bundle-component/src/mock/updateEntities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Bundle, Message, Variant } from "@inlang/sdk2"
import type { ChangeEventDetail } from "../helper/event.js"

export const updateEntities = (args: {
entities: {
bundles: Bundle[]
messages: Message[]
variants: Variant[]
}
change: ChangeEventDetail
}) => {
const newEntities = structuredClone(args.entities)
const entity = (args.change.entity + "s") as "bundles" | "messages" | "variants"

// deletion
if (args.change.newData === undefined) {
newEntities[entity] = newEntities[entity].filter(
(entity: any) => entity.id !== args.change.entityId
) as any[]
}
// update
newEntities[entity] = newEntities[entity].map((entity: any) => {
// replace the entity with the new data
if (entity.id === args.change.newData?.id) {
return args.change.newData
}
// return the entity if it is not the one to be updated
return entity
})
// insert
if (!newEntities[entity].some((entity: any) => entity.id === args.change.newData?.id)) {
newEntities[entity].push(args.change.newData as any)
}
console.info(args.change, args.change.newData, newEntities)
return newEntities
}
Loading
Loading