53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { create } from 'zustand';
|
|
import { persist } from 'zustand/middleware';
|
|
|
|
// Define interface for team, project and tag objects
|
|
interface TeamData {
|
|
team_id: string;
|
|
team_name: string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
interface ProjectData {
|
|
project_id: string;
|
|
project_name: string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
// 定义 ShortUrl 数据类型
|
|
export interface ShortUrlData {
|
|
id: string;
|
|
externalId: string;
|
|
slug: string;
|
|
originalUrl: string;
|
|
title?: string;
|
|
shortUrl: string;
|
|
teams?: TeamData[];
|
|
projects?: ProjectData[];
|
|
tags?: string[];
|
|
createdAt?: string;
|
|
domain?: string;
|
|
}
|
|
|
|
// 定义 store 类型
|
|
interface ShortUrlStore {
|
|
selectedShortUrl: ShortUrlData | null;
|
|
setSelectedShortUrl: (shortUrl: ShortUrlData | null) => void;
|
|
clearSelectedShortUrl: () => void;
|
|
}
|
|
|
|
// 创建 store 并使用 persist 中间件保存到 localStorage
|
|
export const useShortUrlStore = create<ShortUrlStore>()(
|
|
persist(
|
|
(set) => ({
|
|
selectedShortUrl: null,
|
|
setSelectedShortUrl: (shortUrl) => set({ selectedShortUrl: shortUrl }),
|
|
clearSelectedShortUrl: () => set({ selectedShortUrl: null }),
|
|
}),
|
|
{
|
|
name: 'shorturl-storage', // localStorage 中的 key 名称
|
|
partialize: (state) => ({ selectedShortUrl: state.selectedShortUrl }), // 只持久化 selectedShortUrl
|
|
}
|
|
)
|
|
);
|