Skip to content

Commit a09f349

Browse files
authored
Merge pull request #213 from buggregator/issue/#212-add-default-project-support
Issue/#212 add default project support
2 parents c589703 + 815e4b8 commit a09f349

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

src/shared/lib/use-api-transport/use-api-transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const useApiTransport = () => {
6565
if (ctx.data?.event === 'event.received') {
6666
const event = ctx?.data?.data || null
6767

68-
if (event && event.project === project) {
68+
if (event && event.project === project.value) {
6969
eventsStore.addList([event]);
7070
}
7171
}

src/shared/stores/events/events-store.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const useEventsStore = defineStore("eventsStore", {
3333
lockedIds: getStoredLockedIds() || [],
3434
projects: {
3535
available: [] as TProjects['data'],
36-
activeKey: null as string | null,
36+
activeKey: undefined as string | undefined,
3737
}
3838
}),
3939
getters: {
@@ -56,12 +56,22 @@ export const useEventsStore = defineStore("eventsStore", {
5656
return Object.entries(cachedIds).filter(([_, value]) => value.length > 0).map(([key]) => key as TEventsGroup)
5757
},
5858
activeProjectKey: ({ projects }) => projects.activeKey,
59+
activeProject: ({ projects }) => {
60+
const storedProject = projects.available.find((proj) => proj.key === projects.activeKey)
61+
const defaultProject = projects.available.find((proj) => proj.is_default)
62+
63+
return storedProject || defaultProject || projects.available[0]
64+
},
5965
availableProjects: ({ projects }) => projects.available,
66+
isMultipleProjects: ({ projects }) => (
67+
projects.available.length > 1 ||
68+
!projects.available.some((proj) => proj.is_default)
69+
),
6070
},
6171
actions: {
6272
async initialize (): Promise<void> {
6373
const {api: { getProjects }} = useSettings();
64-
this.initActiveProject();
74+
this.initActiveProjectKey();
6575

6676
try {
6777
const { data } = await getProjects();
@@ -77,7 +87,7 @@ export const useEventsStore = defineStore("eventsStore", {
7787
this.events = events.slice(0, MAX_EVENTS_COUNT);
7888

7989
this.syncCachedWithActive(events.map(({ uuid }) => uuid));
80-
this.initActiveProject();
90+
this.initActiveProjectKey();
8191
},
8292
addList(events: ServerEvent<unknown>[]): void {
8393
events.forEach((event) => {
@@ -194,27 +204,28 @@ export const useEventsStore = defineStore("eventsStore", {
194204
setStoredLockedIds(this.lockedIds);
195205
},
196206
// projects
197-
initActiveProject() {
198-
this.projects.activeKey = getStoredProject();
207+
initActiveProjectKey() {
208+
this.projects.activeKey = getStoredProject() || this.activeProjectKey;
199209
},
200210
setAvailableProjects(projects: TProjects['data']) {
201211
if (projects.length > 0) {
202212
this.projects.available = projects;
203213

204-
if (this.projects.activeKey === null) {
205-
this.setActiveProject(projects[0].key);
214+
if (!this.projects.activeKey) {
215+
const defaultProject = projects.find((proj) => proj.is_default) || projects[0];
216+
this.setActiveProjectKey(defaultProject.key);
206217
}
207218
} else {
208-
this.resetActiveProject();
219+
this.resetActiveProjectKey();
209220
}
210221
},
211-
setActiveProject(project: string | null) {
222+
setActiveProjectKey(project: string) {
212223
this.projects.activeKey = project;
213224

214225
setStoredProject(project);
215226
},
216-
resetActiveProject() {
217-
this.projects.activeKey = null;
227+
resetActiveProjectKey() {
228+
this.projects.activeKey = undefined;
218229

219230
removeStoredProject();
220231
}

src/shared/stores/settings/settings-store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
getStoredActiveTheme,
99
setStoredEventsCountVisibility,
1010
setStoredFixedHeader,
11-
setStoredActiveTheme, getStoredPrimaryCodeEditor, setStoredPrimaryCodeEditor,
11+
setStoredActiveTheme,
12+
getStoredPrimaryCodeEditor,
13+
setStoredPrimaryCodeEditor,
1214
} from "./local-storage-actions";
1315

1416
export const useSettingsStore = defineStore("settingsStore", {

src/shared/types/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type TSettings = {
1616
export type TProjects = {
1717
data: {
1818
name: string,
19-
key: string
19+
key: string,
20+
is_default: boolean,
2021
}[]
2122
}

src/widgets/ui/layout-sidebar/layout-sidebar.vue

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import {
1212
useEventsStore,
1313
} from "~/src/shared/stores";
1414
import { useConnectionStore } from "~/src/shared/stores/connections";
15-
import type { TProjects } from "~/src/shared/types";
1615
import { BadgeNumber, IconSvg } from "~/src/shared/ui";
1716
import { version } from "../../../../package.json";
1817
import { EVENTS_LINKS_MAP, EVENTS_NAV_ORDER } from "./constants";
1918
2019
const { isConnectedWS } = storeToRefs(useConnectionStore());
2120
const { isVisibleEventCounts, auth } = storeToRefs(useSettingsStore());
2221
const eventsStore = useEventsStore();
23-
const { availableProjects, activeProjectKey } = storeToRefs(eventsStore);
22+
const { availableProjects, isMultipleProjects, activeProject } =
23+
storeToRefs(eventsStore);
2424
2525
const profileStore = useProfileStore();
2626
const { profile } = storeToRefs(profileStore);
@@ -35,6 +35,9 @@ const userMenu = ref<HTMLElement | null>(null);
3535
const isVisibleProfile = ref(false);
3636
const isVisibleProjects = ref(false);
3737
38+
// TODO: need to check why project is empty on first load
39+
const isProjectLoading = computed(() => !activeProject.value);
40+
3841
onClickOutside(projectMenu, () => {
3942
isVisibleProjects.value = false;
4043
});
@@ -93,14 +96,6 @@ const toggleProjects = () => {
9396
isVisibleProjects.value = !isVisibleProjects.value;
9497
};
9598
96-
const activeProject = computed(() => {
97-
const project = availableProjects.value.find(
98-
(p) => p.key === activeProjectKey.value,
99-
);
100-
101-
return project as unknown as TProjects["data"][number];
102-
});
103-
10499
const logout = () => {
105100
profileStore.removeToken();
106101
const router = useRouter();
@@ -123,15 +118,15 @@ const serverVersion = computed(() =>
123118
: `@${apiVersion.value}`,
124119
);
125120
126-
const setProject = (project: string) => {
127-
eventsStore.setActiveProject(project);
121+
const setProject = (projectKey: string) => {
122+
eventsStore.setActiveProjectKey(projectKey);
128123
129124
isVisibleProjects.value = false;
130125
};
131126
132-
const makeShortTitle = (title: string) => title.substring(0, 2);
127+
const makeShortTitle = (title: string) => (title || "").substring(0, 2);
133128
const generateRadialGradient = (input: string) =>
134-
`linear-gradient(to right, ${textToColors(input).join(", ")})`;
129+
`linear-gradient(to right, ${textToColors(input || "").join(", ")})`;
135130
</script>
136131

137132
<template>
@@ -146,7 +141,7 @@ const generateRadialGradient = (input: string) =>
146141
<IconSvg class="layout-sidebar__link-icon" name="logo-short" />
147142
</NuxtLink>
148143

149-
<template v-if="availableProjects.length > 0">
144+
<template v-if="!isProjectLoading && isMultipleProjects">
150145
<hr class="layout-sidebar__sep" />
151146

152147
<div class="layout-sidebar__projects">
@@ -171,7 +166,7 @@ const generateRadialGradient = (input: string) =>
171166
<hr class="layout-sidebar__sep" />
172167
</template>
173168

174-
<template v-if="!availableProjects.length">
169+
<template v-if="!isMultipleProjects || isProjectLoading">
175170
<NuxtLink to="/" title="Events" class="layout-sidebar__link">
176171
<IconSvg class="layout-sidebar__link-icon" name="events" />
177172
</NuxtLink>
@@ -230,7 +225,12 @@ const generateRadialGradient = (input: string) =>
230225
{{ makeShortTitle(project.name) }}
231226
</span>
232227

233-
{{ project.name }}
228+
<div class="layout-sidebar__dropdown-item-text">
229+
<span class="layout-sidebar__dropdown-item-text-key">
230+
{{ project.key }}
231+
</span>
232+
{{ project.name }}
233+
</div>
234234
</button>
235235
</div>
236236

@@ -354,7 +354,16 @@ const generateRadialGradient = (input: string) =>
354354
@apply text-2xs font-semibold uppercase;
355355
@apply h-6 md:h-8 w-7 md:w-8 rounded-lg;
356356
@apply flex items-center justify-center relative flex-shrink-0;
357-
@apply text-white dark:text-black;
357+
@apply text-white dark:text-black self-start;
358+
}
359+
360+
.layout-sidebar__dropdown-item-text {
361+
@apply flex flex-col;
362+
}
363+
364+
.layout-sidebar__dropdown-item-text-key {
365+
@apply text-2xs uppercase font-normal -mt-0.5;
366+
@apply text-gray-600 dark:text-gray-400;
358367
}
359368
360369
.layout-sidebar__dropdown {

0 commit comments

Comments
 (0)