Skip to content

Commit e9e4503

Browse files
authored
refactor: improve frontend code (#405)
* refactor: refactored frontend code for improve maintenanceability * fix: layout corruption in asset list view due to upgrading shadcn/ui Card component * refactor: adjust layout and text selection availability and colors * refactor: improve useEffect deps * fix: unable to open edit asset dialog * refactor: delete debug code
1 parent 4764c31 commit e9e4503

File tree

122 files changed

+735
-1007
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+735
-1007
lines changed

eslint.config.mjs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
1-
import eslint from "@eslint/js";
2-
import tseslint from "typescript-eslint";
3-
import unusedImports from "eslint-plugin-unused-imports";
1+
import eslint from '@eslint/js'
2+
import tseslint from 'typescript-eslint'
3+
import unusedImports from 'eslint-plugin-unused-imports'
4+
import reactHooks from 'eslint-plugin-react-hooks'
45

56
export default tseslint.config(
67
eslint.configs.recommended,
78
tseslint.configs.recommended,
89
{
9-
files: ["**/*.ts", "**/*.js", "**/*.tsx"],
10+
files: ['**/*.ts', '**/*.js', '**/*.tsx'],
1011

1112
plugins: {
12-
"unused-imports": unusedImports,
13+
'unused-imports': unusedImports,
14+
'react-hooks': reactHooks,
1315
},
1416

1517
languageOptions: {
1618
parserOptions: {
1719
ecmaVersion: 2019,
18-
sourceType: "module",
20+
sourceType: 'module',
1921
},
2022
},
2123

2224
rules: {
2325
semi: [
24-
"error",
25-
"never",
26+
'error',
27+
'never',
2628
{
27-
beforeStatementContinuationChars: "never",
29+
beforeStatementContinuationChars: 'never',
2830
},
2931
],
3032

31-
"semi-spacing": [
32-
"error",
33+
'semi-spacing': [
34+
'error',
3335
{
3436
after: true,
3537
before: false,
3638
},
3739
],
3840

39-
"semi-style": ["error", "first"],
40-
"no-extra-semi": "error",
41-
"no-unexpected-multiline": "error",
42-
"no-unreachable": "error",
43-
"no-unused-vars": "off",
44-
"unused-imports/no-unused-imports": "error",
41+
'semi-style': ['error', 'first'],
42+
'no-extra-semi': 'error',
43+
'no-unexpected-multiline': 'error',
44+
'no-unreachable': 'error',
45+
'no-unused-vars': 'off',
46+
'unused-imports/no-unused-imports': 'error',
47+
'react-hooks/rules-of-hooks': 'error',
48+
'react-hooks/exhaustive-deps': 'error',
4549

46-
"unused-imports/no-unused-vars": [
47-
"error",
50+
'unused-imports/no-unused-vars': [
51+
'error',
4852
{
49-
vars: "all",
50-
varsIgnorePattern: "^_",
51-
args: "after-used",
52-
argsIgnorePattern: "^_",
53+
vars: 'all',
54+
varsIgnorePattern: '^_',
55+
args: 'after-used',
56+
argsIgnorePattern: '^_',
5357
},
5458
],
5559
},
56-
}
57-
);
60+
},
61+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@vitest/coverage-v8": "3.1.1",
7676
"eslint": "9.25.0",
7777
"eslint-plugin-react": "7.37.5",
78+
"eslint-plugin-react-hooks": "^5.2.0",
7879
"eslint-plugin-unused-imports": "4.1.4",
7980
"globals": "16.0.0",
8081
"husky": "9.1.7",

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/src/command/asset/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub async fn get_asset(
4141

4242
#[tauri::command]
4343
#[specta::specta]
44-
pub async fn get_sorted_assets_for_display(
44+
pub async fn get_sorted_asset_summaries(
4545
basic_store: State<'_, Arc<Mutex<StoreProvider>>>,
4646
sort_by: SortBy,
4747
) -> Result<Vec<AssetSummary>, String> {

src-tauri/src/command/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn generate_tauri_specta_builder() -> Builder<tauri::Wry> {
1414
Builder::<tauri::Wry>::new().commands(collect_commands![
1515
// アセット関連
1616
asset::get::get_asset,
17-
asset::get::get_sorted_assets_for_display,
17+
asset::get::get_sorted_asset_summaries,
1818
asset::get::get_asset_displays_by_booth_id,
1919
asset::create::request_avatar_import,
2020
asset::create::request_avatar_wearable_import,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { AssetSummary } from '@/lib/bindings'
2+
import { AssetContextType } from '.'
3+
import { useCallback, useContext, useEffect, useState } from 'react'
4+
import { refreshAssets } from './logic'
5+
import { PersistentContext } from '../PersistentContext'
6+
7+
type ReturnProps = {
8+
value: AssetContextType
9+
}
10+
11+
export const useAssetContext = (): ReturnProps => {
12+
const [assetDisplaySortedList, setAssetDisplaySortedList] = useState<
13+
AssetSummary[]
14+
>([])
15+
const [filteredIds, setFilteredIds] = useState<string[] | null>(null)
16+
17+
const { sortBy } = useContext(PersistentContext)
18+
19+
const refresh = useCallback(async () => {
20+
const result = await refreshAssets(sortBy)
21+
22+
if (result !== null) {
23+
setAssetDisplaySortedList(result)
24+
}
25+
}, [sortBy])
26+
27+
useEffect(() => {
28+
refresh()
29+
}, [refresh])
30+
31+
const value: AssetContextType = {
32+
assetDisplaySortedList,
33+
setAssetDisplaySortedList: setAssetDisplaySortedList,
34+
35+
filteredIds: filteredIds,
36+
setFilteredIds: setFilteredIds,
37+
38+
deleteAssetById: async (id: string) => {
39+
setAssetDisplaySortedList((prev) =>
40+
prev.filter((asset) => asset.id !== id),
41+
)
42+
},
43+
44+
refreshAssets: refresh,
45+
}
46+
47+
return { value }
48+
}

src/components/context/AssetContext/index.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { AssetSummary, AssetType } from '@/lib/bindings'
2-
import { createContext } from 'react'
1+
import { AssetSummary } from '@/lib/bindings'
2+
import { createContext, FC } from 'react'
3+
import { useAssetContext } from './hook'
34

45
export type AssetContextType = {
56
assetDisplaySortedList: AssetSummary[]
@@ -10,7 +11,7 @@ export type AssetContextType = {
1011

1112
deleteAssetById: (id: string) => void
1213

13-
refreshAssets: (assetType?: AssetType) => Promise<void>
14+
refreshAssets: () => Promise<void>
1415
}
1516

1617
export const AssetContext = createContext<AssetContextType>({
@@ -24,3 +25,13 @@ export const AssetContext = createContext<AssetContextType>({
2425

2526
refreshAssets: async () => {},
2627
})
28+
29+
type Props = {
30+
children: React.ReactNode
31+
}
32+
33+
export const AssetContextProvider: FC<Props> = ({ children }) => {
34+
const { value } = useAssetContext()
35+
36+
return <AssetContext.Provider value={value}>{children}</AssetContext.Provider>
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AssetSummary, commands, SortBy } from '@/lib/bindings'
2+
3+
export const refreshAssets = async (
4+
sortBy: SortBy,
5+
): Promise<AssetSummary[] | null> => {
6+
const result = await commands.getSortedAssetSummaries(sortBy)
7+
8+
if (result.status === 'error') {
9+
console.error(result.error)
10+
return null
11+
}
12+
13+
return result.data
14+
}

src/components/context/DragDropContext/hook/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ReturnProps = {
1818

1919
const DEFAULT_PRIORITY = 100
2020

21-
const useDragDropContext = (): ReturnProps => {
21+
export const useDragDropContext = (): ReturnProps => {
2222
const [fnList, setFnList] = useState<StateType[]>([])
2323

2424
const register = useCallback(
@@ -85,7 +85,7 @@ const useDragDropContext = (): ReturnProps => {
8585
isCancelled = true
8686
unlistenFn?.()
8787
}
88-
}, [fnList, setFnList])
88+
}, [fnList])
8989

9090
const dragDropContextValue: DragDropContextType = {
9191
register,
@@ -95,5 +95,3 @@ const useDragDropContext = (): ReturnProps => {
9595
dragDropContextValue,
9696
}
9797
}
98-
99-
export default useDragDropContext

src/components/context/DragDropContext/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { createContext, FC } from 'react'
2-
import useDragDropContext from './hook'
2+
import { useDragDropContext } from './hook'
33
import { Event } from '@tauri-apps/api/event'
44
import { DragDropEvent } from '@tauri-apps/api/window'
55

66
export type DragDropRegisterConfig = {
77
uniqueId: string
88
priority?: number
99
}
10+
1011
export type DragDropHandlingFn = (
1112
event: Event<DragDropEvent>,
1213
) => Promise<boolean>
@@ -23,7 +24,7 @@ type Props = {
2324
children: React.ReactNode
2425
}
2526

26-
const DragDropContextProvider: FC<Props> = ({ children }) => {
27+
export const DragDropContextProvider: FC<Props> = ({ children }) => {
2728
const { dragDropContextValue } = useDragDropContext()
2829

2930
return (
@@ -32,5 +33,3 @@ const DragDropContextProvider: FC<Props> = ({ children }) => {
3233
</DragDropContext.Provider>
3334
)
3435
}
35-
36-
export default DragDropContextProvider

0 commit comments

Comments
 (0)