Skip to content

Commit

Permalink
chore: update ts definition generator
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 3, 2023
1 parent 7cde25b commit 2fcd496
Show file tree
Hide file tree
Showing 29 changed files with 12,178 additions and 55 deletions.
4 changes: 4 additions & 0 deletions api/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
),
)
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(secret, store))
apiv2pb.RegisterUserSettingServiceServer(grpcServer, NewUserSettingService(store))
apiv2pb.RegisterShortcutServiceServer(grpcServer, NewShortcutService(secret, store))

return &APIV2Service{
Expand Down Expand Up @@ -62,6 +63,9 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
if err := apiv2pb.RegisterUserServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
if err := apiv2pb.RegisterUserSettingServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
if err := apiv2pb.RegisterShortcutServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@types/lodash-es": "^4.17.8",
"@types/node": "^20.5.6",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.4.1",
Expand All @@ -44,9 +43,11 @@
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.33.2",
"long": "^5.2.3",
"postcss": "^8.4.28",
"prettier": "2.6.2",
"protobufjs": "^7.2.5",
"typescript": "^5.2.2",
"vite": "^4.4.9"
}
}
}
92 changes: 80 additions & 12 deletions frontend/web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/web/src/components/setting/AccessTokenSection.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Button, IconButton } from "@mui/joy";
import { ListUserAccessTokensResponse, UserAccessToken } from "@pbtypes/proto/api/v2/user_service_pb";
import axios from "axios";
import copy from "copy-to-clipboard";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { ListUserAccessTokensResponse, UserAccessToken } from "@/types/proto/api/v2/user_service";
import useUserStore from "../../stores/v1/user";
import { showCommonDialog } from "../Alert";
import CreateAccessTokenDialog from "../CreateAccessTokenDialog";
Expand Down
23 changes: 22 additions & 1 deletion frontend/web/src/layouts/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Outlet, useNavigate } from "react-router-dom";
import { UserSetting_Locale } from "@/types/proto/api/v2/user_setting_service";
import Header from "../components/Header";
import useUserStore from "../stores/v1/user";

const Root: React.FC = () => {
const navigate = useNavigate();
const currentUser = useUserStore().getCurrentUser();
const { i18n } = useTranslation();
const userStore = useUserStore();
const currentUser = userStore.getCurrentUser();
const currentUserSetting = userStore.getCurrentUserSetting();

useEffect(() => {
if (!currentUser) {
navigate("/auth", {
replace: true,
});
return;
}

// Prepare user setting.
userStore.fetchUserSetting(currentUser.id);
}, []);

useEffect(() => {
if (!currentUserSetting) {
return;
}

if (currentUserSetting.locale === UserSetting_Locale.LOCALE_EN) {
i18n.changeLanguage("en");
} else if (currentUserSetting.locale === UserSetting_Locale.LOCALE_ZH) {
i18n.changeLanguage("zh");
}
}, [currentUserSetting]);

return (
<>
{currentUser && (
Expand Down
27 changes: 27 additions & 0 deletions frontend/web/src/stores/v1/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from "axios";
import { create } from "zustand";
import { GetUserSettingResponse, UserSetting } from "@/types/proto/api/v2/user_setting_service";
import * as api from "../../helpers/api";

const convertResponseModelUser = (user: User): User => {
Expand All @@ -11,7 +13,10 @@ const convertResponseModelUser = (user: User): User => {

interface UserState {
userMapById: Record<UserId, User>;
userSettingMapById: Record<UserId, UserSetting>;
currentUserId?: UserId;

// User related actions.
fetchUserList: () => Promise<User[]>;
fetchCurrentUser: () => Promise<User>;
getOrFetchUserById: (id: UserId) => Promise<User>;
Expand All @@ -20,10 +25,15 @@ interface UserState {
createUser: (userCreate: UserCreate) => Promise<User>;
patchUser: (userPatch: UserPatch) => Promise<void>;
deleteUser: (id: UserId) => Promise<void>;

// User setting related actions.
fetchUserSetting: (userId: UserId) => Promise<UserSetting>;
getCurrentUserSetting: () => UserSetting;
}

const useUserStore = create<UserState>()((set, get) => ({
userMapById: {},
userSettingMapById: {},
fetchUserList: async () => {
const { data: userList } = await api.getUserList();
const userMap = get().userMapById;
Expand Down Expand Up @@ -83,6 +93,23 @@ const useUserStore = create<UserState>()((set, get) => ({
const currentUserId = get().currentUserId;
return userMap[currentUserId as UserId];
},
fetchUserSetting: async (userId: UserId) => {
const {
data: { userSetting },
} = await axios.get<GetUserSettingResponse>(`api/v2/users/${userId}/settings`);
if (!userSetting) {
throw new Error(`User setting not found for user ${userId}`);
}
const userSettingMap = get().userSettingMapById;
userSettingMap[userId] = userSetting;
set(userSettingMap);
return userSetting;
},
getCurrentUserSetting: () => {
const userSettingMap = get().userSettingMapById;
const currentUserId = get().currentUserId;
return userSettingMap[currentUserId as UserId];
},
}));

export default useUserStore;
42 changes: 42 additions & 0 deletions frontend/web/src/types/proto/api/v2/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable */

export const protobufPackage = "slash.api.v2";

export enum RowStatus {
ROW_STATUS_UNSPECIFIED = 0,
NORMAL = 1,
ARCHIVED = 2,
UNRECOGNIZED = -1,
}

export function rowStatusFromJSON(object: any): RowStatus {
switch (object) {
case 0:
case "ROW_STATUS_UNSPECIFIED":
return RowStatus.ROW_STATUS_UNSPECIFIED;
case 1:
case "NORMAL":
return RowStatus.NORMAL;
case 2:
case "ARCHIVED":
return RowStatus.ARCHIVED;
case -1:
case "UNRECOGNIZED":
default:
return RowStatus.UNRECOGNIZED;
}
}

export function rowStatusToJSON(object: RowStatus): string {
switch (object) {
case RowStatus.ROW_STATUS_UNSPECIFIED:
return "ROW_STATUS_UNSPECIFIED";
case RowStatus.NORMAL:
return "NORMAL";
case RowStatus.ARCHIVED:
return "ARCHIVED";
case RowStatus.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}
Loading

0 comments on commit 2fcd496

Please sign in to comment.