2023-05-28 23:13:24 +02:00
|
|
|
import { type MaybeRef, get, useStorage } from '@vueuse/core';
|
2022-12-17 01:30:02 +01:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import type { Ref } from 'vue';
|
2023-11-01 15:38:19 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
import type { Tool, ToolCategory, ToolWithCategory } from './tools.types';
|
2023-05-28 23:13:24 +02:00
|
|
|
import { toolsWithCategory } from './index';
|
2022-12-17 01:30:02 +01:00
|
|
|
|
2023-11-01 15:38:19 +01:00
|
|
|
export const useToolStore = defineStore('tools', () => {
|
|
|
|
const favoriteToolsName = useStorage('favoriteToolsName', []) as Ref<string[]>;
|
|
|
|
const { t } = useI18n();
|
2022-12-17 01:30:02 +01:00
|
|
|
|
2023-11-01 15:38:19 +01:00
|
|
|
const tools = computed<ToolWithCategory[]>(() => toolsWithCategory.map((tool) => {
|
|
|
|
const toolI18nKey = tool.path.replace(/\//g, '');
|
2022-12-17 01:30:02 +01:00
|
|
|
|
2023-11-01 15:38:19 +01:00
|
|
|
return ({
|
|
|
|
...tool,
|
2024-10-22 10:21:29 +02:00
|
|
|
path: tool.path,
|
2023-11-01 15:38:19 +01:00
|
|
|
name: t(`tools.${toolI18nKey}.title`, tool.name),
|
|
|
|
description: t(`tools.${toolI18nKey}.description`, tool.description),
|
|
|
|
category: t(`tools.categories.${tool.category.toLowerCase()}`, tool.category),
|
|
|
|
});
|
|
|
|
}));
|
2022-12-17 01:30:02 +01:00
|
|
|
|
2023-11-01 15:38:19 +01:00
|
|
|
const toolsByCategory = computed<ToolCategory[]>(() => {
|
|
|
|
return _.chain(tools.value)
|
|
|
|
.groupBy('category')
|
2024-10-22 10:21:29 +02:00
|
|
|
.map((components, name, path) => ({
|
2023-11-01 15:38:19 +01:00
|
|
|
name,
|
2024-10-22 10:21:29 +02:00
|
|
|
path,
|
2023-11-01 15:38:19 +01:00
|
|
|
components,
|
|
|
|
}))
|
|
|
|
.value();
|
|
|
|
});
|
|
|
|
|
|
|
|
const favoriteTools = computed(() => {
|
|
|
|
return favoriteToolsName.value
|
2024-10-22 10:21:29 +02:00
|
|
|
.map(favoriteName => tools.value.find(({ name, path }) => name === favoriteName || path === favoriteName))
|
2023-11-01 15:38:19 +01:00
|
|
|
.filter(Boolean) as ToolWithCategory[]; // cast because .filter(Boolean) does not remove undefined from type
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
tools,
|
|
|
|
favoriteTools,
|
|
|
|
toolsByCategory,
|
|
|
|
newTools: computed(() => tools.value.filter(({ isNew }) => isNew)),
|
2022-12-17 01:30:02 +01:00
|
|
|
|
|
|
|
addToolToFavorites({ tool }: { tool: MaybeRef<Tool> }) {
|
2024-10-25 09:27:14 -07:00
|
|
|
const toolPath = get(tool).path;
|
|
|
|
if (toolPath) {
|
|
|
|
favoriteToolsName.value.push(toolPath);
|
|
|
|
}
|
2022-12-17 01:30:02 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
removeToolFromFavorites({ tool }: { tool: MaybeRef<Tool> }) {
|
2024-10-22 10:21:29 +02:00
|
|
|
favoriteToolsName.value = favoriteToolsName.value.filter(name => get(tool).name !== name && get(tool).path !== name);
|
2022-12-17 01:30:02 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
isToolFavorite({ tool }: { tool: MaybeRef<Tool> }) {
|
2024-10-22 10:21:29 +02:00
|
|
|
return favoriteToolsName.value.includes(get(tool).name)
|
|
|
|
|| favoriteToolsName.value.includes(get(tool).path);
|
2022-12-17 01:30:02 +01:00
|
|
|
},
|
2024-10-25 13:42:12 -07:00
|
|
|
|
|
|
|
updateFavoriteTools(newOrder: ToolWithCategory[]) {
|
|
|
|
favoriteToolsName.value = newOrder.map(tool => tool.path);
|
|
|
|
},
|
2023-11-01 15:38:19 +01:00
|
|
|
};
|
2022-12-17 01:30:02 +01:00
|
|
|
});
|