diff --git a/apps/api-gql/internal/gql/resolvers/overlays.resolver.go b/apps/api-gql/internal/gql/resolvers/overlays.resolver.go index 209a0c522..12e4beff0 100644 --- a/apps/api-gql/internal/gql/resolvers/overlays.resolver.go +++ b/apps/api-gql/internal/gql/resolvers/overlays.resolver.go @@ -14,11 +14,18 @@ import ( ) // ChatOverlayUpdate is the resolver for the chatOverlayUpdate field. -func (r *mutationResolver) ChatOverlayUpdate( - ctx context.Context, - opts gqlmodel.ChatOverlayUpdateOpts, -) (bool, error) { - return r.updateChatOverlay(ctx, opts) +func (r *mutationResolver) ChatOverlayUpdate(ctx context.Context, id string, opts gqlmodel.ChatOverlayMutateOpts) (bool, error) { + return r.updateChatOverlay(ctx, id, opts) +} + +// ChatOverlayCreate is the resolver for the chatOverlayCreate field. +func (r *mutationResolver) ChatOverlayCreate(ctx context.Context, opts gqlmodel.ChatOverlayMutateOpts) (bool, error) { + panic(fmt.Errorf("not implemented: ChatOverlayCreate - chatOverlayCreate")) +} + +// ChatOverlayDelete is the resolver for the chatOverlayDelete field. +func (r *mutationResolver) ChatOverlayDelete(ctx context.Context, id string) (bool, error) { + panic(fmt.Errorf("not implemented: ChatOverlayDelete - chatOverlayDelete")) } // ChatOverlays is the resolver for the chatOverlays field. @@ -32,10 +39,7 @@ func (r *queryResolver) ChatOverlays(ctx context.Context) ([]gqlmodel.ChatOverla } // ChatOverlaysByID is the resolver for the chatOverlaysById field. -func (r *queryResolver) ChatOverlaysByID(ctx context.Context, id string) ( - *gqlmodel.ChatOverlay, - error, -) { +func (r *queryResolver) ChatOverlaysByID(ctx context.Context, id string) (*gqlmodel.ChatOverlay, error) { dashboardId, err := r.sessions.GetSelectedDashboard(ctx) if err != nil { return nil, err @@ -45,11 +49,7 @@ func (r *queryResolver) ChatOverlaysByID(ctx context.Context, id string) ( } // ChatOverlaySettings is the resolver for the chatOverlaySettings field. -func (r *subscriptionResolver) ChatOverlaySettings( - ctx context.Context, - id string, - apiKey string, -) (<-chan *gqlmodel.ChatOverlay, error) { +func (r *subscriptionResolver) ChatOverlaySettings(ctx context.Context, id string, apiKey string) (<-chan *gqlmodel.ChatOverlay, error) { user := model.Users{} if err := r.gorm.Where(`"apiKey" = ?`, apiKey).First(&user).Error; err != nil { return nil, fmt.Errorf("failed to get user: %w", err) diff --git a/apps/api-gql/internal/gql/resolvers/overlays.resolver.service.go b/apps/api-gql/internal/gql/resolvers/overlays.resolver.service.go index ecb9f4c93..da9cc596f 100644 --- a/apps/api-gql/internal/gql/resolvers/overlays.resolver.service.go +++ b/apps/api-gql/internal/gql/resolvers/overlays.resolver.service.go @@ -6,17 +6,11 @@ import ( "log/slog" "github.com/google/uuid" - "github.com/samber/lo" model "github.com/satont/twir/libs/gomodels" "github.com/twirapp/twir/apps/api-gql/internal/gql/gqlmodel" ) func chatOverlayDbToGql(entity *model.ChatOverlaySettings) *gqlmodel.ChatOverlay { - var animation *gqlmodel.ChatOverlayAnimation - if entity.Animation != nil { - animation = lo.ToPtr(gqlmodel.ChatOverlayAnimation(*entity.Animation)) - } - return &gqlmodel.ChatOverlay{ ID: entity.ID.String(), MessageHideTimeout: int(entity.MessageHideTimeout), @@ -35,7 +29,7 @@ func chatOverlayDbToGql(entity *model.ChatOverlaySettings) *gqlmodel.ChatOverlay FontWeight: int(entity.FontWeight), FontStyle: entity.FontStyle, PaddingContainer: int(entity.PaddingContainer), - Animation: animation, + Animation: gqlmodel.ChatOverlayAnimation(entity.Animation), } } @@ -83,7 +77,8 @@ func (r *Resolver) getChatOverlaySettings( func (r *mutationResolver) updateChatOverlay( ctx context.Context, - opts gqlmodel.ChatOverlayUpdateOpts, + id string, + opts gqlmodel.ChatOverlayMutateOpts, ) (bool, error) { dashboardId, err := r.sessions.GetSelectedDashboard(ctx) if err != nil { @@ -93,7 +88,7 @@ func (r *mutationResolver) updateChatOverlay( entity := model.ChatOverlaySettings{} if err := r.gorm. WithContext(ctx). - Where("channel_id = ? AND id = ?", dashboardId, opts.ID). + Where("channel_id = ? AND id = ?", dashboardId, id). First(&entity).Error; err != nil { return false, fmt.Errorf("failed to get chat overlay settings: %w", err) } @@ -163,7 +158,7 @@ func (r *mutationResolver) updateChatOverlay( } if opts.Animation.IsSet() { - entity.Animation = lo.ToPtr(model.ChatOverlaySettingsAnimationType(*opts.Animation.Value())) + entity.Animation = model.ChatOverlaySettingsAnimationType(*opts.Animation.Value()) } if err := r.gorm. diff --git a/apps/api-gql/schema/overlays.graphqls b/apps/api-gql/schema/overlays.graphqls index fc624e77b..184f515c5 100644 --- a/apps/api-gql/schema/overlays.graphqls +++ b/apps/api-gql/schema/overlays.graphqls @@ -4,7 +4,9 @@ extend type Query { } extend type Mutation { - chatOverlayUpdate(opts: ChatOverlayUpdateOpts!): Boolean! @isAuthenticated @hasChannelRolesDashboardPermission(permission: MANAGE_OVERLAYS) + chatOverlayUpdate(id: String!, opts: ChatOverlayMutateOpts!): Boolean! @isAuthenticated @hasChannelRolesDashboardPermission(permission: MANAGE_OVERLAYS) + chatOverlayCreate(opts: ChatOverlayMutateOpts!): Boolean! @isAuthenticated @hasChannelRolesDashboardPermission(permission: MANAGE_OVERLAYS) + chatOverlayDelete(id: String!): Boolean! @isAuthenticated @hasChannelRolesDashboardPermission(permission: MANAGE_OVERLAYS) } extend type Subscription { @@ -13,6 +15,7 @@ extend type Subscription { enum ChatOverlayAnimation { DISABLED + DEFAULT } type ChatOverlay { @@ -33,11 +36,10 @@ type ChatOverlay { fontWeight: Int! fontStyle: String! paddingContainer: Int! - animation: ChatOverlayAnimation + animation: ChatOverlayAnimation! } -input ChatOverlayUpdateOpts { - id: String! +input ChatOverlayMutateOpts { messageHideTimeout: Int messageShowDelay: Int preset: String diff --git a/frontend/dashboard/codegen.ts b/frontend/dashboard/codegen.ts index 137b6b706..4e76120ac 100644 --- a/frontend/dashboard/codegen.ts +++ b/frontend/dashboard/codegen.ts @@ -1,8 +1,9 @@ -import { join, resolve } from 'path'; +import { join, resolve } from 'node:path' +import process from 'node:process' -import type { CodegenConfig } from '@graphql-codegen/cli'; +import type { CodegenConfig } from '@graphql-codegen/cli' -const schemaDir = resolve(join(process.cwd(), '..', '..', 'apps', 'api-gql', 'schema', '*.graphqls')); +const schemaDir = resolve(join(process.cwd(), '..', '..', 'apps', 'api-gql', 'schema', '*.graphqls')) const config: CodegenConfig = { config: { @@ -21,6 +22,6 @@ const config: CodegenConfig = { }, }, }, -}; +} -export default config; +export default config diff --git a/frontend/dashboard/src/api/overlays/chat.ts b/frontend/dashboard/src/api/overlays/chat.ts index 3dcb7c1d9..c2f7604e3 100644 --- a/frontend/dashboard/src/api/overlays/chat.ts +++ b/frontend/dashboard/src/api/overlays/chat.ts @@ -1,71 +1,73 @@ -import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'; -import type { - Settings, - UpdateRequest, - GetAllResponse, -} from '@twir/api/messages/overlays_chat/overlays_chat'; -import { unref } from 'vue'; -import type { MaybeRef } from 'vue'; +import { useQuery } from '@urql/vue' +import { createGlobalState } from '@vueuse/core' -import { protectedApiClient } from '@/api/twirp.js'; +import { useMutation } from '@/composables/use-mutation' +import { graphql } from '@/gql' -export const useChatOverlayManager = () => { - const queryClient = useQueryClient(); - const queryKey = 'chatOverlay'; +const cacheKey = ['chatOverlays'] - return { - useGet: (id: MaybeRef) => useQuery({ - queryKey: [queryKey, id], - queryFn: async (): Promise => { - try { - const call = await protectedApiClient.overlayChatGet({ - id: unref(id), - }); - return call.response; - } catch { - return null; +export const useChatOverlayApi = createGlobalState(() => { + const useOverlaysQuery = () => useQuery({ + query: graphql(` + query UseOverlaysData { + chatOverlays { + id + messageHideTimeout + messageShowDelay + preset + fontSize + hideCommands + hideBots + fontFamily + showBadges + showAnnounceBadge + textShadowColor + textShadowSize + chatBackgroundColor + direction + fontWeight + fontStyle + paddingContainer + animation } - }, - }), - useGetAll: () => useQuery({ - queryKey: [queryKey], - queryFn: async (): Promise => { - const call = await protectedApiClient.overlayChatGetAll({}); - return call.response; - }, - }), - useCreate: () => useMutation({ - mutationKey: ['chatOverlayCreate'], - mutationFn: async (opts: MaybeRef) => { - const data = unref(opts); - const call = await protectedApiClient.overlayChatCreate(data); - return call.response; - }, - onSuccess: async () => { - await queryClient.invalidateQueries([queryKey]); - }, - }), - useUpdate: () => useMutation({ - mutationKey: ['chatOverlayUpdate'], - mutationFn: async (opts: MaybeRef) => { - const data = unref(opts); - await protectedApiClient.overlayChatUpdate(data); - }, - onSuccess: async (_, opts) => { - const data = unref(opts); - await queryClient.invalidateQueries([queryKey, data.id]); - }, - }), - useDelete: () => useMutation({ - mutationKey: ['chatOverlayDelete'], - mutationFn: async (id: MaybeRef) => { - await protectedApiClient.overlayChatDelete({ - id: unref(id), - }); - }, - onSuccess: async () => { - await queryClient.invalidateQueries([queryKey]); - }, - }), - }; -}; + } + `), + context: { + additionalTypenames: cacheKey, + }, + }) + + const useOverlayDelete = () => useMutation( + graphql(` + mutation DeleteOverlay($id: String!) { + chatOverlayDelete(id: $id) + } + `), + cacheKey, + ) + + const useOverlayCreate = () => useMutation( + graphql(` + mutation CreateOverlay($input: ChatOverlayMutateOpts!) { + chatOverlayCreate(opts: $input) + } + `), + cacheKey, + ) + + const useOverlayUpdate = () => useMutation( + graphql(` + mutation UpdateOverlay($id: String!, $input: ChatOverlayMutateOpts!) { + chatOverlayUpdate(id: $id, opts: $input) + } + `), + cacheKey, + ) + + return { + useOverlaysQuery, + useOverlayDelete, + useOverlayCreate, + useOverlayUpdate, + } +}) diff --git a/frontend/dashboard/src/components/overlays/chat.vue b/frontend/dashboard/src/components/overlays/chat.vue index 973998106..20379c36d 100644 --- a/frontend/dashboard/src/components/overlays/chat.vue +++ b/frontend/dashboard/src/components/overlays/chat.vue @@ -1,17 +1,17 @@ diff --git a/frontend/dashboard/src/pages/overlays/chat/Chat.vue b/frontend/dashboard/src/pages/overlays/chat/Chat.vue index 7f69bfd46..88469dde4 100644 --- a/frontend/dashboard/src/pages/overlays/chat/Chat.vue +++ b/frontend/dashboard/src/pages/overlays/chat/Chat.vue @@ -1,6 +1,5 @@ @@ -177,9 +172,9 @@ const addable = computed(() => { -