Skip to content

Commit

Permalink
sessionStorageよりも更に短命な方法で持つように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
samunohito committed Feb 15, 2025
1 parent a28dadd commit f69b2f1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/frontend/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { defineAsyncComponent, reactive, ref } from 'vue';
import * as Misskey from 'misskey-js';
import { apiUrl } from '@@/js/config.js';
import type { MenuItem, MenuButton } from '@/types/menu.js';
import { defaultMemoryStorage } from '@/memory-storage';
import { showSuspendedDialog } from '@/scripts/show-suspended-dialog.js';
import { i18n } from '@/i18n.js';
import { miLocalStorage } from '@/local-storage.js';
Expand Down Expand Up @@ -40,6 +41,8 @@ export function incNotesCount() {
export async function signout() {
if (!$i) return;

defaultMemoryStorage.clear();

waiting();
document.cookie.split(';').forEach((cookie) => {
const cookieName = cookie.split('=')[0].trim();
Expand Down Expand Up @@ -107,7 +110,7 @@ export async function removeAccount(idOrToken: Account['id']) {
}

function fetchAccount(token: string, id?: string, forceShowDialog?: boolean): Promise<Account> {
document.cookie = "token=; path=/; max-age=0";
document.cookie = 'token=; path=/; max-age=0';
document.cookie = `token=${token}; path=/queue; max-age=86400; SameSite=Strict; Secure`; // bull dashboardの認証とかで使う

return new Promise((done, fail) => {
Expand Down
51 changes: 51 additions & 0 deletions packages/frontend/src/memory-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

export type MemoryStorage = {
has: (key: string) => boolean;
getItem: <T>(key: string) => T | null;
setItem: (key: string, value: unknown) => void;
removeItem: (key: string) => void;
clear: () => void;
size: number;
};

class MemoryStorageImpl implements MemoryStorage {
private readonly storage: Map<string, unknown>;

constructor() {
this.storage = new Map();
}

has(key: string): boolean {
return this.storage.has(key);
}

getItem<T>(key: string): T | null {
return this.storage.has(key) ? this.storage.get(key) as T : null;
}

setItem(key: string, value: unknown): void {
this.storage.set(key, value);
}

removeItem(key: string): void {
this.storage.delete(key);
}

clear(): void {
this.storage.clear();
}

get size(): number {
return this.storage.size;
}
}

export function createMemoryStorage(): MemoryStorage {
return new MemoryStorageImpl();
}

export const defaultMemoryStorage: MemoryStorage = createMemoryStorage();
7 changes: 4 additions & 3 deletions packages/frontend/src/pages/admin/users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import { computed, shallowRef, ref, watchEffect } from 'vue';
import XHeader from './_header_.vue';
import { defaultMemoryStorage } from '@/memory-storage';
import MkButton from '@/components/MkButton.vue';
import MkInput from '@/components/MkInput.vue';
import MkSelect from '@/components/MkSelect.vue';
Expand All @@ -79,10 +80,10 @@ type SearchQuery = {
origin?: string;
username?: string;
hostname?: string;
}
};

const paginationComponent = shallowRef<InstanceType<typeof MkPagination>>();
const storedQuery = JSON.parse(sessionStorage.getItem('admin-users-query') ?? '{}') as SearchQuery;
const storedQuery = JSON.parse(defaultMemoryStorage.getItem('admin-users-query') ?? '{}') as SearchQuery;

const sort = ref(storedQuery.sort ?? '+createdAt');
const state = ref(storedQuery.state ?? 'all');
Expand Down Expand Up @@ -159,7 +160,7 @@ const headerActions = computed(() => [{
const headerTabs = computed(() => []);

watchEffect(() => {
sessionStorage.setItem('admin-users-query', JSON.stringify({
defaultMemoryStorage.setItem('admin-users-query', JSON.stringify({
sort: sort.value,
state: state.value,
origin: origin.value,
Expand Down

0 comments on commit f69b2f1

Please sign in to comment.