Add config migration code
This should ensure that each user has the default tag categories when updating from an older version.pull/55/head
parent
7a296c1729
commit
ab6b0186ee
|
|
@ -11,8 +11,7 @@ import {
|
|||
TemporaryFileUploadResult,
|
||||
UpdatePostRequest,
|
||||
} from "./models";
|
||||
import { ScrapedPostDetails } from "~/models";
|
||||
import { SzuruSiteConfig } from "~/config";
|
||||
import { ScrapedPostDetails, SzuruSiteConfig } from "~/models";
|
||||
|
||||
/**
|
||||
* A 1:1 wrapper around the szurubooru API.
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
export class SzuruSiteConfig {
|
||||
id = window.crypto.randomUUID();
|
||||
domain = "https://example.com";
|
||||
username = "user";
|
||||
authToken = "";
|
||||
}
|
||||
|
||||
export class TagCategoryColor {
|
||||
constructor(public name: string, public color: string) {}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { TagCategoryColor } from "./config";
|
||||
import { TagCategoryColor } from "./models";
|
||||
|
||||
export function makeCssName(prefix: string, name: string) {
|
||||
return prefix + "-" + name.replace(/[^a-z0-9]/g, "_");
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag } from "neo-scraper";
|
||||
import { MicroTag, Tag, UpdatePostRequest } from "~/api/models";
|
||||
import { SzuruSiteConfig } from "~/config";
|
||||
|
||||
export class TagDetails {
|
||||
public implications: TagDetails[] = [];
|
||||
|
|
@ -146,3 +145,21 @@ export class PostUpdateCommandData {
|
|||
export class FetchCommandData {
|
||||
constructor(public readonly url: string, public readonly options: RequestInit | undefined = undefined) {}
|
||||
}
|
||||
|
||||
export class SzuruSiteConfig {
|
||||
id = window.crypto.randomUUID();
|
||||
domain = "https://example.com";
|
||||
username = "user";
|
||||
authToken = "";
|
||||
}
|
||||
|
||||
export class TagCategoryColor {
|
||||
constructor(public name: string, public color: string) {}
|
||||
}
|
||||
|
||||
export const getDefaultTagCategories = () => [
|
||||
new TagCategoryColor("copyright", "#a0a"),
|
||||
new TagCategoryColor("character", "#0a0"),
|
||||
new TagCategoryColor("artist", "#a00"),
|
||||
new TagCategoryColor("meta", "#f80"),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import { useColorMode } from "@vueuse/core";
|
||||
import SzuruWrapper from "~/api";
|
||||
import { SzuruSiteConfig, TagCategoryColor } from "~/config";
|
||||
import { cfg } from "~/stores";
|
||||
import { getErrorMessage } from "~/utils";
|
||||
|
||||
|
|
@ -9,10 +8,11 @@ import TabView from "primevue/tabview";
|
|||
import TabPanel from "primevue/tabpanel";
|
||||
import DataTable from "primevue/datatable";
|
||||
import Column from "primevue/column";
|
||||
import { SzuruSiteConfig, TagCategoryColor, getDefaultTagCategories } from "~/models";
|
||||
|
||||
const statusText = ref("");
|
||||
const statusType = ref("status-quiet");
|
||||
const versionInfo = "Version: " + (import.meta.env.VITE_SZ_VERSION_INFO ?? browser.runtime.getManifest().version);
|
||||
const versionInfo = "Version: " + (import.meta.env.VITE_SZ_VERSION ?? browser.runtime.getManifest().version);
|
||||
|
||||
const columns = ref([
|
||||
{ field: "name", header: "Category name" },
|
||||
|
|
@ -80,12 +80,7 @@ function removeSelectedSite() {
|
|||
|
||||
function resetTagCategories() {
|
||||
cfg.value.tagCategories.splice(0);
|
||||
cfg.value.tagCategories.push(
|
||||
new TagCategoryColor("copyright", "#a0a"),
|
||||
new TagCategoryColor("character", "#0a0"),
|
||||
new TagCategoryColor("artist", "#a00"),
|
||||
new TagCategoryColor("meta", "#f80")
|
||||
);
|
||||
cfg.value.tagCategories.push(...getDefaultTagCategories());
|
||||
}
|
||||
|
||||
function addTagCategory() {
|
||||
|
|
|
|||
|
|
@ -11,12 +11,12 @@ import {
|
|||
SimpleSimilarPost,
|
||||
SimpleImageSearchResult,
|
||||
PostUploadCommandData,
|
||||
SzuruSiteConfig,
|
||||
} from "~/models";
|
||||
import { ImageSearchResult } from "~/api/models";
|
||||
import { isMobile } from "~/env";
|
||||
import { DeepReadonly } from "vue";
|
||||
import { cfg, usePopupStore } from "~/stores";
|
||||
import { SzuruSiteConfig } from "~/config";
|
||||
import SzurubooruApi from "~/api";
|
||||
|
||||
const pop = usePopupStore();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,18 @@
|
|||
import { defineStore } from "pinia";
|
||||
import deepMerge from "deepmerge";
|
||||
import { SzuruSiteConfig, TagCategoryColor } from "~/config";
|
||||
import type { ScrapedPostDetails, SetPostUploadInfoData } from "~/models";
|
||||
import {
|
||||
getDefaultTagCategories,
|
||||
type ScrapedPostDetails,
|
||||
type SetPostUploadInfoData,
|
||||
type SzuruSiteConfig,
|
||||
type TagCategoryColor,
|
||||
} from "~/models";
|
||||
import { useStorageLocal } from "~/composables/useStorageLocal";
|
||||
|
||||
export const cfg = useStorageLocal(
|
||||
"config",
|
||||
{
|
||||
version: 0,
|
||||
addPageUrlToSource: true,
|
||||
autoSearchSimilar: false,
|
||||
loadTagCounts: true,
|
||||
|
|
@ -27,7 +33,29 @@ export const cfg = useStorageLocal(
|
|||
tagCategories: [] as Array<TagCategoryColor>,
|
||||
},
|
||||
{
|
||||
mergeDefaults: (storageValue, defaults) => deepMerge(defaults, storageValue),
|
||||
mergeDefaults(storageValue, defaults) {
|
||||
const cfg = deepMerge(defaults, storageValue);
|
||||
const oldVersion = cfg.version;
|
||||
|
||||
// Crappy config migration.
|
||||
switch (cfg.version) {
|
||||
case 0:
|
||||
cfg.version++;
|
||||
|
||||
// Don't clear the existing tagCategories and don't add duplicates.
|
||||
for (const cat of getDefaultTagCategories()) {
|
||||
if (!cfg.tagCategories.find((x) => x.name == cat.name)) {
|
||||
cfg.tagCategories.push(cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (oldVersion != cfg.version) {
|
||||
console.log(`Migrated config from version ${oldVersion} to ${cfg.version}`);
|
||||
}
|
||||
|
||||
return cfg;
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue