Skip to content

Commit 378d6b6

Browse files
committed
refactor(ui): improve naming and typing of new snackbar implementation
1 parent b18eed3 commit 378d6b6

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

ui/src/components/Snackbar/SnackbarNew.vue

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
v-model="show"
44
location="top"
55
:timeout="4000"
6-
:color="type"
6+
:color="color"
77
transition="slide-x-transition"
88
>
99
{{ message }}
@@ -12,13 +12,14 @@
1212

1313
<script setup lang="ts">
1414
import { computed } from "vue";
15-
import { rawPlugin } from "@/plugins/snackbar";
15+
import { plugin } from "@/plugins/snackbar";
1616
1717
const show = computed({
18-
get: () => rawPlugin.state.show,
19-
set: (value) => rawPlugin.setShow(value),
18+
get: () => plugin.state.show,
19+
set: (value) => plugin.setShow(value),
2020
});
2121
22-
const message = computed(() => rawPlugin.state.message);
23-
const type = computed(() => rawPlugin.state.type);
22+
const message = computed(() => plugin.state.message);
23+
const type = computed(() => plugin.state.type);
24+
const color = computed(() => type.value ?? "info");
2425
</script>

ui/src/helpers/snackbar.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inject } from "vue";
2-
import { InjectionKey, type ISnackbarPlugin } from "@/plugins/snackbar";
2+
import { InjectionKey, type SnackbarPlugin } from "@/plugins/snackbar";
33

4-
const useSnackbar = (): ISnackbarPlugin => inject(InjectionKey) as ISnackbarPlugin;
4+
const useSnackbar = (): SnackbarPlugin => inject(InjectionKey) as SnackbarPlugin;
55

66
export default useSnackbar;

ui/src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import "asciinema-player/dist/bundle/asciinema-player.css";
1313
import { loadFonts } from "./plugins/webfontloader";
1414

1515
import SnackbarComponent from "./components/Snackbar/Snackbar.vue";
16-
import { SnackbarPlugin } from "./plugins/snackbar";
16+
import { SnackbarPluginProvider } from "./plugins/snackbar";
1717

1818
const app = createApp(App);
1919

@@ -58,6 +58,6 @@ if ((envVariables.isCloud) && (envVariables.chatWootWebsiteToken && envVariables
5858
);
5959
}
6060

61-
app.use(SnackbarPlugin);
61+
app.use(SnackbarPluginProvider);
6262
app.component("SnackbarComponent", SnackbarComponent);
6363
app.mount("#app");

ui/src/plugins/snackbar.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@ import { App, Plugin, reactive, readonly } from "vue";
22

33
const InjectionKey = Symbol("snackbar");
44

5+
type SnackbarType = "success" | "error" | "info" | "warning";
6+
57
interface SnackbarState {
68
message: string;
7-
type: "success" | "error" | "info" | "warning" | "";
9+
type: SnackbarType | null;
810
show: boolean;
911
}
1012

11-
interface ISnackbarPlugin {
13+
interface SnackbarPlugin {
1214
showSuccess: (msg: string) => void;
1315
showError: (msg: string) => void;
1416
showInfo: (msg: string) => void;
1517
showWarning: (msg: string) => void;
16-
hideSnackbar: () => void;
18+
setShow: (value: boolean) => void;
1719
state: Readonly<SnackbarState>;
1820
}
1921

2022
const state = reactive<SnackbarState>({
2123
message: "",
22-
type: "",
24+
type: null,
2325
show: false,
2426
});
2527

26-
const showSnackbar = (type: SnackbarState["type"], message: string) => {
28+
const showSnackbar = (type: SnackbarType, message: string) => {
2729
state.message = message;
2830
state.type = type;
2931
state.show = true;
@@ -33,7 +35,7 @@ const showSnackbar = (type: SnackbarState["type"], message: string) => {
3335
}, 4000);
3436
};
3537

36-
const plugin = {
38+
const plugin: SnackbarPlugin = {
3739
showSuccess: (msg: string) => showSnackbar("success", msg),
3840
showError: (msg: string) => showSnackbar("error", msg),
3941
showInfo: (msg: string) => showSnackbar("info", msg),
@@ -42,11 +44,11 @@ const plugin = {
4244
state: readonly(state),
4345
};
4446

45-
const SnackbarPlugin: Plugin = {
47+
const SnackbarPluginProvider: Plugin = {
4648
install(app: App) {
4749
app.provide(InjectionKey, plugin);
4850
},
4951
};
5052

51-
export { SnackbarPlugin, InjectionKey, plugin as rawPlugin };
52-
export type { SnackbarState, ISnackbarPlugin };
53+
export { SnackbarPluginProvider, InjectionKey, plugin };
54+
export type { SnackbarState, SnackbarPlugin };

0 commit comments

Comments
 (0)